How to casting cuda tensor types

Hey guys! Could you all help me with this issue?

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

I am using this function to initialize the weights:

def weights_init(model):
    if type(model) in [nn.Conv2d, nn.Linear]:
        nn.init.constant_(, 0.1)

And I am using this code to submit an image to the CNN layers:

for inputs, labels in images_loader:    
    inputs = 
    labels = 
    inputs = inputs.unsqueeze(0)    

    outputs = model(inputs)

Best regards,

Matheus Santos.

As the error says, your inputs (likely) are double (aka float64) while your weights are float32 (which is the standard in GPU computing).

inputs =, dtype=torch.float32)

will help, but you could also see whether you want to provide the data as 32 bit floats to start with.

Best regards


1 Like

Hey, thanks!
Solved the problem, but I got another one:

“conv_transpose2d(): argument ‘stride’ must be tuple of ints, but found element of type float at pos 1”

Is this because the tensor elements are in float type?
The configuration of my CNN layers is in this layout:

# conv1
          nn.Conv2d(in_channels = 1, out_channels = 64, kernel_size = 3, stride = 1, padding = 1, dilation = 1),
          nn.Conv2d(in_channels = 64, out_channels = 64, kernel_size = 3, stride = 2, padding = 1, dilation = 1),
          nn.BatchNorm2d(num_features = 64),


I solved here ahahahaha
I was passing stride with float value :sweat_smile:
Thanks for the help :smiley:

1 Like