RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor

Pytorch use error

Cause analysis and solution of error content

Error content

RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor

Cause analysis

Because I use a higher version of torch 1.7.1, this error is caused by the upgrade of torch 1.5 or above bilstm.

Solution

    go to the CPU to run the code and no error will be reported! Convert the error location parameter lengths to CPU type:

    lengths.to("cpu")
    

    It can be solved!

    Reference: link


Read More: