PyTorch的Softmax维度

2024-04-23 18:26:58 发布

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

我试图写一个简单的NN模块,有2层,第一层ReLU激活,输出softmax与3个类(一个热编码)。我使用softmax函数的方式似乎有问题,但我不确定发生了什么。在

X是178x13 Y是178x3

我使用的数据集相当简单,可以找到datasets/Wine" rel="nofollow noreferrer">here。在

我一直收到错误:

RuntimeError: dimension out of range (expected to be in range of [-2, 1], but got 3) . 

一。在

^{pr2}$

Tags: 模块of数据函数编码方式rangenn
2条回答

这是个问题,因为对于nlloss:

The target that this loss expects is a class index (0 to N-1, where N = number of classes)

我试着给它一个热编码向量。我通过以下方式解决了我的问题:

^{pr2}$

在哪里torch.max.最大找到了最大值及其各自的指数。在

在我看来你误解了LogSoftmaxdim的论点。从文件上看

dim (int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).

现在,当你把你的输入通过你的两个线性层之后,你得到的张量是LogSoftmax的维数是178 x 3。显然,dim = 3不可用,因为张量只有两个维度。相反,请尝试dim=1跨列求和。在

相关问题 更多 >