How to solve error: 'All input tensors must be on the same device. Received cuda:0 and cpu'

Prats: <p>code is:</p>
<pre><code class="lang-auto">syn_dataset = train_dataset.create_syn_dataset()
final_dataset = ZSLDataset('awa1', n_train, n_test, train_agent,
        gzsl=gzsl, train=True, synthetic=True, syn_dataset=syn_dataset)
final_train_generator = DataLoader(final_dataset, **params)

model_name = "awa1_final_classifier"
success = train_agent.load_model(model=model_name)
if success:
    print("\nFinal classifier parameters loaded....")
else:
    print("\nTraining the final classifier on the synthetic dataset...")
    for ep in range(1, n_epochs + 1):
        syn_loss = 0
        for idx, (img, label_attr, label_idx) in enumerate(final_train_generator):
            l = train_agent.fit_final_classifier(img, label_attr, label_idx)
            syn_loss += l
        # print losses on real and synthetic datasets
        print("Loss for epoch: %3d - %.4f" %(ep, syn_loss))
</code></pre>
<p>error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-37-12dd6797e446&gt; in &lt;module&gt;
     16     for ep in range(1, n_epochs + 1):
     17         syn_loss = 0
---&gt; 18         for idx, (img, label_attr, label_idx) in enumerate(final_train_generator):
     19             l = train_agent.fit_final_classifier(img, label_attr, label_idx)
     20             syn_loss += l

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    433         if self._sampler_iter is None:
    434             self._reset()
--&gt; 435         data = self._next_data()
    436         self._num_yielded += 1
    437         if self._dataset_kind == _DatasetKind.Iterable and \

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
    473     def _next_data(self):
    474         index = self._next_index()  # may raise StopIteration
--&gt; 475         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    476         if self._pin_memory:
    477             data = _utils.pin_memory.pin_memory(data)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     45         else:
     46             data = self.dataset[possibly_batched_index]
---&gt; 47         return self.collate_fn(data)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py in default_collate(batch)
     81             raise RuntimeError('each element in list of batch should be of equal size')
     82         transposed = zip(*batch)
---&gt; 83         return [default_collate(samples) for samples in transposed]
     84 
     85     raise TypeError(default_collate_err_msg_format.format(elem_type))

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py in &lt;listcomp&gt;(.0)
     81             raise RuntimeError('each element in list of batch should be of equal size')
     82         transposed = zip(*batch)
---&gt; 83         return [default_collate(samples) for samples in transposed]
     84 
     85     raise TypeError(default_collate_err_msg_format.format(elem_type))

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py in default_collate(batch)
     53             storage = elem.storage()._new_shared(numel)
     54             out = elem.new(storage)
---&gt; 55         return torch.stack(batch, 0, out=out)
     56     elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \
     57             and elem_type.__name__ != 'string_':

RuntimeError: All input tensors must be on the same device. Received cuda:0 and cpu
</code></pre>
<p>I am unable to point at the error. I tried running it just on cpu and I got this error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-10-12dd6797e446&gt; in &lt;module&gt;
     16     for ep in range(1, n_epochs + 1):
     17         syn_loss = 0
---&gt; 18         for idx, (img, label_attr, label_idx) in enumerate(final_train_generator):
     19             l = train_agent.fit_final_classifier(img, label_attr, label_idx)
     20             syn_loss += l

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    433         if self._sampler_iter is None:
    434             self._reset()
--&gt; 435         data = self._next_data()
    436         self._num_yielded += 1
    437         if self._dataset_kind == _DatasetKind.Iterable and \

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
    473     def _next_data(self):
    474         index = self._next_index()  # may raise StopIteration
--&gt; 475         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    476         if self._pin_memory:
    477             data = _utils.pin_memory.pin_memory(data)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     45         else:
     46             data = self.dataset[possibly_batched_index]
---&gt; 47         return self.collate_fn(data)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py in default_collate(batch)
     81             raise RuntimeError('each element in list of batch should be of equal size')
     82         transposed = zip(*batch)
---&gt; 83         return [default_collate(samples) for samples in transposed]
     84 
     85     raise TypeError(default_collate_err_msg_format.format(elem_type))

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py in &lt;listcomp&gt;(.0)
     81             raise RuntimeError('each element in list of batch should be of equal size')
     82         transposed = zip(*batch)
---&gt; 83         return [default_collate(samples) for samples in transposed]
     84 
     85     raise TypeError(default_collate_err_msg_format.format(elem_type))

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py in default_collate(batch)
     53             storage = elem.storage()._new_shared(numel)
     54             out = elem.new(storage)
---&gt; 55         return torch.stack(batch, 0, out=out)
     56     elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \
     57             and elem_type.__name__ != 'string_':

TypeError: expected Tensor as element 1 in argument 0, but got int
</code></pre>
<p>Both these errors are arising from the same line in the code ’ for idx, (img, label_attr, label_idx) in enumerate(final_train_generator):’<br>
Thanks.</p>

mma525: <p>Make sure you send all of your input tensors (img, label_attr, label_idx) to GPU. You can send it to your GPU by adding .cuda() (e.g. for img, it will be img.cuda())</p>

Prats: <p>Hi, thanks for the reply!<br>
I tried that but it says, <code>SyntaxError: can't assign to function call</code><br>
Is there any other way?</p>

mma525: <p>Can you show me your updated code?</p>

Prats: <p>I only added it to the first term, img.to(device) and it gave an error, code:</p>
<pre><code class="lang-auto">for idx, (img.to(device), label_attr, label_idx) in enumerate(final_train_generator):
</code></pre>
<p>Error:</p>
<pre><code class="lang-auto">  File "&lt;ipython-input-9-fff104645c7f&gt;", line 18
    for idx, (img.to(device), label_attr, label_idx) in enumerate(final_train_generator):
            ^
SyntaxError: can't assign to function call
</code></pre>

mma525: <p>There might be some syntax issue since you are applying it directly in the for loop. Try this and see if it works:</p>
<pre><code class="lang-python">---&gt; 18         for idx, (img, label_attr, label_idx) in enumerate(final_train_generator):
     19             l = train_agent.fit_final_classifier(img.cuda(), label_attr.cuda(), label_idx.cuda())
     20             syn_loss += l
</code></pre>

Prats: <p>The correction you suggested is inside the for loop but the error (<code>RuntimeError: All input tensors must be on the same device. Received cuda:0 and cpu</code>) is at the for loop statement.<br>
Also, I just tried what you suggested, but it’s throwing the same error.</p>

mma525: <p>This means that your model has not been sent to GPU. Make sure your model is also on GPU.</p>