Expected object of backend CPU but got backend CUDA for argument #2 'source'
I have tried other answers but the error is not getting removed. Difference from the other question I am getting is that the last term used in error is 'source' which I did not find in any question. If possible please also explain the term 'source' in error. And running code without CPU is working fine.


  I am using Google Colab with GPU enabled.


import torch
from torch import nn
import syft as sy

hook = sy.TorchHook(torch)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model = nn.Sequential(nn.Linear(784,256),
                     nn.ReLU(),
                     nn.Linear(256,128),
                     nn.ReLU(),
                     nn.Linear(128,64),
                     nn.ReLU(),
                     nn.Linear(64,10),
                     nn.LogSoftmax(dim = 1))

model = model.to(device)


output :

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-42-136ec343040a&gt; in &lt;module&gt;()
      8                      nn.LogSoftmax(dim = 1))
      9 
---&gt; 10 model = model.to(device)

3 frames
/usr/local/lib/python3.6/dist-packages/syft/frameworks/torch/hook/hook.py in data(self, new_data)
    368 
    369                 with torch.no_grad():
--&gt; 370                     self.set_(new_data)
    371             return self
    372 

RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'source'


This issue is related to PySyft. As you can see in this Issue#1893, the current workaround is to set:

import torch
torch.set_default_tensor_type(torch.cuda.FloatTensor)


right after import torch.

Code:

import torch
from torch import nn
torch.set_default_tensor_type(torch.cuda.FloatTensor)  # &lt;-- workaround

import syft as sy
hook = sy.TorchHook(torch)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)

model = nn.Sequential(nn.Linear(784,256),
                     nn.ReLU(),
                     nn.Linear(256,128),
                     nn.ReLU(),
                     nn.Linear(128,64),
                     nn.ReLU(),
                     nn.Linear(64,10),
                     nn.LogSoftmax(dim = 1))

model = model.to(device)
print(model)


Output:

cuda
Sequential(
  (0): Linear(in_features=784, out_features=256, bias=True)
  (1): ReLU()
  (2): Linear(in_features=256, out_features=128, bias=True)
  (3): ReLU()
  (4): Linear(in_features=128, out_features=64, bias=True)
  (5): ReLU()
  (6): Linear(in_features=64, out_features=10, bias=True)
  (7): LogSoftmax()
)

