RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #2 'weight', help me understand how and when to use cuda

Hi, I got the above error, and from the docs I’m not able to understand where should I adjust to cuda.
For example in the docs they are showing how to create a tensor on the gpu, but what with the case when I’m loading Images? still the same?

Now I have a situation my tensors are cuda type, but my layers not, and that’s why I’m getting :
"Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor ".
Now I need to put .cuda() on each layer?(or something like this) doesn’t make sense.

This is were the program stoped, in the nn.conv2d , tried to “.cuda()”
, didn’t help

             return nn.Sequential(
                nn.Conv2d(inp, oup, 3, stride, 1, bias=False).cuda(),
                nn.BatchNorm2d(oup),
                nn.ReLU(inplace=True)
            )

I tried few of the solutions online, but nothing worked.

Your data as well as your model should be both on the GPU.
To transfer both you can use something like this:

model = model.to('cuda')
data = data.to('cuda')
target = target.to('cuda')

You don’t need to push each layer onto the GPU. The model will push all it’s layers to the device.

3 Likes

I have already do this job, but it doesn’t work.It seems the first batch can work well while the next gets error

Could you post your code so that we could have a look?
The issue is strange, as the first batch seems to work.
If you can’t post code due to some restrictions, could you come up with a small dummy code snippet to reproduce this issue?

I found the bug when I was copying my code.this is my test data that not transfered lead to the error.
Anyway, thanks for your attention.:blush:

1 Like

could you please tell how to solve this problem?