Getting unmatched arguments in pytorch __Init()__

Prak_kas: <p>This is my class for implementing logistic regression and a code to instantiate this class.</p>
<pre><code> 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)
</code></pre>
<h2>I am getting the following error, … I have passed only one argument to model, yet it is saying there is mismatch.</h2>
<p>TypeError                                 Traceback (most recent call last)<br>
 in ()<br>
11         <span class="hashtag">#print</span>(data_X.shape, data_X.type())<br>
12<br>
—&gt; 13         outputs = model(data_X)<br>
14         labels = labels.squeeze_()<br>
15         loss = criterion(outputs, labels)</p>
<p>1 frames<br>
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
539             result = self._slow_forward(*input, **kwargs)<br>
540         else:<br>
–&gt; 541             result = self.forward(*input, **kwargs)<br>
542         for hook in self._forward_hooks.values():<br>
543             hook_result = hook(self, input, result)</p>
<p> in forward(self, x)<br>
17     def forward(self, x):<br>
18         outputs = self.linear(x)<br>
—&gt; 19         outputs = nn.Sigmoid(outputs)<br>
20         return outputs</p>
<p>TypeError: <strong>init</strong>() takes 1 positional argument but 2 were given</p>
<p>Any help is much appreciated.</p>

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

Prak_kas: <p>Thank You,<a class="mention" href="/u/ptrblck">@ptrblck</a>,  have replaced  nn.Sigmopid() with functional like torch.sigmoid()</p>
<p>but got the following error during training…</p>
<pre><code>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)
&lt;ipython-input-30-1023803e6516&gt; in &lt;module&gt;()
     15         #outputs=model()
     16         labels = labels.squeeze_()
</code></pre>
<ul>
<li>–&gt; 17         loss = criterion(outputs, labels)<br>
18         loss.backward()<br>
19         optimizer.step()</li>
</ul>
<p>3 frames<br>
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input,<br>
**kwargs)<br>
539             result = self._slow_forward(*input, **kwargs)<br>
540         else:<br>
–&gt; 541             result = self.forward(*input, **kwargs)<br>
542         for hook in self._forward_hooks.values():<br>
543             hook_result = hook(self, input, result)</p>
<pre><code>/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,
--&gt; 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)
-&gt; 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:
-&gt; 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 &gt;= 0 &amp;&amp; cur_target &lt; n_classes' failed.  at 
/pytorch/aten/src/THNN/generic/ClassNLLCriterion.c:97</code></pre>

ptrblck: <p>It seems your target tensor is out of the expected range of <code>[0, n_classes-1]</code> and it seems you might be using <code>nn.CrossEntropyLoss</code>.<br>
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.</p>

Prak_kas: <p>Thank You <a class="mention" href="/u/ptrblck">@ptrblck</a> , yeah passed outputs directly without applying sigmoid. Yet the error is same.</p>

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

Prak_kas: <p>Thank You <a class="mention" href="/u/ptrblck">@ptrblck</a> , I have traced the target values they are well within the range…</p>
<p>As it is a binary classification, the values are either 1 or 0</p>

ptrblck: <p>Could you post the shapes of your output and target?<br>
Also, could you add an assert statement to your loop and check the target for values <code>&gt;=0</code> and <code>&lt;nb_classes</code>?</p>

Prak_kas: <p>Thank You <a class="mention" href="/u/ptrblck">@ptrblck</a> , have added assert statement and the error remains the same</p>
<p>outputs shape=  torch.Size([128, 1])<br>
target shape =  torch.Size([128, 1])</p>

ptrblck: <p>For a binary classification use case, you could use <code>nn.BCEWithLogitsLoss</code> and just keep the shapes.</p>
<p>If you want to use <code>nn.CrossEntropyLoss</code>, your output should contain a neuron for each class (<code>[128, 2]</code>) while the target should contain the class indices with the shape (<code>[128]</code>).</p>
<p>I would recommend to stick to the first approach, since your shapes are already matching. Make sure to pass the target as a <code>FloatTensor</code> via <code>target = target.float()</code>.</p>

Prak_kas: <p>Thanks a lot <a class="mention" href="/u/ptrblck">@ptrblck</a> , I did use nn.BCEWithLogitsLoss() function and  converted labels/target to float() type…</p>
<p>There are no errors , but the result is suffering from overfitting (100% accuracy)</p>
<p>To reduce overfitting, I used PCA and scaled the data ,<br>
changed the shapes and ran the program again… But still I am getting same 100% accuracy !!</p>
<p>Iteration: 100. Loss: 0.8057927637150751.Correct:601. total:601. Accuracy: 100.0.<br>
Iteration: 200. Loss: 0.7555791968181238.Correct:601. total:601. Accuracy: 100.0.<br>
Iteration: 300. Loss: 0.8404111710965548.Correct:601. total:601. Accuracy: 100.0.<br>
Iteration: 400. Loss: 0.7050717906166214.Correct:601. total:601. Accuracy: 100.0.<br>
Iteration: 500. Loss: 0.7241441765733534.Correct:601. total:601. Accuracy: 100.0.<br>
Iteration: 600. Loss: 0.8043209660671171.Correct:601. total:601. Accuracy: 100.0.<br>
Iteration: 700. Loss: 0.6430073846611701.Correct:601. total:601. Accuracy: 100.0.<br>
Iteration: 800. Loss: 0.7107452274836563.Correct:601. total:601. Accuracy: 100.0.<br>
Iteration: 900. Loss: 0.7869175416749621.Correct:601. total:601. Accuracy: 100.0.</p>