PyTorch torch.fake_quantize_per_channel_affine 関数
torch.fake_quantize_per_channel_affinePyTorch でテンソルに対してチャンネル単位のフェイク量子化(Fake Quantization)を行うための関数であり、量子化訓練でよく使用されます。
関数定義
torch.fake_quantize_per_channel_affine(input, scale, zero_point, axis, quant_min, quant_max)
使用例
例
import torch
# 入力テンソルを作成
x = torch.randn(2, 3, 4, 5)
# スケールファクターとゼロポイントを定義
scale = torch.tensor([1.0, 1.2, 1.5])
zero_point = torch.tensor([0, 0, 0], dtype=torch.long)
# チャンネル単位のフェイク量子化を実行
y = torch.fake_quantize_per_channel_affine(x, scale, zero_point, axis=1, quant_min=0, quant_max=255)
print("量子化後の形状:", y.shape)
# 入力テンソルを作成
x = torch.randn(2, 3, 4, 5)
# スケールファクターとゼロポイントを定義
scale = torch.tensor([1.0, 1.2, 1.5])
zero_point = torch.tensor([0, 0, 0], dtype=torch.long)
# チャンネル単位のフェイク量子化を実行
y = torch.fake_quantize_per_channel_affine(x, scale, zero_point, axis=1, quant_min=0, quant_max=255)
print("量子化後の形状:", y.shape)
その他の拡張
Pytorch torch リファレンスマニュアル