定义torch.cuda.LongTensor而不是torch.LongTensor

时间:2017-10-11 17:25:14

标签: python pytorch

当我运行以下代码时,变量类型变为torch.LongTensor。我怎样才能创建一个torch.cuda.LongTensor呢?

# Turn string into list of longs
def char_tensor(string):
   tensor = torch.zeros(len(string)).long()
   for c in range(len(string)):          
      tensor[c] = all_characters.index(string[c])
   return Variable(tensor)

print(char_tensor('abcDEF'))

输出:

Variable containing:
 10
 11
 12
 39
 40
 41
[torch.LongTensor of size 6]

1 个答案:

答案 0 :(得分:0)

正确答案:

# Turn string into list of longs
def char_tensor(string):
   tensor = torch.zeros(len(string)).long()
   for c in range(len(string)):          
      tensor[c] = all_characters.index(string[c])
   return Variable(tensor).cuda()

print(char_tensor('abcDEF'))