RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

I am in the 06_multicat notebook of the fastbook directory. When I run the following cell:

x,y = dls.train.one_batch()
activs = learn.model(x)
activs.shape

I get the following error:

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

I don’t know where it is going wrong.

I am trying to find the shape of the activations of a learner object that is created.

Here is the full output-

Full output (click here)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-27-7b26555ca519> in <module>
      1 x,y = dls.train.one_batch()
----> 2 activs = learn.model(x)
      3 activs.shape

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/container.py in forward(self, input)
    115     def forward(self, input):
    116         for module in self:
--> 117             input = module(input)
    118         return input
    119 

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/container.py in forward(self, input)
    115     def forward(self, input):
    116         for module in self:
--> 117             input = module(input)
    118         return input
    119 

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/conv.py in forward(self, input)
    417 
    418     def forward(self, input: Tensor) -> Tensor:
--> 419         return self._conv_forward(input, self.weight)
    420 
    421 class Conv3d(_ConvNd):

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
    413                             weight, self.bias, self.stride,
    414                             _pair(0), self.dilation, self.groups)
--> 415         return F.conv2d(input, weight, self.bias, self.stride,
    416                         self.padding, self.dilation, self.groups)
    417 

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

Help wanted to sort this out and to understand where it is going wrong.

9 Likes

Hey Ritobrata!

The error is saying that the input (the data) is a CUDA tensor, meaning it is on the gpu, whereas the weights (the model) aren’t, meaning they are on the cpu. Therefore you just need to move your model to the gpu by calling .cuda() and then everything will work as expected :slight_smile:

7 Likes

As per @orendar’s suggestions, I moved the model to CUDA by running:

learn.model.cuda()

The whole thing now looks like:

learn = cnn_learner(dls, resnet18) # learner is created
learn.model.cuda() # model moved to CUDA
x,y = dls.train.one_batch()
activs = learn.model(x) # activations assigned name 'activs'

I got the following:

>>> activs.shape
torch.Size([64, 20])
>>> activs[0]
tensor([-3.0163, -1.6634, -0.5556,  3.5918,  0.5905,  2.4112,  1.4508, -1.2942,  1.7534,  0.2403,  1.5989,  0.7929, -0.7490, -0.8803,  0.0217, -1.8234,  0.3048,  2.1683, -0.5320, -1.8445],
       device='cuda:0', grad_fn=<SelectBackward>)

So the problem was solved, thanks to @orendar.

18 Likes

Thanks the posts.

I’ve got the same problem on Paperspace.
Just adding the .cuda() line before the call for .model(), I could preceed to the next.

‘’’
learn = cnn_learner(dls, resnet18)
x,y = dls.train.one_batch()
learn.model.cuda()
activs = learn.model(x)
‘’’

2 Likes

Shouldn’t FastAI automatically put the model on CUDA, when the dataloader is already on the gpu?
I don’t know exactly how cnn_learner is defined in FastAI (haven’t gone through the exact source code), but its just that model should be automatically be put on the device where dataloader is present, just like we used to do for single class classification problems on Bears, or Pets datasets?

2 Likes

The book was later fixed with this code:

x,y = to_cpu(dls.train.one_batch())    # notice to_cpu(...)!
activs = learn.model(x)
activs.shape
1 Like

Interesting. I think, the readers who run the book on a GPU server prefer learn.model.cuda()