Hi
I defined a ResNet as follows
# Residual Block
class DenseResidual(torch.nn.Module):
def __init__(self, inp_dim, neurons, layers, **kwargs):
super(DenseResidual, self).__init__(**kwargs)
self.h1 = torch.nn.Linear(inp_dim, neurons)
self.hidden = [torch.nn.Linear(neurons, neurons)
for _ in range(layers-1)]
def forward(self, inputs):
h = torch.tanh(self.h1(inputs))
x = h
for layer in self.hidden:
x = torch.tanh(layer(x))
# Defining Residual Connection and returning
return x + h
# ResNet Architecture
class MyResNet(torch.nn.Module):
def __init__(self, **kwargs):
super(MyResNet, self).__init__(**kwargs)
self.b1 = DenseResidual(2, 8, 3)
self.b2 = DenseResidual(8, 16, 3)
self.hn = torch.nn.Linear(16, 8)
self.out = torch.nn.Linear(8, 1)
def forward(self, inputs):
x = self.b1(inputs)
x = self.b2(x)
x = torch.tanh(self.hn(x))
x = self.out(x)
return x
model = MyResNet()
When I run the forward pass using code
model.to(device)
optimizer = torch.optim.Adam(model.parameters())
criterion = torch.nn.MSELoss()
EPOCHS = 5
for epoch in range(EPOCHS):
optimizer.zero_grad()
train_m.requires_grad = True
p = model(train_m)
print(p)
I get an error message
RuntimeError Traceback (most recent call last)
<ipython-input-22-cf2450a381be> in <module>
9
10 train_m.requires_grad = True
---> 11 p = model(train_m)
12 print(p)
13
~\miniconda3\envs\torch\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
<ipython-input-20-e99f45da034d> in forward(self, inputs)
9
10 def forward(self, inputs):
---> 11 x = self.b1(inputs)
12 x = self.b2(x)
13 x = torch.tanh(self.hn(x))
~\miniconda3\envs\torch\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
<ipython-input-14-9481614a812a> in forward(self, inputs)
11 x = h
12 for layer in self.hidden:
---> 13 x = torch.tanh(layer(x))
14
15 # Defining Residual Connection and returning
~\miniconda3\envs\torch\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
~\miniconda3\envs\torch\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
91
92 def forward(self, input: Tensor) -> Tensor:
---> 93 return F.linear(input, self.weight, self.bias)
94
95 def extra_repr(self) -> str:
~\miniconda3\envs\torch\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
1688 if input.dim() == 2 and bias is not None:
1689 # fused op is marginally faster
-> 1690 ret = torch.addmm(bias, input, weight.t())
1691 else:
1692 output = input.matmul(weight.t())
RuntimeError: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU, but expected them to be on GPU (while checking arguments for addmm)
As I’ve moved the model to gpu, I can’t understand why this is happening?