Volatile = now has no effect. Use `with torch.no_grad():` instead

username: oasjd7
cooked: <pre><code class="lang-auto">for batch_idx, image in enumerate(data_loader):
        image = Variable(image, volatile=True)
</code></pre>
<p>This is my original code<br>
and I get  this message.<br>
<code>volatile = now has no effect. Use</code>with torch.no_grad():<code>instead.</code></p>
<p>I searched about this and then I find that there is no need use to Variable().<br>
then I just use</p>
<pre><code class="lang-auto">for batch_idx, image in enumerate(data_loader):
        Just use image ...
</code></pre>
<p>or change like this ?</p>
<pre><code class="lang-auto">with torch.no_grad():
       image = Variable(image)
</code></pre>

username: justusschock
cooked: <p>Using</p>
<pre><code class="lang-auto">with torch.no_grad():
    for batch_idx, image in enumerate(data_loader):
        # perform your operations on image without wrapping it in a variable

</code></pre>
<p>Should work since Variables and Tensors have been merged in v 0.4</p>

username: Niki
cooked: <p>I used <code>torch.no_grad()</code> as you mentioned, but now I am getting the error in <code>loss.backward()</code>:</p>
<p>element 0 of tensors does not require grad and does not have a grad_fn.</p>
<p>Instead of <code>inputs = Variable(inputs, volatile=True)</code>, I used:</p>
<pre><code class="lang-auto">inputs = Variable(inputs, requires_grad=True)
</code></pre>
<p>it doesn’t show any errors, but I am not sure that is correct?</p>

username: ptrblck
cooked: <p>If you are using <code>with torch.no_grad():</code>, you explicitly say you don’t want to calculate gradients for the operations inside this block, so <code>loss.backward()</code> won’t work. Usually you use it for the validation/test dataset.<br>
If you need to call <code>loss.backward()</code> you shouldn’t use it.</p>

username: Shbnm21
cooked: <p>I am testing a project.<br>
There is a function<br>
def tensor2var(self,tensor,volatile=False):<br>
Some code…<br>
var=torch.autograd.Variable(tensor,volatile=volatile)</p>
<p>When I run this I get warning<br>
Volatile is depricated.use with torch.no_grad():</p>
<p>Can u please elaborate how to use torch.no_grad(): here in this function.</p>

username: ptrblck
cooked: <p>That might be a bit tricky, as the <code>volatile</code> argument is not an attribute of the data anymore but of the workflow. You could probably write some workaround using <code>torch.set_grad_enabled</code>, but I’m not sure if this will fit your use case.</p>
<p>The proper way now would be to wrap the complete method in a <code>with torch.no_grad()</code> block. Would that work for you?</p>

username: Shbnm21
cooked: <p>Thank u for quick reply.<br>
I will try and let u know.</p>

username: Shbnm21
cooked: <p>I read this on pytorch<br>
Variable has been removed in new version of python. Tensors are the same as the old variables.</p>
<p>So do I need to remove variable from this code<br>
var=torch.autograd.Variable(tensor,volatile=volatile)</p>
<p>I didn’t understand</p>

username: ptrblck
cooked: <p>Yes, <code>Variables</code> are deprecated in newer PyTorch versions.<br>
Instead of setting <code>volatile=True</code>, you would now have to wrap your code in a <code>torch.no_grad()</code> block:</p>
<pre><code class="lang-python">with torch.no_grad():
    x = torch.randn(1, 3, 224, 224)
    output = model(x)
</code></pre>
<p>This will make sure to avoid storing the intermediate activations (same as with <code>volatile=True</code>).</p>

username: Shbnm21
cooked: <p>Ok,I got it now.<br>
Thank u so much.</p>

username: Shbnm21
cooked: <p>Hello<br>
As u suggested I replaced code as follows<br>
def tensor2var(self,tensors,requires_grad=True)<br>
Some code…<br>
With torch.no_grad():<br>
var=torch.autograd.Variable(tensor)</p>
<p>And where this function is called there also I replaced volatile by requires_grad=True</p>
<p>And it worked.</p>
<p>Thank u so much …</p>

username: ptrblck
cooked: <p>Good to hear it’s working now.<br>
However, you don’t need to wrap your tensors in <code>Variables</code> anymore, so just use the tensors directly and set the <code>requires_grad</code> argument while creating the tensor.</p>

username: Shbnm21
cooked: <p>Do u mean<br>
var=(tensor,required_grad=True)<br>
Like this…</p>

username: ptrblck
cooked: <p><code>required_grad</code> is an attribute of <code>tensor</code>, so you should use it as e.g.:</p>
<pre><code class="lang-python">x = torch.tensor([1., 2., 3.], requires_grad=True)

x = torch.randn(1, requires_grad=True)

x = torch.randn(1)
x.requires_grad_(True)
</code></pre>

username: Shbnm21
cooked: <p>Ok<br>
Can we export trained pytorch model in Android studio??</p>

username: solsol
cooked: <p>I also faced this issue in the following line:</p>
<p>return Variable(tensor(np.concatenate(datom, 0)), volatile=self.volatile), chunk_sizes</p>
<p>UserWarning: volatile was removed and now has no effect. Use <code>with torch.no_grad():</code> instead.</p>
<p>any help please?</p>

username: ptrblck
cooked: <p>Warp your code into the <code>torch.no_grad()</code> guard, since <code>Variables</code> are deprecated:</p>
<pre><code class="lang-python"># before
...
x = Variable(torch.randn(1), volatile=True)
return x

# now
with torch.no_grad():
    ...
    x = torch.randn(1)
return x
</code></pre>

username: VahidZarghami
cooked: <p>Hi<br>
I have got the same error but my code is different can you help me please?<br>
This is my error in utils.py file</p>
<pre><code class="lang-auto">content/gdrive/My Drive/DeepFakeDetection/utils.py:21: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
&gt;   return Variable(x, volatile=volatile)
</code></pre>
<p>And this is my code in utils.py file</p>
<pre><code class="lang-auto">def to_var(x, volatile=False):`
&gt; &gt;      if torch.cuda.is_available():
&gt; &gt;          x = x.cuda()
&gt; &gt;      return Variable(x, volatile=volatile)
</code></pre>
<p>Thank you</p>

username: ptrblck
cooked: <p><code>Variables</code> are deprecated since PyTorch <code>0.4</code> and with it the <code>volatile</code> argument.<br>
You can use tensors now, and if you don’t need to calculate gradients (as was specified via <code>volatile=True</code>), you should wrap the code in a <code>torch.no_grad()</code> block:</p>
<pre><code class="lang-python">with torch.no_grad():
    x = x + 1 # Autograd won't track these operations
</code></pre>

username: VahidZarghami
cooked: <p>As you said I changed my code to this and did not received warning again<br>
Thank you</p>
<blockquote>
<p>def to_var(x, volatile=False):</p>
<pre><code> with torch.no_grad():
     
     if torch.cuda.is_available():
         x = x.cuda()
 return x
</code></pre>
</blockquote>