我在将 python 数字列表转换为 pytorch Tensor 时遇到问题:这是我的代码:
caption_feat = [int(x) if x < 11660 else 3 for x in caption_feat]
打印 caption_feat 给出: [1, 9903, 7876, 9971, 2770, 2435, 10441, 9370, 2]
我像这样进行转换: tmp2 = torch.Tensor(caption_feat)
现在打印 tmp2 给出: tensor([1.0000e+00, 9.9030e+03, 7.8760e+03, 9.9710e+03, 2.7700e+03, 2.4350e+03,
1.0441e+04, 9.3700e+03, 2.0000e+00])
但是我希望得到: tensor([1. , 9903, , 9971. ......])
有什么想法吗?
原文由 Ameen Ali 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以通过定义 --- 直接将 python
list
转换为 pytorchTensor
dtype
。例如,