总结:简单的BP网络出现报错。原因是y_train的类型不正确。更改适当的数据形式即可。
出现问题的代码:
loss_func = torch.nn.CrossEntropyLoss()
loss = loss_func(y_pred,y_train)
解决方式:
loss_func = torch.nn.CrossEntropyLoss()
loss = loss_func(y_pred,y_train.type(torch.LongTensor)) #预测值和真实值对比
Ref:
https://www.pythonfixing.com/2021/12/fixed-runtimeerror-expected-scalar-type.html