PyTorch torch.quantize_per_channel function
PyTorch torch reference manual
torch.quantize_per_channel is a function in PyTorch used to create a quantized tensor with per-channel quantization.
Function definition
torch.quantize_per_channel(input, scales, zero_points, axis, dtype)
Usage examples
Example
import
torch
Create an input tensor (2D)
input
=
torch.
tensor
(
[
[
1.0
,
0.0
]
,
[
1.0
,
2.0
]
]
)
Quantize per channel
scales: Scaling factor for each channel
zero_points: Zero point for each channel
axis: Specifies which dimension to quantize along
scales
=
torch.
tensor
(
[
0.1
,
0.2
]
)
zero_points
=
torch.
tensor
(
[
10
,
10
]
)
axis
=
0
x
=
torch.
quantize_per_channel
(
input
,
scales
,
zero_points
,
axis
,
dtype
=
torch.
quint8
)
(
"Quantized tensor:"
)
(
x
)
(
"Dequantized original values:"
)
(
x.
dequantize
(
)
)