RuntimeError: expected dtype Float but got dtype Long for my loss function despite converting all tensors to float

madeye_moody: <p>Creating a basic CNN model for binary image classification. Tried evaluating my model before training but receive the runtime error for dtypes mismatch as the BCE loss function uses it’s weights in float form and it seems as though my inputs are Tensors containing Long data. I don’t believe I ever converted my data into Long but changed all the relevant tensors to float type anyways in the validation step method definition:</p>
<pre><code class="lang-auto">    def validation_step(self, batch):
        images, targets = batch 
        targets.to(torch.float32)
        targets = targets.view(-1,1)
        images.to(torch.float32)
        out = self(images)
        out.to(torch.float32)
        loss = F.binary_cross_entropy(out, targets)  # Calculate loss
        score = binary_acc(out, targets)
        return {'val_loss': loss.detach(), 'val_score': score.detach() }
</code></pre>
<p>However still receiving the same error message. This is the full notebook <a href="https://jovian.ml/moaaz645/malaria-cnn-basic" rel="nofollow noopener">https://jovian.ml/moaaz645/malaria-cnn-basic</a><br>
And this is the error message:</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
1 model = to_device(MalariaCnnModel(), device)<br>
----&gt; 2 evaluate(model, val_dl)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)<br>
13         def decorate_context(*args, **kwargs):<br>
14             with self:<br>
—&gt; 15                 return func(*args, **kwargs)<br>
16         return decorate_context<br>
17</p>
<p> in evaluate(model, val_loader)<br>
2 def evaluate(model, val_loader):<br>
3     model.eval()<br>
----&gt; 4     outputs = [model.validation_step(batch) for batch in val_loader]<br>
5     return model.validation_epoch_end(outputs)<br>
6</p>
<p> in (.0)<br>
2 def evaluate(model, val_loader):<br>
3     model.eval()<br>
----&gt; 4     outputs = [model.validation_step(batch) for batch in val_loader]<br>
5     return model.validation_epoch_end(outputs)<br>
6</p>
<p> in validation_step(self, batch)<br>
17         out = self(images)<br>
18         out.to(torch.float32)<br>
—&gt; 19         loss = F.binary_cross_entropy(out, targets)  # Calculate loss<br>
20         score = binary_acc(out, targets)<br>
21         return {‘val_loss’: loss.detach(), ‘val_score’: score.detach() }</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py in binary_cross_entropy(input, target, weight, size_average, reduce, reduction)<br>
2377<br>
2378     return torch._C._nn.binary_cross_entropy(<br>
-&gt; 2379         input, target, weight, reduction_enum)<br>
2380<br>
2381</p>
<p>RuntimeError: expected dtype Float but got dtype Long</p>

ptrblck: <p>The <code>to()</code> operation is not an inplace operation for tensors, so you would need to reassign the result:</p>
<pre><code class="lang-python">targets = targets.to(torch.float32)
...
</code></pre>

madeye_moody: <p>Thank you very much it works fine now. I apologize, my fundamentals are still weak in pytorch as I’ve recently started and it was a silly mistake.</p>