转载请注明出处:小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你,欢迎[点赞、收藏、关注]哦~
目录
对数据类型有个大致的了解还是很必要的,不然可能会遇到莫名的错误,比如出现负数、报错等,但不知道原因。
数据类型 | 代码中的dtype表示 | 数据范围(仅供参考,可能有错,还是得按照后面的代码结果为准) |
---|---|---|
32 位浮点数 | torch.float32 or torch.float | [−3.4e38,3.4e38] |
64 位浮点数 | torch.float64 or torch.double | [−1.8e308,1.8e308] |
16 位浮点数 1 | torch.float16 or torch.half | [−6.5e4,6.5e4] |
16 位浮点数 2 | torch.bfloat16 | [−3.4e38,3.4e38] |
32 位复数 | torch.complex32 or torch.chalf | 16 位浮点实部和虚部组成 |
64 位复数 | torch.complex64 or torch.cfloat | 32 位浮点实部和虚部组成 |
128 位复数 | torch.complex128 or torch.cdouble | 64 位浮点实部和虚部组成 |
8 位整数(无符号) | torch.uint8 | 0,255 |
16 位整数(无符号)4 (受限支持) | torch.uint16 | 0,65535 |
32 位整数(无符号)4 (受限支持) | torch.uint32 | 0,4294967295 |
64 位整数(无符号)4 (受限支持) | torch.uint64 | 0,18446744073709551615 |
8 位整数(有符号) | torch.int8 | −128,127 |
16 位整数(有符号) | torch.int16 or torch.short | −32768,32767 |
32 位整数(有符号) | torch.int32 or torch.int | −2147483648,2147483647 |
64 位整数(有符号) | torch.int64 or torch.long | −9.2e18,9.2e18 |
布尔值 | torch.bool | {False, True} |
量化 8 位整数(无符号) | torch.quint8 | 0,255 |
量化 8 位整数(有符号) | torch.qint8 | −128,127 |
量化 32 位整数(有符号) | torch.qint32 | −2147483648,2147483647 |
量化 4 位整数(无符号)3 | torch.quint4x2 | 在 1 字节中编码 2 个 4 位值 |
8 位浮点数, e4m3 5 (受限支持) | torch.float8_e4m3fn | 依赖于具体实现 |
8 位浮点数t, e5m2 5 (受限支持) | torch.float8_e5m2 | 依赖于具体实现 |
import torch
int16_info = torch.iinfo(torch.int16)
print("int16的最小值:", int16_info.min)
print("int16的最大值:", int16_info.max)
float16_info = torch.finfo(torch.float16)
print("float16的最小值:", float16_info.min)
print("float16的最大值:", float16_info.max)
当创建一个 torch.tensor
而不指定数据类型(dtype
)时,默认的数据类型会跟你给的张量来确定。
这意味着,如果你直接创建一个浮点数张量而不指定 dtype
,它会自动成为 float32
类型。
对于整数类型,如果你创建一个整数张量且不指定 dtype
,它会默认为 torch.int64
。
import torch
# 创建一个浮点数张量,默认dtype为 torch.float32
float_tensor = torch.tensor([1.0, 2.0, 3.0])
print(float_tensor.dtype) # 输出:torch.float32
# 创建一个整数张量,默认dtype为 torch.int64
int_tensor = torch.tensor([1, 2, 3])
print(int_tensor.dtype) # 输出:torch.int64
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有