nn.CrossEntropyLoss: - optimizer not updating weights

avalon1511: <p>Hello,</p>
<p>I have a very simple prediction network that predicts the labels for a set of pixels. I am calculating the loss as follows:</p>
<pre><code class="lang-auto">m = pixel_classifier()
optimizer = optim.Adam(m.parameters(), lr = 1e-2, betas=(0.9, 0.999))
criterion = nn.CrossEntropyLoss()
</code></pre>
<p>I have run</p>
<pre><code class="lang-auto">for name, params in m.named_parameters():

    if params.requires_grad == True:
        print(name)
</code></pre>
<p>to check if the model params have grad updates enabled and all parameters are being printed.</p>
<p>the training is as follows:</p>
<pre><code class="lang-auto">m.train()
predictions = m(input)
loss = criterion(predictions, true_label)
loss.backward()
</code></pre>
<p>It is at this stage I get the following error:</p>
<pre><code class="lang-auto">element 0 of tensors does not require grad and does not have a grad_fn
</code></pre>
<p>not sure what is happening here. Can anyone help?</p>

blackbirdbarber: <aside class="quote no-group" data-username="avalon1511" data-post="1" data-topic="158620">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/avalon1511/48/45884_2.png" class="avatar"> avalon1511:</div>
<blockquote>
<p><code>element 0 of tensors does not require grad and does not have a grad_fn</code></p>
</blockquote>
</aside>
<p><a href="https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074">Check this  one</a>. Your can check <code>input.requires_grad</code> and if not <code>True</code> this is the problem.</p>
<p>Note: <code>input.requires_grad=True</code> all Tensors computed from <code>input</code> will have <code>requires_grad</code> attribute True. It is like covid-19.</p>
<p>This way you can fool a pretrained network to change  its prediction by modifying the input features.</p>

ptrblck: <p>You are most likely detaching the computation graph in your model somewhere so could you post the model definition, please?</p>
<p><a class="mention" href="/u/blackbirdbarber">@blackbirdbarber</a> you do not need to set <code>input.requires_grad = True</code> and it should not fix this issue.</p>

avalon1511: <p>I actually found the issue, it’s related to the comment of yours I responded to on another post.</p>
<p>Here’s the problem:</p>
<pre><code class="lang-auto">m = pixel_classifier()
pred = m(train_batch)
print(pred.grad_fn)
# &lt;AddmmBackward0 object at 0x7f865771f590&gt;
_, pred = torch.max(pred, dim = 1)
print(pred.grad_fn)
# None
</code></pre>
<p>My loss function is <code>nn.CrossEntropyLoss</code> but my ground truth is just a <code>NxN</code> matrix of labels and not logits - that’s why I was using the second value from <code>torch.max()</code> to make my own <code>NxN</code> matrix of predicted labels to pass to the loss function. But unfortunately this is causing the issue</p>

ptrblck: <p>This makes sense. As explained in the other post, <code>nn.CrossEntropyLoss</code> expects raw logits from the model so don’t use <code>torch.(arg)max</code> on the model outputs and just pass the output of the last layer (without any non-linearity) to the loss function.</p>

avalon1511: <p>Sorry I am confused.</p>
<p>The output from the last layer has shape <code>(N * N x num_classes)</code> but the given ground truth has shape  <code>N*N</code>, moreover the ground truth is labels aka the matrix is populated by the labels of the classes and not logits.</p>
<p>So if I pass the output of the last layer to the loss function with my ground truth that gives an error.</p>
<p>Is that what you meant?</p>

ptrblck: <p><code>nn.CrossEntropyLoss</code> expects a model output containing logits in the shape <code>[batch_size, nb_classes, *]</code> and a target containing class indices in the range <code>[0, nb_classes]</code> in the shape <code>[batch_size, *]</code> as described in the <a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss">docs</a>.</p>