Project scenario:
Today, I saw the code of an RNN instance. I want to try to pass in RNN with my own data, but I can report an error.
Problem Description:
The error is runtimeerror: expected scalar type long but found float
Cause analysis:
The wrong input is as follows:
input=torch.tensor([ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0])
The input in the example is as follows:
input=torch.tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0.]])
The reason is that I set dtype = torch.long when generating input
Solution:
Input = torch. Tensor (input, dtype = torch. Float)
specify dtype = torch. Float when generating the input tensor, and the resulting input will be of the following types
input=torch.tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0.]])
If it meets the requirements, no error will be reported
I didn’t expect to solve this problem. I spent most of the night. I really haven’t started yet. I don’t have enough skills.
Read More:
- [Solved] RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat1‘
- Successfully solved the problem of “runtimee” in RESNET dataset classification rror:expected scalar type Long but found Float”
- RuntimeError: expected scalar type Double but found Float
- RuntimeError: Found dtype Double but expected Float”
- RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor
- Expected STR instance, float found
- RuntimeError: cudnn RNN backward can only be called in training mode
- Expected indentation of 6 spaces but found 10
- RuntimeError: log_vml_cpu not implemented for ‘Long’
- error Expected indentation of 2 spaces but found 8
- Type error: sequence item 0: expected STR instance, int found
- Python: How to Fix “Ord() expected string of length 1, but int found”
- Can’t multiply sequence by non int of type ‘float’
- An error was reported on the command line of Vue when it first wrote a small project: expected indentation of 4 spaces but found 6
- RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- [solved] error: valueerror: expected 2D array, got scalar array instead
- Python learning notes (5) — cross entropy error runtimeerror: 1D target tensor expected, multi target not supported
- RuntimeError: reciprocal is not implemented for type torch.cuda.LongTensor
- Java long type error: error: integer number too large
- [Solved] Scala signature package has wrong version expected: 5.0 found: 5.2 in package.class