TypeError: new(): argument 'size' must be tuple of ints, but found element of type Tensor at pos 3

报错如下:

img

报错的两处地方代码如下:

img

img


请问大家知道这是哪里出错了吗?本菜菜看修改半天也没改对

【以下回答由 GPT 生成】

问题分析: 1. 第一处代码错误: TypeError: new(): argument 'size' must be tuple of ints, but found element of type Tensor at pos 3 错误原因:some_function 函数的输入参数要求为整型的 tuple,但是传入了一个 Tensor 对象。 修改建议:将 tensor 转换为整型的 tuple,可以使用 tensor.tolist() 方法将其转换为 Python 列表,然后再转换为 tuple。

修改后的代码如下:

...
tensor = torch.Tensor([1, 2, 3])
...
output = some_function(tuple(tensor.tolist()))
...
  1. 第二处代码错误: TypeError: 'torch.Size' object cannot be interpreted as an integer 错误原因:torch.zeros() 函数的 size 参数要求为整数的 tuple,但是传入了一个 torch.Size 对象。 修改建议:将 torch.Size 对象转换为整数的 tuple,可以使用 torch.Size 的成员变量进行转换。

修改后的代码如下:

...
size = (256, 256, tensor.size()[2])
...
matrix = torch.zeros(size)
...

经过以上修改后,可以解决报错问题。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

你把代码和报错复制到记事本发过来