Tensorflow Qunatization-零点是什么意思

时间:2019-10-24 08:25:56

标签: tensorflow quantization

我试图了解张量流中的量化,并且正在关注本教程。

https://heartbeat.fritz.ai/8-bit-quantization-and-tensorflow-lite-speeding-up-mobile-inference-with-low-precision-a882dfcafbbd

在本教程中说,量化方程为:

enter image description here

  • r是实际值(通常是float32)
  • q是B位整数(uint8,uint32等)的量化表示形式。
  • S(float32)和z(uint)是我们缩放和移动数字线的因素。 z是量化的“零点”,它将始终精确地映射回0.f。

我正在努力理解零点的含义,希望有人可以举例说明吗?

1 个答案:

答案 0 :(得分:2)

如果值带有负数据,则零点可能会偏移范围。因此,如果您的零点为128,则负值-128至-1将由0至127表示,而正值0至127将由128至255表示。

给出一个输入张量,其数据范围为-1000到+1000,并且元素值为39.215686275:

realValue = 39.215686275
zeroPoint = 128   // 256/2, which is symmetric
realRangeMinValue = -1000
realRangeMaxValue = 1000
integerRangeMinValue = 0
integerRangeMaxValue = 2 ^ 8 - 1 = 255
quantizedRangeMinValue = integerRangeMinValue - zeroPoint = -128
quantizedRangeMaxValue = integerRangeMaxValue - zeroPoint = 127

scale = integerRangeMaxValue / (realRangeMaxValue - -realRangeMinValue) = 0.1275
// scale = 255 / (1000 - -1000)
quantizedValue = realValue * 255 / (1000 - -1000) + 128 = 133
// quantizedValue = 39.215686275 * 255 / (1000 - -1000) + 128 = 133

相反:

realValue = (1000 − -1000) / 255 * (133 - 128) = 39.215686275