When I run my network to GPU,it got an error

fyy: <p>Hi !, I run my model with PyTorch. However, I met a problem when I trained my model with GPU. The error message is shown below.</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "F:/experiment_code/deep-learning-with-iv-fault-detection/network/CNN_ResGRU.py", line 60, in &lt;module&gt;
    out = net(x)
  File "D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "F:/experiment_code/deep-learning-with-iv-fault-detection/network/CNN_ResGRU.py", line 48, in forward
    out = nn.Linear(out.size(1) , 5)(out)
  File "D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "D:\Anaconda3\lib\site-packages\torch\nn\modules\linear.py", line 92, in forward
    return F.linear(input, self.weight, self.bias)
  File "D:\Anaconda3\lib\site-packages\torch\nn\functional.py", line 1406, in linear
    ret = torch.addmm(bias, input, weight.t())
RuntimeError: Expected object of backend CPU but got backend CUDA for argument #4 'mat1'

Process finished with exit code 1
</code></pre>
<p>and  this is my network code ,I don’t know how to solve this problem.</p>
<pre><code class="lang-auto">import torch
from torch import nn
from torch.nn import functional as  F
from torch.utils.data import DataLoader
from torchvision import transforms
from torch import nn, optim

from network.RGRU  import Residual_Gated_Recurrent_Unit
from network.CNN import CNN

class CNN_Residual_Gated_Recurrent_Unit(nn.Module):
    def __init__(self,ch_in, ch_out,input_size1, hidden_size1, input_size2,hidden_size2, input_size3, hidden_size3):
        super(CNN_Residual_Gated_Recurrent_Unit, self).__init__()

        self.cnn = nn.Sequential(
            CNN(ch_in, ch_out)
        )
        self.RGRU = nn.Sequential(
            Residual_Gated_Recurrent_Unit(input_size1, hidden_size1, input_size2, hidden_size2, input_size3, hidden_size3)
        )
        self.outputlayer = nn.Sequential(
           nn.ReLU(),
           nn.Dropout(p=0.5),
           # nn.Linear(in_features, out_features),
           # nn.Softmax()
        )

    def forward(self, x):

        out = self.cnn(x)

        out = self.RGRU(out)

        out = self.outputlayer(out)

        out = out.permute(1, 0, 2)

        out = out.contiguous().view(out.size(0), -1)

        out = nn.Linear(out.size(1) , 5)(out)

        out = nn.Softmax(dim=1)(out)

        return out


if __name__ == '__main__':
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    x = torch.rand(size=(8,  4, 256)).to(device)
    net = CNN_Residual_Gated_Recurrent_Unit(4, 128, 120, 50, 50, 60, 120, 60)
    net = net.to(device)
    out = net(x)
    print(out.size())
    print("************************************打印结果*********************************************")
    print(out)

</code></pre>
<p>I wonder why the error comes.<br>
Thank you!</p>

valgi0: <p>I think it is because nn.Linear() creates a new layer on the fly, and it is not placed on GPU. Try to create it once in the init method, so it will be moved on GPU with the entire net. If you cant do it in init, so you can move the new layer on the GPU every time:</p>
<p><code>new_layer = nn.Linear(out.size(1), 5)</code><br>
<code>new_layer = new_layer.to(self.device)</code><br>
<code>out = new_layer(out)</code></p>
<p>something like that</p>