Getting cuda error after sending model and inputs to the GPU

Hello,

I’m gettting the following error after sending the model and inputs/targets/etc to my GPU.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-80-322e3b0315bb> in <module>
     10 
     11     # Forward pass
---> 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:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

<ipython-input-75-bd129658420d> in forward(self, x)
     15 
     16         log_offset = torch.log(self.o)
---> 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:
--> 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):
---> 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
-> 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

Here is my model:

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}')

Here is how my data is derived:

# (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))

I’m sure I’m missing something obvious but I’m not seeing where.

Hi, I think the problem comes from this line

Do you intend to write layer1 = self.fc1(x) ?

This tells us we should have a good naming habbit :wink:

Thank you for replying. I’m still getting the same error.

Hi!

Have you tried moving the offset to CUDA? Something like:

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)

I do. Under the training loop, offset_tensor is the offsets and they are moved to cuda.

So you initialize the neural network with one offset and then you want to change it during training?

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 @boto

1 Like