pytorch-summary icon indicating copy to clipboard operation
pytorch-summary copied to clipboard

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

Open thinkerthinker opened this issue 6 years ago • 7 comments
trafficstars

why my input type is torch.FloatTensor? I can't find the reason......

thinkerthinker avatar Mar 12 '19 14:03 thinkerthinker

Your input tensor and model are on different devices (CPU and GPU). Try to set device="cpu" when calling summary(). See this issue #36.

elvisyjlin avatar Mar 12 '19 15:03 elvisyjlin

Thank you~ I found that the model I ran also defined torchsummary.py, the problem caused by the conflict between the two.

thinkerthinker avatar Mar 13 '19 01:03 thinkerthinker

What is about this one?

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

This does not seem like a device mismatch error.

sayakpaul avatar May 05 '19 04:05 sayakpaul

Declaring the model to the GPU solves the issue,

class Classifier(nn.Module):
    def __init__(self):
        super(Classifier, self).__init__()

        self.model = nn.Sequential(
            nn.Conv2d(1, 8, 3, 1), # (-1, 8, 26, 26)
            nn.LeakyReLU(0.2),
            nn.MaxPool2d(2), # (-1, 8, 13, 13)
            nn.Conv2d(8, 16, 3, 1), # (-1, 16, 11, 11)
            nn.LeakyReLU(0.2),
            nn.MaxPool2d(2),# (-1, 16, 5, 5)
            nn.Flatten(), # (-1, 16 * 5 * 5)
            nn.Dropout(0.5),
            nn.Linear(16 * 5 * 5, 10), # (-1, 10)
            nn.Softmax())
    
    def forward(self, x):
        return self.model(x)

classifier = Classifier().cuda() # without .cuda() summary gives "RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same"
summary(classifier, (1, 28, 28)) # now it works fine

But I don't really know why it should bother where our model is allocated unless we are calling the model and passing inputs. Any explanation would be appreciated. Also, it looks like summary only works for models allocated to GPU.

braindotai avatar Mar 12 '20 04:03 braindotai

both model and input ported on gpu using model.to(device) and image.to(device). Availability of gpu is also verified. Still getting the error. Kindly help if any solution

angy50 avatar Feb 21 '21 16:02 angy50

both model and input ported on gpu using model.to(device) and image.to(device). Availability of gpu is also verified. Still getting the error. Kindly help if any solution

Try passing device argument:

summary(classifier, (1, 28, 28), device = 'cuda')

Or try same with cpu as well...

braindotai avatar Feb 21 '21 16:02 braindotai

@braindotai img.type(torch.cuda.FloatTensor) saved me. Thanks

angy50 avatar Feb 21 '21 16:02 angy50