Building pytorch binary classification model

Hi, I’m building pytorh binary classification model(eg : cat vs dog)

My model’s output is

[[0.4820, 0.5180]] and my lable is [1,0] for example.
my loss is criterion = nn.CrossEntropyLoss()

loss = criterion(outputs, true_value)
#loss = criterion([[0.4820, 0.5180]] , [1,0])

I’m expecting that, the lable is [1,0] than… output shold be [[0.99, 0.01]] like that…

BUT, there is many error or no loss is downgoing…
The datashape of lable and output is not right? please help!!
could you give me the correct shape of lable and outputs?
my shape of outputs is 1x2 and my label’s shape is 2 and batchsize is 1 in my code.
(I want to find pytorch binary classification guide at homepage but I failed T.T)

log:
I got this error.


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

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

1 Like

Adding more info to @larcane 's explanation,

  1. true_value should be a LongTensor which means the type of elements inside the tensor is long.
  2. Your true_value should have only one value. In this case, [0] or [1].
    Please make it clear whether your output should be updated into [0, 1] or [1, 0]
1 Like

Thank you, larcane and thecho7!

I finally succeed to train the model! thanks.
The correct shape of outputs and label(when batchsize = 1)
is [1x2] and [1x1]. (when using CrossEntropyLoss)

And here is my train code for those who may need(binary classification) :slight_smile:


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