RuntimeError: result type Double can't be cast to the desired output type Long

Ravi_Raja: <p>I want to compute BCEWithLogitsLoss(). I am using the following code:</p>
<pre><code class="lang-auto">for epoch in range(max_epochs):
        total_epoch_loss = 0
        for batch_idx, (inps, tgts) in enumerate(train_loader):
            tgts = tgts.reshape((-1)).to(device)
            tgts = tgts.to(torch.long)
            out = cln(inps)
            inpOut = torch.cat((inps, out), dim=1)
            fOut = util.continuous_xor_vectorized(inpOut.T, name)
            loss = criterion(fOut, tgts)
            total_epoch_loss += loss
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
</code></pre>
<p>where fOut and tgts are both 32 x 1 sized vectors. But getting the following error message:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "trainClassification2ndForm.py", line 52, in &lt;module&gt;
    cln, lossess = train_classifier(train_loader, loss_fn)
  File "trainClassification2ndForm.py", line 35, in train_classifier
    loss = criterion(fOut, tgts)
  File "/home/ravi/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/ravi/anaconda3/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 632, in forward
    reduction=self.reduction)
  File "/home/ravi/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py", line 2582, in binary_cross_entropy_with_logits
    return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)
RuntimeError: result type Double can't be cast to the desired output type Long
</code></pre>
<p>If I change dtype of fOut to long, then it throws following error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "trainClassification2ndForm.py", line 51, in &lt;module&gt;
    cln, lossess = train_classifier(train_loader, loss_fn)
  File "trainClassification2ndForm.py", line 35, in train_classifier
    loss = criterion(fOut, tgts)
  File "/home/ravi/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/ravi/anaconda3/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 632, in forward
    reduction=self.reduction)
  File "/home/ravi/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py", line 2582, in binary_cross_entropy_with_logits
    return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)
RuntimeError: exp_vml_cpu not implemented for 'Long'
</code></pre>
<p>I am unable to find out the mistake. Can anyone please help here?</p>
<p>Thanks!</p>

Ravi_Raja: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> can you help please?</p>

eqy: <p>Why are you using a threshold (producing a binary output) when the loss function is intended to be used with logits?</p>

Ravi_Raja: <p>Sorry, that should be commented.</p>
<p>I ran again without thresholding. It gives the same error:</p>
<pre><code class="lang-auto">RuntimeError: result type Double can't be cast to the desired output type Long
</code></pre>

Ravi_Raja: <p>Also for clarity, the function <code>util.continuous_xor_vectorized(inpOut.T, name)</code> computes continuous xor amongst the tensors using tnorms (Boolean Fuzzy Logic). This essentially is torch.multiplication operation.</p>
<p>Below is the function definition for reference.</p>
<pre><code class="lang-auto">def continuous_xor_vectorized(self, inp_vars, name):
        op1 = inp_vars[0, :]
        # print(inp_vars.shape[0])
        for i in range(inp_vars.shape[0]-1):
            op2 = inp_vars[i+1, :]
            t = self.tnorm_vectorized(1-op1, op2, name)
            u = self.tnorm_vectorized(op1, 1-op2, name)
            res = 1-self.tnorm_vectorized(1-t, 1-u, name)
            op1 = res
        return res
</code></pre>
<pre><code class="lang-auto">def tnorm_vectorized(self, t, u, name):
        if name == "luka":
            return max(0, t + u - 1)
        elif name == "godel":
            return torch.minimum(t, u)
        elif name == "product":
            return torch.multiply(t, u)
        else:
            print("Wrong Name!")
</code></pre>

eqy: <p>OK, I think the issue is that the targets are supposed to be of <code>float</code> dtype rather than <code>long</code> here. Does <code>tgts.to(torch.float)</code> work?</p>

Ravi_Raja: <aside class="quote no-group quote-modified" data-username="Ravi_Raja" data-post="1" data-topic="123742">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ravi_raja/48/32723_2.png" class="avatar"> Ravi_Raja:</div>
<blockquote>
<p><code>fOut = (fOut &gt; threshold).double()</code></p>
</blockquote>
</aside>
<p>Hey, it worked now! Thanks.</p>
<p>I don’t know why but when I tried this earlier, it broke the loss propagation.</p>