Building pytorch binary classification model

SungmanHong: <p>Hi, I’m building pytorh binary classification model(eg : cat vs dog)</p>
<p>My model’s output is</p>
<p>[[0.4820, 0.5180]] and my lable is [1,0] for example.<br>
my loss is <code>criterion = nn.CrossEntropyLoss()</code></p>
<pre><code class="lang-auto">loss = criterion(outputs, true_value)
#loss = criterion([[0.4820, 0.5180]] , [1,0])
</code></pre>
<p>I’m expecting that, the lable is [1,0] than… output shold be [[0.99, 0.01]] like that…</p>
<p>BUT, there is many error or no loss is downgoing…<br>
The datashape of lable and output is not right? please help!!<br>
could you give me the correct shape of lable and outputs?<br>
my shape of outputs is 1x2 and my label’s shape is 2 and batchsize is 1 in my code.<br>
(I want to find pytorch binary classification guide at homepage but I failed T.T)</p>
<p>log:<br>
I got this error.</p>
<pre><code class="lang-auto">
    loss = criterion(outputs, true_value)
  File "/home/hsm/anaconda3/envs/reproducibleresearch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/hsm/anaconda3/envs/reproducibleresearch/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 1048, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/hsm/anaconda3/envs/reproducibleresearch/lib/python3.6/site-packages/torch/nn/functional.py", line 2690, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/home/hsm/anaconda3/envs/reproducibleresearch/lib/python3.6/site-packages/torch/nn/functional.py", line 2382, in nll_loss
    "Expected input batch_size ({}) to match target batch_size ({}).".format(input.size(0), target.size(0))
ValueError: Expected input batch_size (1) to match target batch_size (2).
</code></pre>

larcane: <p>outputs and true_value must be same batch_size.<br>
[[0.4820, 0.5180]]'s shape is [1,2](batch_size,num_labels), and true_value’s shape is [2](num_labels).<br>
therefore you must change true_value’s shape to [1,2](batch_size,num_labels)!</p>

thecho7: <p>Adding more info to <a class="mention" href="/u/larcane">@larcane</a> 's explanation,</p>
<ol>
<li>
<code>true_value</code> should be a LongTensor which means the type of elements inside the tensor is long.</li>
<li>Your <code>true_value</code> should have only one value. In this case, [0] or [1].<br>
Please make it clear whether your output should be updated into [0, 1] or [1, 0]</li>
</ol>

SungmanHong: <p>Thank you, larcane and thecho7!</p>
<p>I finally succeed to train the model! thanks.<br>
The correct shape of outputs and label(when batchsize = 1)<br>
is [1x2] and [1x1]. (when using CrossEntropyLoss)</p>
<p>And here is my train code for those who may need(binary classification) <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code class="lang-auto">
criterion = nn.CrossEntropyLoss()

        for idx, (raw_data,  true_value) in enumerate(train_module):
                raw_data = raw_data.to(device).float()
                true_value= true_value.to(device)
                true_value= true_value.reshape(1)
                optimizer.zero_grad()
                outputs = model(raw_data)
                loss = criterion(outputs.float(), true_value.long())
                loss.backward()
                optimizer.step()
</code></pre>