When .grad is none?

username: cosmmb
cooked: <p>Hi there,</p>
<p>I just have a quick question, assume I have a variable x and its type is :<br>
type(x)<br>
&lt;class ‘torch.autograd.variable.Variable’&gt;</p>
<p>When the x.grad is none and when the x.grad is not none?</p>
<p>Thanks!</p>

username: albanD
cooked: <p>Hi,</p>
<p>x.grad is None when you create the Variable.<br>
It won’t be None if you specified <code>requires_grad=True</code> when creating it and you backpropagated some gradients up to that Variable.</p>

username: cosmmb
cooked: <p>Hi,</p>
<p>Thanks for the reply.</p>
<p>For example, in my case, I have:<br>
total_rl_loss.creator<br>
&lt;torch.autograd._functions.basic_ops.Add object at 0x1195af138&gt;<br>
total_rl_loss.requires_grad<br>
True<br>
but total_rl_loss.grad is still none, do you know why?</p>
<p>Thanks!</p>

username: adamvest
cooked: <p>Pretty sure a variable’s .grad member is only non-None after backproping some gradient to it. I.e., you need call .backward() in order for the variables in a given subgraph to have non-None .grad members</p>

username: cosmmb
cooked: <p>I total agree with you. However, I found there is one interesting usage here:</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/OpenNMT/OpenNMT-py/blob/master/train.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/OpenNMT/OpenNMT-py/blob/master/train.py" target="_blank" rel="nofollow noopener">OpenNMT/OpenNMT-py/blob/master/train.py</a></h4>
<pre><code class="lang-py">#!/usr/bin/env python

from __future__ import division

import argparse
import glob
import os
import sys
import random

import torch
import torch.nn as nn
from torch import cuda

import onmt
import onmt.io
import onmt.Models
import onmt.ModelConstructor
import onmt.modules
from onmt.Utils import use_gpu
</code></pre>

  This file has been truncated. <a href="https://github.com/OpenNMT/OpenNMT-py/blob/master/train.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>in line 175, there is no .backward() has been called yet, but the outputs.grad is not None.</p>

username: adamvest
cooked: <p>.backward() is called in line 173 of that file whenever the model is not in evaluation mode, ie it’s training. If .backward() is called, outputs.grad will be non-None as gradient is backpropogated to the variable. If the model is in evaluation mode, then .backward() is not called and None is returned (in this case outputs.grad would be None as well).</p>

username: cosmmb
cooked: <p>I see, Thanks for the explanations.</p>

username: Santosh_Manicka
cooked: <p>Here’s another scenario where a variable that requires a gradient has no gradients even after backward().</p>
<pre><code class="lang-auto">x = Variable(torch.ones(2, 2), requires_grad=True)
y = x + 2
y.retain_grad()
out1 = x.mean()
out1.backward()
print(y.grad,y.requires_grad)
out2 = y.mean()
out2.backward()
print(x.grad,x.requires_grad)
</code></pre>
<p>Outputs are:</p>
<pre><code class="lang-auto">None True
tensor([[0.5000, 0.5000],
        [0.5000, 0.5000]]) True
</code></pre>
<p>The reason is simple: x -&gt; y but not y -&gt; x, therefore dx/dy=0 (or None) but dy/dx is non-zero.</p>

username: someAdjectiveNoun
cooked: <p>I get <code>param.grad = None</code> even after defining <code>requires_grad=True</code>. I checked and the variable does exist in the <code>list(model.parameters()[0])</code>. Have you encountered such an issue?</p>

username: albanD
cooked: <p>Hi,</p>
<p>You will need to be a bit more precise here.<br>
Maybe open a new thread with a code sample showing what you’re trying to do <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Also note that setting requires_grad must be done before performing operations on the Tensor!</p>

username: someAdjectiveNoun
cooked: <p>Yes, I will open a new thread for it <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>