RuntimeError(2): element 0 of tensors does not require grad and does not have a grad_fn

pmushidi: <p>Kindly assist to find out why I am still this error message: “RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn”</p>
<p>My section of code I am running is:</p>
<p>model.zero_grad()<br>
log_probs = model(images)<br>
loss = self.criterion(log_probs, labels)</p>
<pre><code>            loss.backward()
            optimizer.step()</code></pre>

ptrblck: <p><code>log_probs</code> might be detached from the computation graph. Check, if it’s <code>.grad_fn</code> attribute is pointing to a valid function or <code>None</code> (detached). In the latter case, print the <code>.grad_fn</code> attribute of the intermediate tensors in your model’s <code>forward</code> method to check which operation detaches the tensor from the computation graph. Often this is done by e.g. rewrapping a tensor via <code>x = torch.tensor(x)</code>, using another library such as <code>numpy</code>, explicitly calling <code>tensor.detach()</code> etc.</p>

Desai_Ashish: <p>Hello everyone,</p>
<p>I’m facing the below mentioned run-time error:</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-98-7b6b8391c42e&gt; in &lt;cell line: 1&gt;()
----&gt; 1 trainer.fit(model, data_module)

29 frames
/usr/local/lib/python3.10/dist-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
    198     # some Python versions print out the first line of a multi-line function
    199     # calls in the traceback and some print out the last line
--&gt; 200     Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
    201         tensors, grad_tensors_, retain_graph, create_graph, inputs,
    202         allow_unreachable=True, accumulate_grad=True)  # Calls into the C++ engine to run the backward pass

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
</code></pre>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/1cQI6fRss140wUF6_7wTsQuus8fBCrIXu?hl=de#scrollTo=MsOKyCEGSqUX">
  <header class="source">
      <img src="https://ssl.gstatic.com/colaboratory-static/common/24c82d731f43c1da2cb6ba70dc20825d/img/favicon.ico" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/1cQI6fRss140wUF6_7wTsQuus8fBCrIXu?hl=de#scrollTo=MsOKyCEGSqUX" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/drive/1cQI6fRss140wUF6_7wTsQuus8fBCrIXu?hl=de#scrollTo=MsOKyCEGSqUX" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I also have mentioned here my Collaboratory, please let me know incase you can not access the file.</p>
<p>Support from you will be appreciated, Thank you:)</p>