# Loss dropping to zero in two epochs

**URL:** <https://discuss.pytorch.org/t/loss-dropping-to-zero-in-two-epochs/70636>\
**Category:** vision\
**Created:** [February 22, 2020, 12:13am UTC](https://discuss.pytorch.org/t/loss-dropping-to-zero-in-two-epochs/70636 "2020-02-22T00:13:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PCPJ](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@PCPJ](https://discuss.pytorch.org/u/PCPJ)\
**Post date:** [February 22, 2020, 12:13am UTC](https://discuss.pytorch.org/t/loss-dropping-to-zero-in-two-epochs/70636/1 "2020-02-22T00:13:59Z")

</div>

Hello, everyone. I am new to pytorch, so sorry if something sounds stupid.  
My model consists of a vgg16 that outputs a 64x64 grid of boolean predictions.  
I have built my own custom loss functions, but I think that I am doing something really wrong.  
In two epochs the loss is dropping to zero, but when I try to test the model, with the exact same imagens used on training, I got results really weird.  
For all images the result was a bunch of zeros and a one in the first position:  
[1, 0, 0, 0, …0]  
[0, 0, 0, 0, …0]  
…  
…  
…  
[0, 0, 0, 0, …0]

I will post here the loss function that I built. If someone could give me a hint on what I am doing wrong, I would be really grateful. If my question/explanation was confused/incomplete, please tell me, so I can try to explain better.

```auto
        true_scores = torch.zeros((batch_size, 1, self.grid_size, self.grid_size)).to(device) # (N, 1, 64, 64)
        # For each image
        for i in range(batch_size):
            for row in targets[i]:
                cells = torch.floor(row/self.cell_size).type(torch.long) # (n_points, 2)
                cells = cells[:, 1]*self.grid_size + cells[:, 0] # (n_points)
                true_scores[i][0].view(self.grid_size**2)[cells] = 1.0
        score_loss = self.logist_bce(pred, true_scores) 
        return score_loss

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [February 22, 2020, 7:26am UTC](https://discuss.pytorch.org/t/loss-dropping-to-zero-in-two-epochs/70636/2 "2020-02-22T07:26:57Z")

</div>

How did you implement `logist_bce`?  
Could you post a small code snippet with a random input and target tensor, so that we could have a better look at the use methods? 🙂

---

<div class="post-metadata">

**Author:** ![PCPJ](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@PCPJ](https://discuss.pytorch.org/u/PCPJ)\
**Post date:** [February 22, 2020, 3:17pm UTC](https://discuss.pytorch.org/t/loss-dropping-to-zero-in-two-epochs/70636/3 "2020-02-22T15:17:31Z")

</div>

Hello, @ptrblck. Thanks for the reply!  
Logist\_bce is the nn.BCEWithLogitsLoss from PyTorch. I will post the code from my custom loss function here, it is just a few lines.

I wrote a code simulation of my loss with random input. For better visualization, I used a grid size of 4. So the input of the loss should be a tensor of size (batch\_size, 1, 4, 4), and a list of list of tensors with (x,y) coordinates.

```auto
x = torch.randn(1,1,4,4)
loss = MyLoss(4)
y = list()
rows = list()
r1 = torch.Tensor([[0.1, 0.1], [0.3, 0.3]]) # tensor with two coordinates normalized to 0-1, (0.1, 0.1) and (0.3, 0.3)
r2 = torch.Tensor([[0.6, 0.6], [0.9, 0.9]]) # tensor with two coordinates normalized to 0-1, (0.6, 0.6) and (0.9, 0.9)
rows.append(r1)
rows.append(r2)
y.append(rows)
x
tensor([[[[0.1691, -1.2786, 1.3548, -0.3432],
          [0.8771, -0.8598, -1.0828, -0.7924],
          [0.1644, 1.0497, -0.4348, -1.5413],
          [0.7028, -0.6409, 0.5056, -1.9122]]]])
>>> y
[[tensor([[0.1000, 0.1000],
        [0.3000, 0.3000]]), tensor([[0.6000, 0.6000],
        [0.9000, 0.9000]])]]
>>> loss(x,y)
true_scores =
 tensor([[[[1., 0., 0., 0.],
          [0., 1., 0., 0.],
          [0., 0., 1., 0.],
          [0., 0., 0., 1.]]]])
tensor(2.3711)

```

```auto
class MyLoss(nn.Module):
    def __init__ (self, grid_size=64):
        super(CropRowLoss, self). __init__ ()
        # grid_size => The size of the grid, in cells

        self.grid_size = grid_size
        self.cell_size = 1.0/self.grid_size

        self.logist_bce = nn.BCEWithLogitsLoss(reduction='mean', pos_weight=torch.FloatTensor([6.0]).to(device))

    def forward(self, pred, targets):
        """
        Forward propagation.
        :param pred: a tensor of dimensions (N, 1, grid_size, grid_size)
        :param targets: list of list of tensor (n_points, 2)
        """
        batch_size = pred.size(0)

        true_scores = torch.zeros((batch_size, 1, self.grid_size, self.grid_size)).to(device) # (N, 1, 64, 64)

        # For each image
        for i in range(batch_size):
            for row in targets[i]:
                cells = torch.floor(row/self.cell_size).type(torch.long) # (n_points, 2)
                cells = cells[:, 1]*self.grid_size + cells[:, 0] # (n_points)
                true_scores[i][0].view(self.grid_size**2)[cells] = 1.0
        print("true_scores = \n", true_scores)
        score_loss = self.logist_bce(pred, true_scores) # Scalar
        return score_loss

```
