我在 pytorch 中的预测是 torch([0]) , torch([1])....,torch([25])
分别对应 26 个字母,即 A,B,C....Z
.
我的预测以我想要的火炬([0])的形式出现,依此类推。
知道如何进行这种转换。
最佳答案
要将字母表的索引转换为实际字母,您可以:
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # the Alphabet
pred = torch.randint(0, 26, (30,)) # your prediction, int tensor with values in range[0, 25]
# convert to characters
pred_string = ''.join(alphabet[c_] for c_ in pred)
输出将类似于:'KEFOTIJBNTAPWHSBXUIQKTTJNSCNDF'
这也适用于
pred
使用单个元素,在这种情况下,转换可以更紧凑地完成:alphabet[pred.item()]
关于python - 如何将 torch.Tensor 替换为 python 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68573682/