Result type Double can't be cast to the desired output type Long

username: wachhu
cooked: <p>Hi,</p>
<p>I’ve googled this problem, but most people run into it in the loss calculation step.  But mine happened during the generation of batch, I don’t know what went wrong.</p>
<blockquote>
<p>Blockquote<br>
Traceback (most recent call last):<br>
File “dtd_rnn_training.py”, line 469, in <br>
training(args)<br>
File “dtd_rnn_training.py”, line 212, in training<br>
for batch_idx, batch in enumerate(train):<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/utils/data/dataloader.py”, line 517, in <strong>next</strong><br>
data = self._next_data()<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/utils/data/dataloader.py”, line 1199, in _next_data<br>
return self._process_data(data)<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/utils/data/dataloader.py”, line 1225, in _process_data<br>
data.reraise()<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/_utils.py”, line 429, in reraise<br>
raise self.exc_type(msg)<br>
RuntimeError: Caught RuntimeError in DataLoader worker process 0.<br>
Original Traceback (most recent call last):<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py”, line 202, in _worker_loop<br>
data = fetcher.fetch(index)<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py”, line 47, in fetch<br>
return self.collate_fn(data)<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py”, line 73, in default_collate<br>
return {key: default_collate([d[key] for d in batch]) for key in elem}<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py”, line 73, in <br>
return {key: default_collate([d[key] for d in batch]) for key in elem}<br>
File “/home/anaconda3/envs/aa/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py”, line 55, in default_collate<br>
return torch.stack(batch, 0, out=out)<br>
RuntimeError: result type Double can’t be cast to the desired output type Long</p>
</blockquote>

username: ptrblck
cooked: <p>The error is raised, if <code>out</code> is a <code>LongTensor</code>, while (some of) the <code>batch</code> tensors are <code>DoubleTensors</code> as seen here:</p>
<pre><code class="lang-python">batch = [
    torch.randn(3, 24, 24, dtype=torch.float64),
    torch.randn(3, 24, 24, dtype=torch.float64)
]

out = torch.empty(2, 3, 24, 24).long()
torch.stack(batch, 0, out=out)
&gt; RuntimeError: result type Double can't be cast to the desired output type Long
</code></pre>
<p><a href="https://github.com/pytorch/pytorch/blob/c5f0692b6e9c3cfaa874286a28542cb3701d77b4/torch/utils/data/_utils/collate.py#L46-L56">These lines of code</a> check the first element of <code>batch</code> and use its <code>dtype</code> to create <code>out</code>. Could you check, if you are returning different <code>dtypes</code> in the <code>__getitem__</code> of your <code>Dataset</code>?</p>

username: wachhu
cooked: <p>You are right. It’s a binary classification task, so the label is saved as type long. At first, I just tried to fix the problem during taining stage. But it didn’t work, so in the end I changed the dtype of my dataset.</p>