Expected isFloatingType(grads[i].type().scalarType())

while i was running gradient algorithm on this code:

I got this error:

RuntimeError: Expected isFloatingType(grads[i].type().scalarType()) to be true, but got false. 

I have no idea how to correct it
Could someone please give me a help??

Did you check the type of your gradients? My guess is that they are doubles. If that is the case, then you can cast your model as torch.float() to get the grads as the correct type.

thanks @sdv4 for your response
but what you mean exactly by saying check your gradients?
could you explain more please?

When you are training your model, after you call loss.backwards() , but before you call optimizer.step(), you can inspect the gradients of any parameter of the model that you are interested in.

You can do this by calling model.named_parameters(). This creates an iterator that you can go through to find any of the model’s parameters that you would like to look at. Once you have the tensor containing model parameters, you call .grad to view the gradient. By looking at the type of the output of this call, you can see if your gradients are doubles or floats.

Alternatively, you can just call model = model.float() on your model before training, but this might raise other errors if your data is of the double type.

1 Like

Ow, Got it…thanks for the clarification @sdv4
I will check it out and will share the results:relaxed:

Hi, have you solved this problem?

Hi @Yanxian_Chen …yeah, i did

@ati how did you solve it? I am facing the same issue, but all tensors I checked are float32.

I had similar problem. I was using pytorch 1.4 with cuda 10.1. I just switched back to pytorch 1.2 with cuda 10.0 and it was solved.