【问题标题】:TypeError: can't assign a str to a torch.LongTensor pytochTypeError:无法将字符串分配给torch.LongTensor pytorch
【发布时间】:2019-10-20 12:23:18
【问题描述】:

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

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]

【问题讨论】:

标签: pytorch torch


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多