QKeras/Python中的round through函数是什么?
What is the round through function in QKeras/Python?
我正在研究 'quantized_bits' class 的 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()' 函数的结果
所以我的问题是:这个函数有什么作用?它属于哪个模块?
Link 代码:https://github.com/google/qkeras/blob/master/qkeras/quantizers.py#L489
如有任何帮助,我们将不胜感激!
如果你点击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](
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
我正在研究 'quantized_bits' class 的 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()' 函数的结果所以我的问题是:这个函数有什么作用?它属于哪个模块?
Link 代码:https://github.com/google/qkeras/blob/master/qkeras/quantizers.py#L489
如有任何帮助,我们将不胜感激!
如果你点击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](
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