微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么tf.contrib.layers.instance_norm图层包含StopGradient操作?

如何解决为什么tf.contrib.layers.instance_norm图层包含StopGradient操作?

为什么tf.contrib.layers.instance_norm层包含StopGradient操作?即为什么需要它?

enter image description here

似乎在更简单的StopGradient层中也有tf.nn.moments(这可能是tf.contrib.layers.instance_norm的构建块)。

x_m,x_v = tf.nn.moments(x,[1,2],keep_dims=True)

enter image description here

我也在StopGradient代码中找到了关于tf.nn.moments的注释:

# The dynamic range of fp16 is too limited to support the collection of
# sufficient statistics. As a workaround we simply perform the operations
# on 32-bit floats before converting the mean and variance back to fp16
y = math_ops.cast(x,dtypes.float32) if x.dtype == dtypes.float16 else x
# Compute true mean while keeping the dims for proper broadcasting.
mean = math_ops.reduce_mean(y,axes,keepdims=True,name="mean")
# sample variance,not unbiased variance
# Note: stop_gradient does not change the gradient that gets
#       backpropagated to the mean from the variance calculation,#       because that gradient is zero
variance = math_ops.reduce_mean(
    math_ops.squared_difference(y,array_ops.stop_gradient(mean)),name="variance")

这是一种优化,因为梯度始终为零?

解决方法

试图回答。

此设计告诉我们,将第二矩最小化,我们将不希望通过第一矩传播梯度。是否有意义?如果我们尝试最小化E[x^2]-E[x]^2,我们将最小化E[x^2]同时最大化E[x]^2。第一项会减少每个元素的绝对值(将它们拖到中心)。第二项会按梯度增加所有值,这不会使方差最小化,但可能会对其他梯度路径产生负面影响。

因此,我们不会在第一时刻传播第二时刻的梯度,因为至少在使用普通SGD时,该梯度不会影响第二时刻。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。