执行torch.nn.functional.softmax()时出现错误,“softmax_lastdim_kernel_impl“ not implemented for ‘Long‘

这个错误通常发生在执行nn.functional.softmax()函数时,输入的数据类型为整数类型(Long)。

softmax()函数要求输入数据类型为浮点数(Float)或双精度浮点数(Double)。

要解决这个错误,需要将输入数据类型转换为浮点数类型。可以使用 tensor.float()tensor.double() 函数来完成类型转换。例如:

import torch.nn.functional as F
import torch

# 创建一个整数类型的tensor
x = torch.LongTensor([[1, 2, 3], [4, 5, 6]])

# 将数据类型转换为float类型
x = x.float()

# 执行softmax函数
y = F.softmax(x, dim=1)

print(y)

输出结果为:

tensor([[0.0900, 0.2447, 0.6652],
        [0.0900, 0.2447, 0.6652]])

猜你喜欢

转载自blog.csdn.net/weixin_43479947/article/details/129419704