python - PyTorch Softmax 尺寸错误

标签 python machine-learning neural-network pytorch

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

X 为 178x13 Y 为 178x3

我使用的数据集相当简单,可以找到here

我不断收到错误:

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

.

import pandas as pd
import numpy as np
import torch
from torch.autograd import Variable
from sklearn.preprocessing import LabelBinarizer

# Read in dataset, specifying that this set didn't come with column headers
x = pd.read_csv('Datasets/wine.data', header=None)

# Rename columns
x.columns = ['Class', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12', 'A13']

y = x[['Class']].values

#turn class labels into one-hot encoding
one_hot = LabelBinarizer()
y = Variable(torch.from_numpy(one_hot.fit_transform(y)), )

x = Variable(torch.from_numpy(x.iloc[:, 1:14].values).float())


N, D_in, H, D_out = y.shape[0], x.shape[1], 20, 3

# Implement neural net with nn module

model = torch.nn.Sequential(
    torch.nn.Linear(D_in, H),
    torch.nn.ReLU(),
    torch.nn.Linear(H, D_out),
    torch.nn.LogSoftmax(dim=3)
)

loss_fn = torch.nn.NLLLoss

learning_rate = 1e-4
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

for t in range(500):
    y_pred = model(x)

    loss = loss_fn(y_pred, y)
    print("Iteration: %d | Loss: %.3f" % (t, loss))

    optimizer.zero_grad()

    loss.backward()

    optimizer.step()

最佳答案

在我看来,您似乎误解了 LogSoftmax 的参数 dim。从文档来看,

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 跨列求和。

关于python - PyTorch Softmax 尺寸错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48070505/

相关文章:

python - 写值需要多行时如何给变量赋值(python)

python - 如何修复 `tensorflow` 中的准确度函数?

python - 如何使用pickle保存聊天机器人模型

neural-network - 任何监控神经网络训练的pytorch工具?

python - Django fixtures 和 OneToOneField

python - 如何减少Python中比较多个字符串的处理成本?

python - virtualenv pip 不升级

tensorflow - 带 ID 列的训练模型

machine-learning - 参数无效错误预期 begin[0] = 0

python - Keras LSTM 形状不包含序列长度