Getting unmatched arguments in pytorch __Init()__

This is my class for implementing logistic regression and a code to instantiate this class.

 class LogisticRegression(torch.nn.Module):
     def __init__(self, input_dim):
        super(LogisticRegression, self).__init__()
        self.linear = torch.nn.Linear(input_dim, 1)
        #self.linear = torch.nn.Linear(2*input_dim, output_dim)
        #raise NotImplementedError()

    def forward(self, x):
        outputs = self.linear(x)
        outputs = nn.Sigmoid(outputs)
        return outputs


model = LogisticRegression(input_dim)

criterion = torch.nn.CrossEntropyLoss() 

optimizer = torch.optim.SGD(model.parameters(), lr=lr_rate)


output=model(data_X)

I am getting the following error, … I have passed only one argument to model, yet it is saying there is mismatch.

TypeError Traceback (most recent call last)
in ()
11 #print(data_X.shape, data_X.type())
12
—> 13 outputs = model(data_X)
14 labels = labels.squeeze_()
15 loss = criterion(outputs, labels)

1 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
–> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)

in forward(self, x)
17 def forward(self, x):
18 outputs = self.linear(x)
—> 19 outputs = nn.Sigmoid(outputs)
20 return outputs

TypeError: init() takes 1 positional argument but 2 were given

Any help is much appreciated.

nn.Sigmoid creates a module instance, so you would either need to initialize the instance beforehand (e.g. in the __init__) or use the functional API via torch.sigmoid(outputs).

Thank You,@ptrblck, have replaced nn.Sigmopid() with functional like torch.sigmoid()

but got the following error during training…

length of data_X =  128 torch.Size([128, 34])
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:7: UserWarning: To copy 
construct from a tensor, it is recommended to use sourceTensor.clone().detach() or 
sourceTensor.clone().detach().requires_grad_(True), rather than 
torch.tensor(sourceTensor).
  import sys
 /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:11: UserWarning: To copy 
 construct from a tensor, it is recommended to use sourceTensor.clone().detach() or 
 sourceTensor.clone().detach().requires_grad_(True), rather than 
torch.tensor(sourceTensor).
 # This is added back by InteractiveShellApp.init_path()
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-30-1023803e6516> in <module>()
     15         #outputs=model()
     16         labels = labels.squeeze_()
  • –> 17 loss = criterion(outputs, labels)
    18 loss.backward()
    19 optimizer.step()

3 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in call(self, *input,
**kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
–> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in forward(self, input, 
 target)
    914     def forward(self, input, target):
    915         return F.cross_entropy(input, target, weight=self.weight,
--> 916                                ignore_index=self.ignore_index, reduction=self.reduction)
    917 
    918 

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in cross_entropy(input, target, 
weight, size_average, ignore_index, reduce, reduction)
   2007     if size_average is not None or reduce is not None:
   2008         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2009     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, 
reduction)
   2010 
   2011 

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, 
size_average, ignore_index, reduce, reduction)
   1836                          .format(input.size(0), target.size(0)))
   1837     if dim == 2:
-> 1838         ret = torch._C._nn.nll_loss(input, target, weight, 
_Reduction.get_enum(reduction), ignore_index)
   1839     elif dim == 4:
   1840         ret = torch._C._nn.nll_loss2d(input, target, weight, 
_Reduction.get_enum(reduction), ignore_index)

 RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed.  at 
/pytorch/aten/src/THNN/generic/ClassNLLCriterion.c:97

It seems your target tensor is out of the expected range of [0, n_classes-1] and it seems you might be using nn.CrossEntropyLoss.
If that’s the case, note that this loss function expects logits as the model output, so you should remove the sigmoid and just pass the output to the criterion directly.

Thank You @ptrblck , yeah passed outputs directly without applying sigmoid. Yet the error is same.

Could you add a condition to the DataLoader loop and check for the min and max values of your target?
The error points to invalid values outside of [0, nb_classes-1].

Thank You @ptrblck , I have traced the target values they are well within the range…

As it is a binary classification, the values are either 1 or 0

Could you post the shapes of your output and target?
Also, could you add an assert statement to your loop and check the target for values >=0 and <nb_classes?

Thank You @ptrblck , have added assert statement and the error remains the same

outputs shape= torch.Size([128, 1])
target shape = torch.Size([128, 1])

For a binary classification use case, you could use nn.BCEWithLogitsLoss and just keep the shapes.

If you want to use nn.CrossEntropyLoss, your output should contain a neuron for each class ([128, 2]) while the target should contain the class indices with the shape ([128]).

I would recommend to stick to the first approach, since your shapes are already matching. Make sure to pass the target as a FloatTensor via target = target.float().

Thanks a lot @ptrblck , I did use nn.BCEWithLogitsLoss() function and converted labels/target to float() type…

There are no errors , but the result is suffering from overfitting (100% accuracy)

To reduce overfitting, I used PCA and scaled the data ,
changed the shapes and ran the program again… But still I am getting same 100% accuracy !!

Iteration: 100. Loss: 0.8057927637150751.Correct:601. total:601. Accuracy: 100.0.
Iteration: 200. Loss: 0.7555791968181238.Correct:601. total:601. Accuracy: 100.0.
Iteration: 300. Loss: 0.8404111710965548.Correct:601. total:601. Accuracy: 100.0.
Iteration: 400. Loss: 0.7050717906166214.Correct:601. total:601. Accuracy: 100.0.
Iteration: 500. Loss: 0.7241441765733534.Correct:601. total:601. Accuracy: 100.0.
Iteration: 600. Loss: 0.8043209660671171.Correct:601. total:601. Accuracy: 100.0.
Iteration: 700. Loss: 0.6430073846611701.Correct:601. total:601. Accuracy: 100.0.
Iteration: 800. Loss: 0.7107452274836563.Correct:601. total:601. Accuracy: 100.0.
Iteration: 900. Loss: 0.7869175416749621.Correct:601. total:601. Accuracy: 100.0.