Expected object of device type cuda but got device type cpu

nzinc: <p>Code below:</p>
<pre><code class="lang-auto">class Critic(nn.Module):
    def __init__(self):
        super(Critic, self).__init__()
        self.main = nn.Sequential(
            nn.Linear((data_train.shape[1] - 1), 128).to(device),
            nn.ReLU(),
            nn.Linear(128, 128),
            nn.ReLU(),
            nn.Linear(128, 128),
            nn.ReLU(),
            nn.Linear(128, 128),
            nn.ReLU(),
            nn.Linear(128, 128),
            nn.ReLU(),
            nn.Linear(128, utils_rich.y_count)
        )

    def forward(self, input):
        output = self.main(input)
        return output

def init_weights(m):
    if type(m) == nn.Linear:
        torch.nn.init.xavier_uniform(m.weight)
        m.bias.data.fill_(0.01)

device = torch.device("cuda:3")
netG = Generator().to(device)
netC = Critic().to(device)
netC.apply(init_weights)
</code></pre>
<p>Its model code and init.</p>
<p>Train looks like:</p>
<pre><code class="lang-auto">for epoch in tqdm(range(TOTAL_ITERATIONS), position=0, leave=True):
    for i, data in enumerate(all_dataloader):
        train_full, w_full, train_x_1, w_x_1, train_x_2, w_x_2 = data
        train_full.to(device)
        for j in range(critic_policy(epoch)):
            #Critic train
            netC.zero_grad()
            output = netC(train_full)
........
</code></pre>
<p>and error message looks like</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-51-d80103d7bf6d&gt; in &lt;module&gt;
      6             #Critic train
      7             netC.zero_grad()
----&gt; 8             output = netC(train_full)
      9             print(output.shape)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--&gt; 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

&lt;ipython-input-46-50038b4dca0f&gt; in forward(self, input)
     41 
     42     def forward(self, input):
---&gt; 43         output = self.main(input)
     44         return output
     45 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--&gt; 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py in forward(self, input)
     90     def forward(self, input):
     91         for module in self._modules.values():
---&gt; 92             input = module(input)
     93         return input
     94 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--&gt; 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---&gt; 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1368     if input.dim() == 2 and bias is not None:
   1369         # fused op is marginally faster
-&gt; 1370         ret = torch.addmm(bias, input, weight.t())
   1371     else:
   1372         output = input.matmul(weight.t())

RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'mat1' in call to _th_addmm
</code></pre>
<p>I tried all solution for this error, that was on this site (apply nn.Sequential and transfer to cuda all data and model)</p>

albanD: <p>Hi,</p>
<p>The <code>.to()</code> operation on Tensor is not inplace, so you need to replace <code>train_full.to(device)</code> by <code>train_full = train_full.to(device)</code>.</p>