expected scalar type Long but found Float
时间: 2023-10-13 10:22:20 浏览: 95
This error message occurs when there is a type mismatch between the expected data type and the actual data type in a PyTorch tensor.
In this case, the code is expecting a Long data type but found a Float data type. To fix this error, you need to convert the Float tensor to a Long tensor using the `long()` method.
For example:
```python
import torch
# create a Float tensor
float_tensor = torch.tensor([1.0, 2.0, 3.0])
# convert the Float tensor to a Long tensor
long_tensor = float_tensor.long()
# print the Long tensor
print(long_tensor)
```
Output:
```
tensor([ 1, 2, 3])
```
Alternatively, you can also specify the data type when creating the tensor:
```python
import torch
# create a Long tensor
long_tensor = torch.tensor([1, 2, 3], dtype=torch.long)
# print the Long tensor
print(long_tensor)
```
Output:
```
tensor([ 1, 2, 3])
```
阅读全文