*args的切片问题

*args的类型为tuple,一般是不能随意更改的,除非有新的tuple取代

# Incorrect
*args = ['Hi', 'Bye']
function(args[1:], **kargs) = function(['Bye'], **kargs)
# Correct
*args = ['Hi', 'Bye']
*args = args[1:]
function(*args, **kargs) = function('Bye', **kargs)

关键的问题在于*args取tuple中的数来用,而第一种用法中返回了list导致了错误

代码运行后报错:D:\Anaconda\Lib\site-packages\torch\nn\modules\transformer.py:385: UserWarning: enable_nested_tensor is True, but self.use_nested_tensor is False because encoder_layer.self_attn.batch_first was not True(use batch_first for better inference performance) warnings.warn( Traceback (most recent call last): File “D:\PyCharm\data_test\Transformer_based_trial.py”, line 243, in <module> main() File “D:\PyCharm\data_test\Transformer_based_trial.py”, line 217, in main trained_model = train_model( ^^^^^^^^^^^^ File “D:\PyCharm\data_test\Transformer_based_trial.py”, line 119, in train_model outputs = model(sequences) ^^^^^^^^^^^^^^^^ File “D:\Anaconda\Lib\site-packages\torch\nn\modules\module.py”, line 1749, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “D:\Anaconda\Lib\site-packages\torch\nn\modules\module.py”, line 1760, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “D:\PyCharm\data_test\Transformer_based_trial.py”, line 90, in forward x = self.pos_encoding(x) ^^^^^^^^^^^^^^^^^^^^ File “D:\Anaconda\Lib\site-packages\torch\nn\modules\module.py”, line 1749, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “D:\Anaconda\Lib\site-packages\torch\nn\modules\module.py”, line 1760, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “D:\PyCharm\data_test\Transformer_based_trial.py”, line 46, in forward return x + self.pe[:x.size(0)] ^~~~~~~~~~~~~~~~~~~ RuntimeError: The size of tensor a (24) must match the size of tensor b (10) at non-singleton dimension 1这样的报错还是张量维度的问题吗?如何解决呢?
03-08
Traceback (most recent call last): File "/Users/zhangjingjing/Downloads/YoloV5_PyQt5-master/detect_logical.py", line 162, in button_image_open info_show = self.detect(name_list, img) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangjingjing/Downloads/YoloV5_PyQt5-master/detect_logical.py", line 131, in detect pred = self.model(img, augment=self.opt.augment)[0] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/anaconda3/envs/yolov5/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/anaconda3/envs/yolov5/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangjingjing/Downloads/YoloV5_PyQt5-master/models/yolo.py", line 168, in forward return self._forward_once(x, profile, visualize) # single-scale inference, train ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangjingjing/Downloads/YoloV5_PyQt5-master/models/yolo.py", line 81, in _forward_once x = m(x) # run ^^^^ File "/opt/anaconda3/envs/yolov5/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/anaconda3/envs/yolov5/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangjingjing/Downloads/YoloV5_PyQt5-master/models/yolo.py", line 59, in forward y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
最新发布
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值