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.