I have a model class like this, and I tried to run it on gpu:
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv = nn.Conv2d(3, 64, kernel_size = 3)
self.avgpool = nn.AvgPool2d(3, 1)
self.denses = []
self.denses.append(nn.Linear(64, 10))
self.denses.append(nn.Linear(64, 20))
def forward(self, x):
x = self.conv(x)
x = self.avgpool(x).view(-1, 64)
out1 = self.denses[0](x)
out2 = self.denses[1](x)
return out1, out2
m = Model()
in_tensor = torch.randn(1, 3, 128, 128).cuda()
m.cuda()
out = m(in_tensor)
Then I have the error message:
Traceback (most recent call last):
File "try.py", line 56, in <module>
out = m(in_tensor)
File "/home/zhangzy/.local/lib/python3.5/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "try.py", line 48, in forward
out1 = self.denses[0](x)
File "/home/zhangzy/.local/lib/python3.5/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "/home/zhangzy/.local/lib/python3.5/site-packages/torch/nn/modules/linear.py", line 55, in forward
return F.linear(input, self.weight, self.bias)
File "/home/zhangzy/.local/lib/python3.5/site-packages/torch/nn/functional.py", line 992, in linear
return torch.addmm(bias, input, weight.t())
RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #4 'mat1'
Can’t I use python list to hold these dense branches? How shall I use structure such as list or tuple to manipulate these layers properly ?