torch.set_default_dtype(d, /)

torch.set_default_dtype(d, /)

  • d: 要设置的数据类型

将默认浮点数据类型设置为 d。PyTorch 初始默认浮点数据类型为torch.float32

  • dtorch.float16torch.float32torch.float64
  • 当默认浮点数据类型为torch.float16时,默认复数数据类型为 torch.complex32
  • 当默认浮点数据类型为torch.float32时,默认复数数据类型为 torch.complex64
  • 当默认浮点数据类型为torch.float64时,默认复数数据类型为 torch.complex128
  • 当默认浮点数据类型为torch.bfloat16时,没有对应的默认复数数据类型。会引发异常
import torch
print(torch.tensor([1.2, 3]).dtype) # pytorch默认使用float32精度
print(torch.tensor([1.2, 3j]).dtype)# 复数默认使用complex64精度
import torch
torch.set_default_dtype(torch.float16)
print(torch.tensor([1.2, 3]).dtype) # 设置默认浮点精度为float16后,tensor的默认精度为float16
print(torch.tensor([1.2, 3j]).dtype)# 此时复数默认使用complex32精度
import torch
torch.set_default_dtype(torch.float64)
print(torch.tensor([1.2, 3]).dtype) # 设置默认浮点精度为float64后,tensor的默认精度为float64
print(torch.tensor([1.2, 3j]).dtype)# 此时复数默认使用complex128精度
import torch
torch.set_default_dtype(torch.bfloat16) # 设置默认浮点精度为bfloat16,会出现错误,因为bfloat16不支持复数
# 错误:RuntimeError: invalid default scalar type for complex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值