异常应为标量类型Float的对象,但参数#2“other”的标量类型为Long

2024-05-29 02:07:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在运行以下代码时遇到此错误

Expected object of scalar type Float but got scalar type Long for argument #2 ‘other’

    def encode(words):
        max_l = max([len(i) for i in words])
        result = torch.empty((1,max_l, 26)).int()
        for word in words:
            ints = (np.fromstring(word,dtype=np.uint8)-ord('a'))
            addition = np.zeros((max_l - ints.shape[0],)) -1
            tr = torch.Tensor(np.expand_dims(np.hstack((ints,addition)),-1))
            tr = (tr[:] == torch.arange(26)).int()
            tr = torch.unsqueeze(tr, 0)
            result = torch.cat((result,tr))
        result = result[1::]
        return result

想知道如何修复它吗?不幸的是,我没有完整的日志堆栈


Tags: 代码infortypenptorchresulttr

热门问题