runtimeerror: expected scalar type double but found float

Reference is from this github issue.

When the error is RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #4 'mat1', you would need to use the .float() function since it says Expected object of scalar type Float.

Therefore, the solution is changing y_pred = model(X_trainTensor) to y_pred = model(X_trainTensor.float()).

Likewise, when you get another error for loss = loss_fn(y_pred, y_trainTensor), you need y_trainTensor.long() since the error message says Expected object of scalar type Long.

You could also do model.double(), as suggested by @Paddy .

3.8
5
Ars Skeptica 105 points

                                    RuntimeError: expected scalar type Long but found Float

3.8 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
RuntimeError: expected scalar type Long but found Double site:stackoverflow.com *** RuntimeError: expected scalar type Double but found Float RuntimeError: expected scalar type Float but found Byte RuntimeError: expected scalar type Float but found Double torch.mm Expected object of scalar type Double but got scalar type Float for argument #3 'mat1' in call to _th_addmm_ RuntimeError: expected scalar type Float but found Double nn.Linear "expected scalar type Float but found Long" expected scalar type Float but found Long Expected object of scalar type Double but got scalar type Float for argument #2 'mat2' in call to _th_mm torch RuntimeError: expected scalar type Float but found Double pytprch expected scalar type Float but found Double torch expected scalar type Double but found Float RuntimeError: expected scalar type Float but found Long RuntimeError: expected scalar type Double but found Float' expected scalar type but found float Expected object of scalar type Float but got scalar type Double for argument #2 'mat1' in call to _th_addmm RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'mat2' RuntimeError: expected scalar type Long but found Double RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 'mat1' in call to _th_addmm RuntimeError: expected scalar type float but found long int RuntimeError: expected scalar type Double but found Float site:stackoverflow.com expected scalar type Long but found Floatù RuntimeError: expected scalar type Float but found Double site:stackoverflow.com expected scalar type double but found float pytorch expected scalar type Double but found Float expected scalar type Long but found Float expected scalar type float but found double torch RuntimeError: expected scalar type Double but found Float RuntimeError: expected scalar type Float but found Double RuntimeError: expected scalar type Long but found Float RuntimeError: expected scalar type Double but found Long runtimeerror: expected scalar type double but found float
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source