Pytorch Tensor 帮助 LongTensor
Pytorch Tensor help in LongTensor
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)))
输出:
Variable containing:
1
2
3
[torch.LongTensor of size 3]
Variable containing:
1
2
3
[torch.LongTensor of size 3]
我正在使用 Python 3.6.2、torch 0.2.0.post3 和 numpy 1.13.3。
您的代码在最新版本的 pytorch 中运行良好。但是对于旧版本,您可以使用 .tolist()
方法将 numpy
数组转换为列表,如下所示以消除错误。
a=[1,2,3];
context_var = autograd.Variable(torch.LongTensor(a.tolist()))
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)))
输出:
Variable containing:
1
2
3
[torch.LongTensor of size 3]
Variable containing:
1
2
3
[torch.LongTensor of size 3]
我正在使用 Python 3.6.2、torch 0.2.0.post3 和 numpy 1.13.3。
您的代码在最新版本的 pytorch 中运行良好。但是对于旧版本,您可以使用 .tolist()
方法将 numpy
数组转换为列表,如下所示以消除错误。
a=[1,2,3];
context_var = autograd.Variable(torch.LongTensor(a.tolist()))