长传感器中的Pytorch张量帮助

长传感器中的Pytorch张量帮助,pytorch,tensor,Pytorch,Tensor,这是一个错误 a=[1,2,3]; context_var = autograd.Variable(torch.LongTensor(a)) 我想不出如何克服这一点 对我来说很好: RuntimeError: tried to construct a tensor from a int sequence, but found an item of type numpy.int32 at index 产出: a=[1,2,3] print(torch.autograd.Variable(tor

这是一个错误

a=[1,2,3];
context_var = autograd.Variable(torch.LongTensor(a))
我想不出如何克服这一点

对我来说很好:

RuntimeError: tried to construct a tensor from a int sequence, but found an item of type numpy.int32 at index
产出:

a=[1,2,3]
print(torch.autograd.Variable(torch.LongTensor(a)))
b = np.array(a)
print(torch.autograd.Variable(torch.LongTensor(b)))

我使用的是Python 3.6.2、torch 0.2.0.post3和numpy 1.13.3。

您的代码在pytorch的最新版本中运行得非常好。但对于较旧的版本,可以使用
.tolist()
方法将
numpy
数组转换为list,如下所示以消除错误

Variable containing:
 1
 2
 3
[torch.LongTensor of size 3]

Variable containing:
 1
 2
 3
[torch.LongTensor of size 3]
a=[1,2,3];
context_var = autograd.Variable(torch.LongTensor(a.tolist()))