tensor类型转换
int -> float
torch.tensor(x, dtype=数据格式)
import torch
a = [1, 2, 3, 4]
a1 = torch.tensor(a)
a_float = torch.tensor(a1, dtype=torch.float32)
a_int64 = torch.tensor(a1, dtype=torch.int64)
print(a1.dtype)
print(a_float.dtype)
print(a_int64.dtype)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
torch.int64
torch.float32
torch.int64
1
2
3
2
3
References
-PyTorch 学习笔记——Tensor 张量的数据类型的转化、Tensor 常见的数据类型、快速创建 Tensor (opens new window)
list 转化为 torch.Tensor 类型
上次更新: 2025/04/02, 12:03:38