Getting cuda error after sending model and inputs to the GPU

username: Jordan_Howell
cooked: <p>Hello,</p>
<p>I’m gettting the following error after sending the model and inputs/targets/etc to my GPU.</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-80-322e3b0315bb&gt; in &lt;module&gt;
     10 
     11     # Forward pass
---&gt; 12     outputs = model(inputs)
     13     loss = criterion(outputs, targets)
     14 

~\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--&gt; 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

&lt;ipython-input-75-bd129658420d&gt; in forward(self, x)
     15 
     16         log_offset = torch.log(self.o)
---&gt; 17         layer1 = self.fc1(self.x)
     18         output = layer1+log_offset
     19 

~\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--&gt; 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-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):

~\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-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>Here is my model:</p>
<pre><code class="lang-auto">class pure_premium(nn.Module):
    def __init__(self, n_inputs, offset, weights):
        super(pure_premium, self).__init__()
        self.x = n_inputs
        self.o = offset
        self.w = weights

        #one layer input for GLM
        self.fc1 = nn.Linear(self.x.shape[1], 1)
        
    def forward(self, x):
        
        log_offset = torch.log(self.o)
        layer1 = self.fc1(self.x)
        output = layer1+log_offset
        
        return output

model = pure_premium(inputs, offset_tensor, 1).cuda()

criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)

# Train the model
n_epochs = 20
losses = []
for it in range(n_epochs):
    inputs = inputs.cuda()
    targets = targets.cuda()
    offset_tensor = offset_tensor.cuda()
    # zero the parameter gradients
    optimizer.zero_grad()

    # Forward pass
    outputs = model(inputs)
    loss = criterion(outputs, targets)

    # keep the loss so we can plot it later
    losses.append(loss.item())

    # Backward and optimize
    loss.backward()
    optimizer.step()

    print(f'Epoch {it+1}/{n_epochs}, Loss: {loss.item():.4f}')
</code></pre>
<p>Here is how my data is derived:</p>
<pre><code class="lang-auto"># (num_samples x num_dimensions)

# X = x.reshape(N, 1)
y_array = np.array(y)
y_array = np.where(y==(-np.inf), 0, y)
X = np.array(x)
Y = y_array.reshape(y.shape[0], 1)
offset_array = offset_factor.reshape(offset_factor.shape[0], 1)

# PyTorch uses float32 by default
# Numpy creates float64 by default
inputs = torch.from_numpy(X.astype(np.float32))
targets = torch.from_numpy(Y.astype(np.float32))
offset_tensor = torch.from_numpy(offset_array.astype(np.float32))
</code></pre>
<p>I’m sure I’m missing something obvious but I’m not seeing where.</p>

username: b02202050
cooked: <p>Hi, I think the problem comes from this line</p>
<aside class="quote no-group" data-username="Jordan_Howell" data-post="1" data-topic="76576">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jordan_howell/48/16421_2.png" class="avatar"> Jordan_Howell:</div>
<blockquote>
<p>layer1 = self.fc1(self.x)</p>
</blockquote>
</aside>
<p>Do you intend to write <code>layer1 = self.fc1(x)</code> ?</p>
<p>This tells us we should have a good naming habbit <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>

username: Jordan_Howell
cooked: <p>Thank you for replying.  I’m still getting the same error.</p>

username: boto
cooked: <p>Hi!</p>
<p>Have you tried moving the offset to CUDA? Something like:</p>
<pre><code class="lang-auto">class pure_premium(nn.Module):
    def __init__(self, n_inputs, offset, weights):
        super(pure_premium, self).__init__()
        self.x = n_inputs
        self.o = offset.cuda() # or torch.tensor(data=offset).cuda()
        self.w = weights

        #one layer input for GLM
        self.fc1 = nn.Linear(self.x.shape[1], 1)
</code></pre>

username: Jordan_Howell
cooked: <p>I do.  Under the training loop, <code>offset_tensor</code> is the offsets and they are moved to cuda.</p>

username: boto
cooked: <p>So you initialize the neural network with one offset and then you want to change it during training?</p>

username: Jordan_Howell
cooked: <p>Ok. I moved the data to cuda before I instantiated the model.  Now it ran. On to figuring out why my custom loss function is failing.  Thank you <a class="mention" href="/u/boto">@boto</a></p>