【发布时间】:2021-03-11 03:31:49
【问题描述】:
我正在尝试使用 Pytorch 计算某个机器学习模型的损失和准确性,但在初始化数据集以便它可以运行时遇到了问题。使用 Moon 数据集,我在运行代码时遇到了一些错误。我首先初始化数据集:
X, y = make_moons(200, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=1, stratify = y)
x, y = Variable (torch.from_numpy(X_train)).float(), Variable(torch.from_numpy(y_train)).float()
然后当我运行神经网络时:
def __init__(self):
super(SoftmaxRegression, self).__init__()
self.fc = nn.Linear(200, 1)
self.softmax = nn.Softmax()
def forward(self, x):
x = self.fc(x)
x = self.softmax(x)
return x
我收到以下错误:
serWarning:softmax 的隐式维度选择已被弃用。更改调用以包含 dim=X 作为参数。
x = F.softmax(self.layer(x))
ret = torch._C._nn.nll_loss(输入,目标,权重,_Reduction.get_enum(reduction),ignore_index)
IndexError:目标 1 超出范围。
我该如何解决这个问题,以便它可以运行数据集并输出损失和准确性?
【问题讨论】:
-
你能提供完整的追溯吗?
-
@null 这是我迄今为止开发的代码
X, y = sklearn.datasets.make_moons(200, noise=0.20) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=1, stratify = y) def __init__(self): super(SoftmaxRegression, self).__init__() self.fc = nn.Linear(500, 1) self.softmax = nn.Softmax() def forward(self, x): x = self.fc(x) x = self.softmax(x) return x每次我尝试运行它时都指出参数至少需要是 1D,但它们是 0D 和 2D。 -
@null 所以我尝试将它们转换为张量,但它仍然不起作用。我正在尝试通过在该数据集上运行来打印出神经网络的损失和准确性。
-
我的意思是完整的“错误”回溯。你能把它放在答案中吗?
-
@null 我再次编辑了这个问题,但这是我得到的错误:错误:serWarning:softmax 的隐式维度选择已被弃用。更改调用以包含 dim=X 作为参数。 x = F.softmax(self.layer(x)) ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index) IndexError: Target 1 is out of bounds.
标签: machine-learning neural-network pytorch