【问题标题】:What is the round through function in QKeras/Python?QKeras/Python 中的轮回函数是什么?
【发布时间】:2020-11-12 09:26:44
【问题描述】:

我正在查看“quantized_bits”类的 QKeras 实现。在 call 函数中,我遇到了一个 '_round_through' 函数。

以下是函数的调用方式:

if unsigned_bits > 0:
      p = x * m / m_i
      xq = m_i * tf.keras.backend.clip(
          _round_through(p, self.use_stochastic_rounding, precision=1.0),
          self.keep_negative  * (-m + self.symmetric), m - 1) / m

我尝试在 Python 中运行代码,但它显然不是内置函数。 我尝试搜索该函数,但在 Python 中只得到了有关“round()”函数的结果。

所以我的问题是:这个函数有什么作用?它属于哪个模块?

代码链接:https://github.com/google/qkeras/blob/master/qkeras/quantizers.py#L489

任何帮助将不胜感激!

【问题讨论】:

  • 请避免使用屏幕截图,而是直接将代码粘贴到您的问题中

标签: python tensorflow rounding quantization


【解决方案1】:

如果你点击 GitHub 中的函数,它会告诉你这个函数是从哪里来的。在这种情况下,the function is defined at line 271

def _round_through(x, use_stochastic_rounding=False, precision=0.5):
  """Rounds x but using straight through estimator.
  We use the trick from [Sergey Ioffe](http://stackoverflow.com/a/36480182).
  Straight through estimator is a biased estimator for the rounding
  operation defined by Hinton"s Coursera Lecture 9c where dL/dx is made
  equal to dL/dy for y = f(x) during gradient computation, where f(x) is
  a non-derivable function. In that case, we assume df/dx = 1 in:
  dL   dL df   dL
  -- = -- -- = --
  dx   df dx   dy
  (https://www.youtube.com/watch?v=LN0xtUuJsEI&list=PLoRl3Ht4JOcdU872GhiYWf6jwrk_SNhz9&index=41)
  Arguments:
    x: tensor to perform round operation with straight through gradient.
    use_stochastic_rounding: if true, we perform stochastic rounding.
    precision: by default we will use 0.5 as precision, but that can overriden
      by the user.
  Returns:
    Rounded tensor.
  """
  if use_stochastic_rounding:
    output = tf_utils.smart_cond(
        K.learning_phase(),
        lambda: x + tf.stop_gradient(-x + stochastic_round(x, precision)),
        lambda: x + tf.stop_gradient(-x + tf.round(x)))
  else:
    output = x + tf.stop_gradient(-x + tf.round(x))
  return output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-23
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多