Loss dropping to zero in two epochs

PCPJ: <p>Hello, everyone. I am new to pytorch, so sorry if something sounds stupid.<br>
My model consists of a vgg16 that outputs a 64x64 grid of boolean predictions.<br>
I have built my own custom loss functions, but I think that I am doing something really wrong.<br>
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.<br>
For all images the result was a bunch of zeros and a one in the first position:<br>
[1, 0, 0, 0, …0]<br>
[0, 0, 0, 0, …0]<br>
…<br>
…<br>
…<br>
[0, 0, 0, 0, …0]</p>
<p>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.</p>
<pre><code class="lang-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
</code></pre>

ptrblck: <p>How did you implement <code>logist_bce</code>?<br>
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? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

PCPJ: <p>Hello, <a class="mention" href="/u/ptrblck">@ptrblck</a>. Thanks for the reply!<br>
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.</p>
<p>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.</p>
<pre><code class="lang-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]]]])
&gt;&gt;&gt; y
[[tensor([[0.1000, 0.1000],
        [0.3000, 0.3000]]), tensor([[0.6000, 0.6000],
        [0.9000, 0.9000]])]]
&gt;&gt;&gt; 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)

</code></pre>
<pre><code class="lang-auto">class MyLoss(nn.Module):
    def __init__(self, grid_size=64):
        super(CropRowLoss, self).__init__()
        #  grid_size =&gt; 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
</code></pre>