Torch.no_grad()

lakehanne: <p>In the past, we were able to separate a variable from the current state of a network by doing:</p>
<p>states = Variable(torch.Tensor(width, height), volatile=True)</p>
<p>It seems there is an API change to <code>with torch.no_grad()</code> but I could not find the documentation on docs pages.</p>
<p>It would seem that this is the correct way not to calculate gradients on a variable going forward:</p>
<pre><code class="lang-python">        &gt;&gt;&gt; x = Variable(torch.Tensor([34, 54]), requires_grad=True)
        &gt;&gt;&gt; with torch.no_grad():
                    y = x * 2
</code></pre>
<p>Am I correct?</p>

SimonW: <p>Yes, the reason that it is not on the doc yet is that it is not in any official release. We will make sure to document such things properly before a new release.</p>

lakehanne: <p>It got pulled when I compiled a docker image this evening.</p>
<p>People can already see it. The earlier the documentation is updated, the better.</p>

SimonW: <p>We should definitely update it soon. Thanks for reminding us. I will ask people who are more familiar with the change to write the doc.</p>
<p>That said, you are compiling from github master, i.e., not a release. Although we are usually pretty good at keeping the doc in sync with master, there can sometimes be some delays.</p>

harsv: <p>I faced same issue, not only it gives warning it also consumes much more memory during validation-- because it computes grads unnecessarily. However, with little code change as shown in the post, it fixes both issues.</p>

Brando_Miranda: <p>for what version is this out? It seems that 0.3.1 yields errors:</p>
<pre><code>AttributeError: module 'torch' has no attribute 'no_grad'</code></pre>

SimonW: <p>it’s only on master.</p>

guysoft: <p>If I have code written by someone else (see <a href="https://github.com/ducha-aiki/affnet/blob/master/examples/hesaffnet/WBS%20demo.ipynb" rel="nofollow noopener">source</a>), is there a way to replace this with something that is not in master?<br>
I can’t use master because it won’t run with my CUDA compute 5.0 GPU.</p>

ygorcanalli: <p>I’m having the same issue <a class="mention" href="/u/brando_miranda">@Brando_Miranda</a></p>

ptrblck: <p>The function was added in <code>0.4.0</code>. You are probably using an older version of PyTorch.<br>
You can find the install instructions on the <a href="http://pytorch.org/">website</a>.</p>

Deeply: <p>I am having the same warning of using:<br>
<code>data, target = Variable(data, volatile=True), Variable(target)</code></p>
<p>should it be changed to:</p>
<pre><code class="lang-auto">with torch.no_grad():            
     data, target = Variable(data), Variable(target)
</code></pre>

SimonW: <ol>
<li>You shouldn’t use Variable wrappers anymore.</li>
<li>Use torch.no_grad() to wrap around the entire inference code. (or use <code>torch.set_grad_enabled</code>）</li>
</ol>

Shbnm21: <p>Can u please show how to use with torch.no_grad():</p>
<p>And replace volatile</p>
<p>I am testing a GitHub project and they have used volatile =False as a function argument<br>
So do I need to replace it with requires_grad=True</p>

yuanzhoulvpi: <p>In fact,you can read the official document,the torch.no_grad() in the part of doc which called torch.autograd,<a href="https://pytorch.org/docs/stable/autograd.html#locally-disable-grad" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/autograd.html#locally-disable-grad</a><br>
In addition,the Variable() function return a torch tensor,which is equal tensor(requires_grad=False)</p>
<p>The Variable API has been deprecated: Variables are no longer necessary to use autograd with tensors. Autograd automatically supports Tensors with  <code>requires_grad</code>  set to  <code>True</code> . Below please find a quick guide on what has changed:</p>
<ul>
<li><code>Variable(tensor)</code>  and  <code>Variable(tensor, requires_grad)</code> still work as expected, but they return Tensors instead of Variables.<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/6/69feb4ef4a7209e1318ee97ad13e1c672eaddadb.png" data-download-href="https://discuss.pytorch.org/uploads/default/69feb4ef4a7209e1318ee97ad13e1c672eaddadb" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/6/69feb4ef4a7209e1318ee97ad13e1c672eaddadb_2_281x499.png" alt="image" data-base62-sha1="f7FQ9nfXYeCRXdGpw3jD29YsuQb" width="281" height="499" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/6/69feb4ef4a7209e1318ee97ad13e1c672eaddadb_2_281x499.png, https://discuss.pytorch.org/uploads/default/optimized/2X/6/69feb4ef4a7209e1318ee97ad13e1c672eaddadb_2_421x748.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/6/69feb4ef4a7209e1318ee97ad13e1c672eaddadb_2_562x998.png 2x" data-dominant-color="ED9CEA"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">750×1334 145 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></li>
</ul>