如何修复损失函数/softmax中的尺寸误差?

2024-06-17 11:52:18 发布

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

我正在Pytork for XOR中实现逻辑回归(我不希望它工作得很好,它只是一个演示)。由于某些原因,我得到一个错误“IndexError:维度超出范围(预期范围为[-1,0],但得到1)”。我不清楚这是从哪里来的。培训期间,错误指向log_softmax

import torch.nn as nn
import torch.nn.functional as F

class LogisticRegression(nn.Module):
  # input_size: Dimensionality of input feature vector.
  # num_classes: The number of classes in the classification problem.
  def __init__(self, input_size, num_classes):
    # Always call the superclass (nn.Module) constructor first!
    super(LogisticRegression, self).__init__()
    # Set up the linear transform
    self.linear = nn.Linear(input_size, num_classes)

  # Forward's sole argument is the input.
  # input is of shape (batch_size, input_size)
  def forward(self, x):
    # Apply the linear transform.
    # out is of shape (batch_size, num_classes)
    out = self.linear(x)
    # Softmax the out tensor to get a log-probability distribution
    # over classes for each example.
    out_distribution = F.softmax(out, dim=-1)
    return out_distribution


# Binary classifiation
num_outputs = 1
num_input_features = 2

# Create the logistic regression model
logreg_clf = LogisticRegression(num_input_features, num_outputs)

print(logreg_clf)


lr_rate = 0.001

X = torch.Tensor([[0,0],[0,1], [1,0], [1,1]])
Y = torch.Tensor([0,1,1,0]).view(-1,1) #view is similar to numpy.reshape()

# Run the forward pass of the logistic regression model
sample_output = logreg_clf(X) #completely random at the moment
print(X)

loss_function = nn.CrossEntropyLoss() # computes softmax and then the cross entropy
optimizer = torch.optim.SGD(logreg_clf.parameters(), lr=lr_rate)



from torch.autograd import Variable
#training loop: 

epochs = 201 #how many times we go through the training set
steps = X.size(0) #steps = 4; we have 4 training examples

for i in range(epochs):
    for j in range(steps):
        #sample from the training set: 
        data_point = np.random.randint(X.size(0))
        x_var = Variable(X[data_point], requires_grad=False)
        y_var = Variable(Y[data_point], requires_grad=False)

        optimizer.zero_grad() # zero the gradient buffers
        y_hat = logreg_clf(x_var) #get the output from the model
        loss = loss_function.forward(y_hat, y_var) #calculate the loss
        loss.backward() #backprop
        optimizer.step() #does the update

    if i % 500 == 0:
        print ("Epoch: {0}, Loss: {1}, ".format(i, loss.data.numpy()))

Tags: oftheselfforinputsizenntorch
1条回答
网友
1楼 · 发布于 2024-06-17 11:52:18

首先,您正在执行一项二进制分类任务。因此,输出特征的数量应为2;i、 例如,num_outputs = 1

其次,正如在^{}文档中声明的那样,.forward方法接受两个张量,如下所示:

  • Input: (N, C)其中C是类的数量(在您的例子中是2)
  • Target: (N)

上面示例中的N是传递给loss函数的训练示例数;为简单起见,您可以将其设置为1(即,对每个实例进行正向传递,然后更新渐变)

注意:另外,您不需要在nn.CrossEntropyLoss()模块之前使用.Softmax(),因为这个类本身包含了nn.LogSoftmax

我对您的代码进行了如下修改,这是您代码片段的一个工作示例:

import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torch

class LogisticRegression(nn.Module):
  # input_size: Dimensionality of input feature vector.
  # num_classes: The number of classes in the classification problem.
  def __init__(self, input_size, num_classes):
    # Always call the superclass (nn.Module) constructor first!
    super(LogisticRegression, self).__init__()
    # Set up the linear transform
    self.linear = nn.Linear(input_size, num_classes)

  # Forward's sole argument is the input.
  # input is of shape (batch_size, input_size)
  def forward(self, x):
    # Apply the linear transform.
    # out is of shape (batch_size, num_classes)
    out = self.linear(x)
    # Softmax the out tensor to get a log-probability distribution
    # over classes for each example.
    return out


# Binary classifiation
num_outputs = 2
num_input_features = 2

# Create the logistic regression model
logreg_clf = LogisticRegression(num_input_features, num_outputs)

print(logreg_clf)


lr_rate = 0.001

X = torch.Tensor([[0,0],[0,1], [1,0], [1,1]])
Y = torch.Tensor([0,1,1,0]).view(-1,1) #view is similar to numpy.reshape()

# Run the forward pass of the logistic regression model
sample_output = logreg_clf(X) #completely random at the moment
print(X)

loss_function = nn.CrossEntropyLoss() # computes softmax and then the cross entropy
optimizer = torch.optim.SGD(logreg_clf.parameters(), lr=lr_rate)



from torch.autograd import Variable
#training loop:

epochs = 201 #how many times we go through the training set
steps = X.size(0) #steps = 4; we have 4 training examples

for i in range(epochs):
    for j in range(steps):
        #sample from the training set:
        data_point = np.random.randint(X.size(0))
        x_var = Variable(X[data_point], requires_grad=False).unsqueeze(0)
        y_var = Variable(Y[data_point], requires_grad=False).long()

        optimizer.zero_grad() # zero the gradient buffers
        y_hat = logreg_clf(x_var) #get the output from the model
        loss = loss_function(y_hat, y_var) #calculate the loss
        loss.backward() #backprop
        optimizer.step() #does the update

    if i % 500 == 0:
        print ("Epoch: {0}, Loss: {1}, ".format(i, loss.data.numpy()))

更新

要获取预测的类标签(0或1),请执行以下操作:

pred = np.argmax(y_hat.detach().numpy, axis=0)

至于.detach()函数,numpy希望张量/数组与计算图分离;i、 例如,张量不应该有require_grad=True,而detach方法可以为您实现这一点

相关问题 更多 >