PyTorch torch.quantized_max_pool2d 函数
torch.quantized_max_pool2d 是 PyTorch 中用于对量化张量执行二维最大池化操作的函数。该函数常用于量化卷积神经网络中进行空间下采样。
函数定义
torch.quantized_max_pool2d(input, kernel_size, stride, padding, dilation)
参数说明
input: 输入的量化张量(4D: batch x channel x height x width)kernel_size: 池化窗口大小stride: 步长(可选)padding: 填充(可选)dilation: 膨胀(可选)
使用示例
实例
import torch
# 创建量化输入张量 (batch=1, channel=1, height=4, width=4)
input = torch.quantize_per_tensor(torch.randn(1, 1, 4, 4), scale=0.1, zero_point=0, dtype=torch.quint8)
# 执行量化最大池化 (2x2 池化窗口)
output = torch.quantized_max_pool2d(input, kernel_size=2, stride=2)
print("输入形状:", input.shape)
print("输出形状:", output.shape)
# 创建量化输入张量 (batch=1, channel=1, height=4, width=4)
input = torch.quantize_per_tensor(torch.randn(1, 1, 4, 4), scale=0.1, zero_point=0, dtype=torch.quint8)
# 执行量化最大池化 (2x2 池化窗口)
output = torch.quantized_max_pool2d(input, kernel_size=2, stride=2)
print("输入形状:", input.shape)
print("输出形状:", output.shape)
输出结果为:
输入形状: torch.Size([1, 1, 4, 4]) 输出形状: torch.Size([1, 1, 2, 2])

Pytorch torch 参考手册