gpt4 book ai didi

python - PyTorch Softmax 尺寸错误

转载 作者:行者123 更新时间:2023-11-30 08:42:53 25 4
gpt4 key购买 nike

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

X 为 178x13Y 为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com