【发布时间】:2019-04-02 12:23:26
【问题描述】:
我想在批量标准化层的变量上添加条件操作。具体来说,在浮点数中训练,然后在微调二次训练阶段进行量化。为此,我想在变量上添加一个 tf.cond 操作(均值和 var 的 scale、shift 和 exp 移动平均值)。
我将 tf.layers.batch_normalization 替换为我编写的 batchnorm 层(见下文)。
此函数完美运行(即,我使用两个函数获得相同的指标),并且我可以将任何管道添加到变量(在 batchnorm 操作之前)。 问题在于性能(运行时)急剧下降(即,只需将 layers.batchnorm 替换为我自己的函数,就会产生 x2 因子,如下所述)。
def batchnorm(self, x, name, epsilon=0.001, decay=0.99):
epsilon = tf.to_float(epsilon)
decay = tf.to_float(decay)
with tf.variable_scope(name):
shape = x.get_shape().as_list()
channels_num = shape[3]
# scale factor
gamma = tf.get_variable("gamma", shape=[channels_num], initializer=tf.constant_initializer(1.0), trainable=True)
# shift value
beta = tf.get_variable("beta", shape=[channels_num], initializer=tf.constant_initializer(0.0), trainable=True)
moving_mean = tf.get_variable("moving_mean", channels_num, initializer=tf.constant_initializer(0.0), trainable=False)
moving_var = tf.get_variable("moving_var", channels_num, initializer=tf.constant_initializer(1.0), trainable=False)
batch_mean, batch_var = tf.nn.moments(x, axes=[0, 1, 2]) # per channel
update_mean = moving_mean.assign((decay * moving_mean) + ((1. - decay) * batch_mean))
update_var = moving_var.assign((decay * moving_var) + ((1. - decay) * batch_var))
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update_mean)
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update_var)
bn_mean = tf.cond(self.is_training, lambda: tf.identity(batch_mean), lambda: tf.identity(moving_mean))
bn_var = tf.cond(self.is_training, lambda: tf.identity(batch_var), lambda: tf.identity(moving_var))
with tf.variable_scope(name + "_batchnorm_op"):
inv = tf.math.rsqrt(bn_var + epsilon)
inv *= gamma
output = ((x*inv) - (bn_mean*inv)) + beta
return output
如果有以下任何问题,我将不胜感激:
- 关于如何提高我的解决方案的性能(减少运行时间)的任何想法?
- 是否可以在batchnorm操作之前将我自己的运算符添加到layers.batchnorm的变量管道中?
- 对于同一问题还有其他解决方案吗?
【问题讨论】:
标签: python tensorflow batch-normalization