错误笔记
- 1. RuntimeError: Expected object of scalar type float but got scalar type __int64 for sequence element 1.
- 2. TypeError: expected Tensor as element 1 in argument 0, but got list
- 3. RuntimeError: All input tensors must be on the same device. Received cuda:0 and cpu
- 4. TypeError: only size-1 arrays can be converted to Python scalars
1. RuntimeError: Expected object of scalar type float but got scalar type __int64 for sequence element 1.
解决办法:
先检查t1和n_t的数据类型,使用t1.dtype
和n_t.dtype
进行检查。
明显看到这两个torch的数据类型是不一样的,那就把n_t的数据类型换成float32。
使用n_t = n_t.float()
就可以将两个torch换成相同的数据类型了。
最后,再执行torch.cat操作就ok了。
2. TypeError: expected Tensor as element 1 in argument 0, but got list
类型不匹配,打印出来看看类型。
上图是list,要是想和另一个B-tensor进行concat,那需要把targets_num转换成tensor,若另一个B-tensor是浮点型,还需要转换成浮点型。
执行代码:torch.tensor(targets_num).float()
3. RuntimeError: All input tensors must be on the same device. Received cuda:0 and cpu
运行torch.cat时候遇到了这个问题,意思是说所有的tensor都必须在相同的设备上运行,因为concat中有一个tensor是自己创建的,不知道是不是错误在这里。。。
解决方法:
将自己创建的tensor(称作B-tensor)输入到cuda中去,执行代码:B-tensor.cuda()
参考博客:https://blog.csdn.net/yutingzhaomeng/article/details/79084405
4. TypeError: only size-1 arrays can be converted to Python scalars
将int()
转换为.astype(np.int)
参考博客:https://blog.csdn.net/yemayling/article/details/100540408