What is the difference between Tensors and Variables in Pytorch?

Brando_Miranda: <p>I was looking that there is are Variables and Tensors. Though I don’t quite understand why we need two structures that do essentially what looks to me, the same thing. The docs even say:</p>
<blockquote>
<p>Variable API is nearly the same as regular Tensor API (with the exception of a couple in-place methods, that would overwrite inputs required for gradient computation). In most cases Tensors can be safely replaced with Variables and the code will remain to work just fine.</p>
</blockquote>
<p>As far as I can tell from reading the docs is that Variables are part of the autograd package thus they can perform gradient computations and stuff. The only clear distinction to me seems to have torch tensors as “placeholders” for data. But variables can already sort of do that by setting the <code>requires_grad=False</code> to false. Thus it just seems odd. Why not only have Tensors and thats it nothing else and avoid making the API more convoluted and confusing than it needs to be (even the docs for Variable aren’t complete cuz they say to go read Tensors API). I am just giving my guesses, I assume there is probably some reason the code is organized as it is and would like to understand it a bit more so that I wrap my head around pytorch (coming from a matlab/tensorflow background).</p>

dgriff: <p>Think of tensors and variables as same thing. Variables are just wrappers for the tensors so you can now easily auto compute the gradients.</p>
<p>So if a tensor was batman…<br>
A Variable would be batman but with his utility belt on…<img src="//discuss.pytorch.org/images/emoji/apple/grin.png?v=5" title=":grin:" class="emoji" alt=":grin:"></p>
<p>Batman can do the job but if he has his utility belt on he’s got cool gadgets to use😉</p>

hughperkins: <p>torch tensors are actually the data.</p>
<p>variables wrap tensors, and construct a chain of operations between teh tensors, so taht the gradietns can flow back.</p>
<p>so, eg you create variable a, and then add 1 to it to get b. Theres now a link stored between a and b, in the <code>creator</code> property of b. Then when you call <code>.backward()</code> on b, the gradient backpropagates, via the function in <code>b.creator</code> into a.</p>
<p>tensors dont have the concept of gradients, creator etc. they purely store data, and handle operations on that data, like adding, multiplying nad stuff.</p>

oblum: <p>But why it is not possible to forward a tensor through a Sequential model (only Variable works for me)</p>

smth: <p><a class="mention" href="/u/oblum">@oblum</a> that’s because we didn’t write the code for Tensors, we wrote it for Variable</p>

Anil_Kumar: <p>The Variable class is a wrapper over torch Tensors (nd arrays in torch) that supports nearly all operations defined on tensors. PyTorch requires that the input tensor to be forward propagated has to be wrapped in a Variable. This facilitates automatic back propagation by simply calling the method backward() in the Variable class.</p>

tor: <p>But as of now, the Variable API has been deprecated, see<br>
<a href="https://pytorch.org/docs/stable/autograd.html#variable-deprecated" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/autograd.html#variable-deprecated</a></p>
<p>Hence, we should not bother using Variable to wrap a tensor anymore, right?<br>
Please do confirm? thanks</p>

hughperkins: <p>Correct. (this text is to get past the spam filter).</p>

111411: <p>Aren’t tensors could have a concept of gradients? like</p>
<pre><code class="lang-auto">t = torch.randn(3,5, requires_grad=True)
</code></pre>

111411: <aside class="quote no-group" data-username="oblum" data-post="4" data-topic="4914">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/o/3be4f8/48.png" class="avatar"> oblum:</div>
<blockquote>
<p>ot possible to forward a tensor through a Sequential model (only Variable works for me)</p>
</blockquote>
</aside>
<p>Ah, Variable is now deprecated and tensors could have gradients too…</p>