2017-11-09 3 views
1
a=[1,2,3]; 
context_var = autograd.Variable(torch.LongTensor(a)) 

にこれは私がこれを乗り越えるためにどのように把握することはできませんよ、エラーPytorchテンソルヘルプはLongTensor

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

を与えています。私にとって

答えて

3

最近のバージョンのpytorchでは、あなたのコードは正常に動作します。しかし、古いバージョンの場合は、.tolist()メソッドを使用してnumpy配列をリストに変換して、次のようにエラーを取り除くことができます。

a=[1,2,3]; 
context_var = autograd.Variable(torch.LongTensor(a.tolist())) 
0

作品罰金:

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、トーチ0.2.0.post3、およびnumpyの1.13.3使用してい

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

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