PyTorch torch.quantized_batch_norm 関数


Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

torch.quantized_batch_normPyTorchにおいて、量子化テンソルに対してバッチ正規化を実行するための関数です。この関数は量子化モデルの推論時に非常に有用で、量子化の利点を維持しながらバッチ正規化操作を実行できます。

関数定義

torch.quantized_batch_norm(input, weight, bias, mean, var, eps, output_scale, output_zero_point)

パラメータ説明

  • input: 入力の量子化テンソル
  • weight: バッチ正規化のスケールパラメータ
  • bias: バッチ正規化のバイアスパラメータ
  • mean: バッチ正規化の平均
  • var: バッチ正規化の分散
  • eps: ゼロ除算を防ぐための小さな定数
  • output_scale: 出力テンソルの量子化スケール
  • output_zero_point: 出力テンソルの量子化ゼロポイント

使用例

実例

import torch

# 量子化入力テンソルを作成
input = torch.quantize_per_tensor(torch.randn(1, 3, 4, 4), scale=0.1, zero_point=0, dtype=torch.quint8)

# バッチ正規化パラメータ
weight = torch.ones(3)
bias = torch.zeros(3)
mean = torch.ones(3) * 0.5
var = torch.ones(3) * 0.2

# 量子化バッチ正規化を実行
output = torch.quantized_batch_norm(
    input, weight, bias, mean, var,
    eps=1e-5, output_scale=0.1, output_zero_point=0
)

print("出力形状:", output.shape)
print("出力型:", output.dtype)

出力結果は次のとおりです:

输出形状: torch.Size([1, 3, 4, 4])
输出类型: torch.quint8

Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

その他の拡張