Passing Variable in BCE loss function

nullgeppetto: <p>I’m working on a Variation AutoEncoder example and I have a simple question that is mainly about PyTorch.</p>
<p>Let’s suppose that we use binary cross entropy (<code>torch.nn.BCELoss()</code>) as the reconstruction loss; that is, we apply BCELoss on the reconstructed images (output of decoder) and target images (original images).</p>
<p>However, original images are the input of the encoder, and, thus, they are naturally defined as <code>torch.autograd.Variable</code>. On the other hand, the reconstructed images are <code>torch.Tensor</code>. Applying BCELoss on them makes PyCharm complain about the types of inputs (BCELoss is expected to take two tensors, not one tensor and one Variable).</p>
<p>I’m just wondering whether this might cause any problems. Should I give BCELoss its targets (i.e., original images) as non-Variable tensors, or is it ok to keep it as is?</p>

albanD: <p>Hi.</p>
<p><code>Variable</code> are not a thing anymore. They are just regular Tensors. So you can just use Tensors for everything <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

nullgeppetto: <p>Hi, thanks for the quick response. That’s fine, but what do you mean by "<code>Variable</code> is not a thing anymore"? Shouldn’t input tensors, for instance, be defined as Variables for auto-differentiation etc?</p>
<p>Thanks for your time <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

albanD: <p>Variable and Tensor have been merged. So they are now exactly the same thing ! (since version 0.4 <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"> )</p>
<p>You can just give regular Tensors to your forward pass. And if you need them to require gradients, you can either create them with <code>torch.rand(10, 10, requires_grad=True)</code> or set it with <code>your_tensor.requires_grad_()</code>.</p>

nullgeppetto: <p>That’s interesting (and terrifying that I didn’t notice this major change before).</p>
<p>So I can just get my input tensors from my data loader and just apply <code>.requires_grad()</code> on them? For example, in a training session, should we just do the following?</p>
<pre><code class="lang-auto">for i, x in enumerate(data_loader):
    out = model(x.requires_grad())
</code></pre>
<p>And in the case where we need to upload data on GPU, we just apply <code>.cuda()</code> after <code>.requires_grad()</code>?</p>

albanD: <p>If you don’t plan on computing the gradients for <code>x</code>, you don’t need to do anything <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
The parameters inside the model already require gradients and that will be enough for the autograd to know when to start recording the graph.</p>

nullgeppetto: <p>I see, thank very much for this enlightening answer <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>