# Cross Entropy Loss not decreasing? 3-category image classification

**URL:** <https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223>\
**Category:** vision\
**Created:** [September 15, 2018, 3:56am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223 "2018-09-15T03:56:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GaoYang-Thu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gaoyang-thu/32/5460_2.png) [@GaoYang-Thu](https://discuss.pytorch.org/u/GaoYang-Thu)\
**Post date:** [September 15, 2018, 3:56am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223/1 "2018-09-15T03:56:30Z")

</div>

Hi,

I wonder why the training loss does not decrease?

The input image batch has the shape of: [batch\_size_1_200\*200]. The true labels are in “one hot” form: [1,0,0], [0,1,0] and [0,0,1].  
I have only 5000 images, so I decided to start from transfer learning.

I loaded pretrained ResNet18 to classify my images into 3 categories.  
I change the input channel of ResNet18 to 1, and final output dimension to 3, in order to suit my classification problem.

So the shape of the model output tensor is [batch\_size \* 3], and the shape of the true label tensor is [batch\_size \* 3].

Since PyTorch does not provide the CrossEntropy loss function between those two tensors, I wrote my own cross entropy loss function based on the equation:

```auto
loss = t.mean(-t.sum(target.float() * t.log(y_prediction),dim=1))

```

Also I am confused about the output of ResNet18: I read somewhere that ResNet18 has softmax layer before output, but the elements of final output did not add up to 1? So I added a softmax layer after ResNet18.

My training code

```auto
import torch as t
import torch.nn.functional as F
device = t.device('cuda:1' if t.cuda.is_available() else 'cpu')
optimizer = optim.SGD(model.parameters(), lr = 0.01, momentum = 0.9)

for images, target in train_dataloader:
    images = images.float().to(device)
    target = target.float().to(device)
    
    optimizer.zero_grad()
    y_prediction = my_resnet18_model(images)
    y_prediction = F.softmax(y_prediction, dim = 1)
    loss = t.mean(-t.sum(target * t.log(y_prediction), dim = 1)
    loss.backward()
    optimizer.step()

    running_loss += loss.item() * images.shape[0]
epoch_loss = running_loss / len(train_dataset)
print('train loss: {}'.format(epoch_loss))

```

The epoch loss stayed around 1.09.

Am I using the loss function right?  
Could I add the softmax after ResNet18?  
How can I improve?

Any comments or suggestions are appreciated. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![TheShadow29](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/theshadow29/32/2013_2.png) [@TheShadow29](https://discuss.pytorch.org/u/TheShadow29)\
**Post date:** [September 15, 2018, 5:43am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223/2 "2018-09-15T05:43:22Z")

</div>

You can just change the target to the label instead of one hot. So 1,0,0 is label 0. 0,1,0 is label 1.

After this you can just use the cross entropy loss in pytorch

---

<div class="post-metadata">

**Author:** ![GaoYang-Thu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gaoyang-thu/32/5460_2.png) [@GaoYang-Thu](https://discuss.pytorch.org/u/GaoYang-Thu)\
**Post date:** [September 17, 2018, 2:45am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223/3 "2018-09-17T02:45:44Z")

</div>

Okay, I decided to do a two categroy classification first with labels “0” and “1”, and see how it goes.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Deeply](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/deeply/32/3840_2.png) [@Deeply](https://discuss.pytorch.org/u/Deeply)\
**Post date:** [September 17, 2018, 1:50pm UTC](https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223/4 "2018-09-17T13:50:37Z")

</div>

> [@GaoYang-Thu](#):
>
> Since PyTorch does not provide the CrossEntropy loss function between those two tensors

Well, why not using the Binary Cross Entropy Loss.

```auto
criterion = criterion = nn.BCEWithLogitsLoss()
...
...
loss = criterion(y_prediction, target)

```

Not sure if I am missing something here?

---

<div class="post-metadata">

**Author:** ![GaoYang-Thu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gaoyang-thu/32/5460_2.png) [@GaoYang-Thu](https://discuss.pytorch.org/u/GaoYang-Thu)\
**Post date:** [September 18, 2018, 2:40am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223/5 "2018-09-18T02:40:50Z")

</div>

I will take a look at BCE. If it takes tensors with right shape, it is worth a try. Thanks!

---

<div class="post-metadata">

**Author:** ![Deeply](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/deeply/32/3840_2.png) [@Deeply](https://discuss.pytorch.org/u/Deeply)\
**Post date:** [September 18, 2018, 11:48am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223/6 "2018-09-18T11:48:49Z")

</div>

It does, as I have been using it for quite some time.  
If it does not work for you, maybe your tensors are not structured correctly.

---

<div class="post-metadata">

**Author:** ![GaoYang-Thu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gaoyang-thu/32/5460_2.png) [@GaoYang-Thu](https://discuss.pytorch.org/u/GaoYang-Thu)\
**Post date:** [September 19, 2018, 8:23am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-not-decreasing-3-category-image-classification/25223/7 "2018-09-19T08:23:05Z")

</div>

```auto
BCEWithLogitLoss()

```

works for my case.  
Thanks again!
