dorien
dorien

Reputation: 5387

How to fix the dimension error in the loss function/softmax?

I am implementing a logistic regression in PyTorch for XOR (I don't expect it to work well it's just a demonstration). For some reason I am getting an error 'IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)'. It is not clear to me where this originates. The error points to log_softmax during training.

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()))

Upvotes: 1

Views: 550

Answers (1)

inverted_index
inverted_index

Reputation: 2437

First of all, you are doing a binary classification task. So the number of output features should be 2; i.e., num_outputs = 1.

Second, as it's been declared in nn.CrossEntropyLoss() documentation, the .forward method accepts two tensors as below:

  • Input: (N, C) where C is the number of classes (in your case it is 2).
  • Target: (N)

N in the example above is the number of training examples that you pass in to the loss function; for simplicity, you can set it to one (i.e., doing a forward pass for each instance and update gradients thereafter).

Note: Also, you don't need to use .Softmax() before nn.CrossEntropyLoss() module as this class has nn.LogSoftmax included in itself.

I modified your code as below, this is a working example of your snippet:

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()))

Update

To get the predicted class labels which is either 0 or 1:

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

As for the .detach() function, numpy expects the tensor/array to get detached from the computation graph; i.e., the tensor should not have require_grad=True and detach method would do the trick for you.

Upvotes: 1

Related Questions