【知识】PyTorch中的数据类型dtype

转载请注明出处:小锋学长生活大爆炸[xfxuezhagn.cn]

如果本文帮助到了你,欢迎[点赞、收藏、关注]哦~

目录

类型概括

代码中查看范围

默认数据类型


        对数据类型有个大致的了解还是很必要的,不然可能会遇到莫名的错误,比如出现负数、报错等,但不知道原因。

类型概括

torch.Tensor — PyTorch 2.4 documentation

数据类型

代码中的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

依赖于具体实现
  • 1:有时也称为 binary16:使用 1 个符号位、5 个指数位和 10 个符号位。当精度很重要而牺牲范围时很有用。
  • 2:有时也称为Brain Floating Point:使用 1 个符号、8 个指数位和 7 个符号位。由于它的指数位数与 float32 相同,因此在范围很重要的情况下非常有用。
  • 3:量化后的 4 位整数存储为 8 位带符号整数。目前只有 EmbeddingBag 运算符支持。
  • 4(1,2,3):除 uint8 之外的无符号类型目前计划只在急切模式下提供有限的支持(它们的存在主要是为了协助使用 torch.compile);如果您需要急切支持且不需要额外的范围,我们建议您使用它们的有符号变体。更多详情,请参见 https://github.com/pytorch/pytorch/issues/58734。
  • 5(1,2):torch.float8_e4m3fn 和 torch.float8_e5m2 实现了 https://arxiv.org/abs/2209.05433 中的 8 位浮点类型规范。对运算符的支持非常有限。

代码中查看范围

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

  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小锋学长生活大爆炸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值