算交叉熵lossFunction报错“1D target tensor expected, multi-target not supported”的解决办法

用pytorch完成字符识别分类任务时,发现loss = lossFunction(out, labels)报错
同样的代码在MNIST数据集上就没有报错,原因是数据载入类型不符合规范

输入labels维度应该为1维,且精度不能是Double,必须换成long

修改后的数据导入代码:

dataset = pandas.read_csv('letter-recognition.data', header=None)
data = np.array(dataset)
# 划分训练集和测试集
X_train = data[:16000, 1:]   # [16000,16] 取前16000个数据
X_train_label = data[:16000, 0:1]  # [16000,1] 取标签
for lb in range(len(X_train_label)):  # 字符变数字
    X_train_label[lb, 0] = ord(X_train_label[lb, 0]) - ord('A')
X_train_label = X_train_label.reshape(16000,)  # 修改成一维,不然会报错
X_train = torch.from_numpy(X_train.astype(float))  # 变成tensor类型
X_train_label = torch.from_numpy(X_train_label.astype(float))

在计算交叉熵时的代码:

lossFunction = torch.nn.CrossEntropyLoss()
loss = lossFunction(out, labels.long())  # 修改数据精度

你可能感兴趣的:(算交叉熵lossFunction报错“1D target tensor expected, multi-target not supported”的解决办法)