2017-11-09 4 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에서 완벽하게 작동합니다. 그러나 이전 버전의 경우 numpy 배열을 다음과 같이 .tolist() 메서드를 사용하여 목록으로 변환하여 오류를 제거 할 수 있습니다.

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))) 

출력 : 내가 파이썬 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] 

.