Output dimensions for Logistic regression

username: Prak_kas
cooked: <p>I am trying to build a logistic regression using Pytorch  framework…<br>
I want Binary logistic regression with “m” parameters.</p>
<p>Hence I thought dimension of network would be [m,2] ( 2 for two binary c;asses)<br>
But it is giving  2*m parameters or weights.</p>
<p>When i changed the dimension to [m,1] , I am getting errors while running the learning program.as follows:</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
26         outputs = model(data_X)<br>
27         labels = labels.squeeze_()<br>
—&gt; 28         loss = criterion(outputs, labels)<br>
29         loss.backward()<br>
30         optimizer.step()</p>
<p>3 frames<br>
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
545             result = self._slow_forward(*input, **kwargs)<br>
546         else:<br>
–&gt; 547             result = self.forward(*input, **kwargs)<br>
548         for hook in self._forward_hooks.values():<br>
549             hook_result = hook(self, input, result)</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)<br>
914     def forward(self, input, target):<br>
915         return F.cross_entropy(input, target, weight=self.weight,<br>
–&gt; 916                                ignore_index=self.ignore_index, reduction=self.reduction)<br>
917<br>
918</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
1993     if size_average is not None or reduce is not None:<br>
1994         reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
-&gt; 1995     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)<br>
1996<br>
1997</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
1822                          .format(input.size(0), target.size(0)))<br>
1823     if dim == 2:<br>
-&gt; 1824         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
1825     elif dim == 4:<br>
1826         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)</p>
<p>RuntimeError: Assertion `cur_target &gt;= 0 &amp;&amp; cur_target &lt; n_classes’ failed.  at /pytorch/aten/src/THNN/generic/ClassNLLCriterion.c:94</p>

username: albanD
cooked: <p>When using neural networks for classification, you usually output as many outputs as there are classes and use a softmax-like layer to get all values between 0 and 1 to then use for CrossEntropy-like loss functions.</p>
<p>Indeed cross entropy expects as many weights as they are classes.</p>
<p>If you want to do regression to a single value, then you might want to use a different loss function.</p>

username: Prak_kas
cooked: <p>Thank You Sir,<br>
But I am building a model  Logistic regression with “m” parameters using pytorch “nn” module…</p>
<p>if we use output_dim=2, we are getting 2*m weights which is not required.</p>
<p>If I changed output_dim=1, I am getting an error…<br>
Is there any other way to develop logistic regression model in pytorch  without nn module?</p>

username: albanD
cooked: <p>A quick google search led to <a href="https://towardsdatascience.com/logistic-regression-on-mnist-with-pytorch-b048327f8d19" rel="nofollow noopener">this</a> for example. But there are many people doing logistic regression with pytorch. I’m sure you can find an example that suits your needs.</p>
<p>And as I mentionned above, the problem you see is because in the first case you do classification with negative log likelihood loss and so 2*m weights is expected.<br>
In the second case, you change the number of weights with the same loss so this is expected not to work.</p>