PyTorch torch.quantize_per_tensor 函数
torch.quantize_per_tensor 是 PyTorch 中用于创建按张量(per-tensor)量化的量化张量的函数。
函数定义
torch.quantize_per_tensor(input, scale, zero_point, dtype)
使用示例
实例
import torch
# 创建输入张量
input = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
# 按张量量化
# scale: 量化缩放因子
# zero_point: 零点
# dtype: 量化数据类型
x = torch.quantize_per_tensor(input, scale=0.1, zero_point=10, dtype=torch.quint8)
print("量化张量:")
print(x)
print("原始值反量化:")
print(x.dequantize())
# 创建输入张量
input = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
# 按张量量化
# scale: 量化缩放因子
# zero_point: 零点
# dtype: 量化数据类型
x = torch.quantize_per_tensor(input, scale=0.1, zero_point=10, dtype=torch.quint8)
print("量化张量:")
print(x)
print("原始值反量化:")
print(x.dequantize())

Pytorch torch 参考手册