TypeError:无法为火炬分配str.LongTensor pytoch

时间:2019-06-05 00:33:41

标签: pytorch torch

我正在尝试将字符串列表转换为张量,但出现此错误

lengths = [len(cap) for cap in captions]
    targets = torch.zeros(len(captions), max(lengths)).long()
    for i, cap in enumerate(captions):
        end = lengths[i]
        targets[i, :end] = cap[:end]

1 个答案:

答案 0 :(得分:1)

您可以使用python的ord将字符转换为其unicode:

targets[i, :end] = torch.from_numpy(np.array(list(map(ord, cap[:end])))).to(torch.long)