我正在尝试将字符串列表转换为张量,但出现此错误
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]
答案 0 :(得分:1)
您可以使用python的ord
将字符转换为其unicode:
targets[i, :end] = torch.from_numpy(np.array(list(map(ord, cap[:end])))).to(torch.long)