Error: Expected all tensors to be on the same device, While all are on same device
I don't know why I am getting this error, while my all tensors are at the same device &quot;cuda&quot;.
Here is code
def train(self):
    self.model.train()
    self.optimizer.zero_grad()  # Clear gradients.
    print(&quot;X is&quot;,self.XT.get_device())
    print(&quot;edge_index_ is&quot;, self.edge_index_.get_device())
    print(&quot;R_train_mask is&quot;, self.R_train_mask.get_device())

    print(&quot;datas.y is&quot;, self.datas.y.get_device())
    print(&quot;edge_index_ is&quot;, self.edge_index_.get_device())
    out = self.model(self.XT, self.edge_index_)  # Perform a single forward pass.
    loss = self.criterion(out[self.R_train_mask],
                          self.datas.y[self.R_train_mask])

Here is error
X is 0
edge_index_ is 0
R_train_mask is 0
datas.y is 0
edge_index_ is 0
Traceback (most recent call last):
  File &quot;/home/adnan/GNNPaperCodes/PyGCL/examples/GCAPaperAd.py&quot;, line 126, in &lt;module&gt;
    main()
  File &quot;/home/adnan/GNNPaperCodes/PyGCL/examples/GCAPaperAd.py&quot;, line 91, in main
    topfeatures=tf.TopFeaturesFind()
  File &quot;/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py&quot;, line 64, in TopFeaturesFind
    loss = self.train()
  File &quot;/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py&quot;, line 154, in train
    out = self.model(self.XT, self.edge_index_)  # Perform a single forward pass.
  File &quot;/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py&quot;, line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File &quot;/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py&quot;, line 211, in forward
    x = self.conv1(x, edge_index_)
  File &quot;/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py&quot;, line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File &quot;/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch_geometric/nn/conv/gcn_conv.py&quot;, line 181, in forward
    x = self.lin(x)
  File &quot;/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py&quot;, line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File &quot;/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch_geometric/nn/dense/linear.py&quot;, line 103, in forward
    return F.linear(x, self.weight, self.bias)
  File &quot;/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/functional.py&quot;, line 1848, in linear
    return torch._C._nn.linear(input, weight, bias)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat2 in method wrapper_mm)

Obvioulsy, Everything is ok if i change device to 'cpu'

You forgot to transfer self.model to the device. Indeed, the model has weights that must be transferred to the correct device, since they interact with your input.
You can do it with self.model.to(&quot;cuda&quot;).
