bla*_*eep 4 quantization tensorflow
我正在尝试了解张量流中的量化,并且正在遵循本教程。
\n\n\n\n在教程中它说,量化方程是:
\n\n\n\n我正在努力理解零点的含义,希望有人能用一个例子来解释它?
\nDwa*_*son 12
如果原始数据具有负值,则零点可以抵消范围,从而允许您将它们存储在无符号整数中。因此,如果您的零点是 128,则未缩放的负值 -127 到 -1 可以用 1 到 127 表示,正值 0 到 127 可以用 128 到 255 表示。注意量化值 0(或输入 -128此处)有意用于保持两端的范围对称,因此总范围为 256-1 个值而不是 256。然后可以使用比例因子映射更大的实际值。
例如,给定一个数据范围从 -1000 到 +1000(包括两端)的输入张量,以及其中一个值为 39.215686275 的元素,当使用 128 作为零点时,该元素的量化值为 133:
quantizedValue = round(realValue / scale + quantizedZeroPoint)
## round(39.215686275 / 7.843137255 + 128 ) = 133
## round(0 / 7.843137255 + 128 ) = 128
## round(1000 / 7.843137255 + 128 ) = 255
## round(-1000 / 7.843137255 + 128 ) = 1
Run Code Online (Sandbox Code Playgroud)
使用:
## Determine the maximum range of the data (either known or via reduce min/max):
realRangeMinValue = -1000
realRangeMaxValue = 1000
## Use a zero point halfway between the quantized data type range.
## Note 256/2 is symmetric, with 127 on either side, as q=0 isn't really used.
quantizedRange = 2^8 - 1 = 255
quantizedRangeMinValue = 1
quantizedRangeMaxValue = 255
quantizedZeroPoint = 128
## Precompute scale, using the same zero point and scale for the entire tensor.
scale = (realRangeMaxValue - realRangeMinValue) / quantizedRange
## = (1000 - -1000 ) / 255
## = 7.843137255
##
## Alternately, you could precompute the inverse scale and multiply rather than divide
## (inverseScale = 0.1275).
Run Code Online (Sandbox Code Playgroud)
相反,从量化值到实数:
realValue = (quantizedValue - quantizedZeroPoint) * scale
## (133 - 128 ) * 7.843137255 = 39.215686275
## (128 - 128 ) * 7.843137255 = 0
## (255 - 128 ) * 7.843137255 = 1000
## (1 - 128 ) * 7.843137255 = -1000
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4424 次 |
| 最近记录: |