import tensorflow as tf

    def _bn(self, name, x):
        with tf.variable_scope(name):
            moving_average_decay = 0.9
            decay = moving_average_decay

            batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2])

            mu = tf.get_variable('mu', batch_mean.shape, dtype=tf.float32,
                                 initializer=tf.zeros_initializer(), trainable=False)
            tf.add_to_collection(tf.GraphKeys.GLOBAL_VARIABLES, mu)
            tf.add_to_collection('mu_sigma_bn', mu)
            sigma = tf.get_variable('sigma', batch_var.shape, dtype=tf.float32,
                                    initializer=tf.ones_initializer(), trainable=False)
            tf.add_to_collection(tf.GraphKeys.GLOBAL_VARIABLES, sigma)
            tf.add_to_collection('mu_sigma_bn', sigma)
            beta = tf.get_variable('beta', batch_mean.shape, dtype=tf.float32,
                                   initializer=tf.zeros_initializer())
            gamma = tf.get_variable('gamma', batch_var.shape, dtype=tf.float32,
                                    initializer=tf.ones_initializer())

            # BN when training
            update = 1.0 - decay
            update_mu = mu.assign_sub(update * (mu - batch_mean))
            update_sigma = sigma.assign_sub(update * (sigma - batch_var))
            tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update_mu)
            tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update_sigma)

            mean, var = tf.cond(self.train_flag, lambda: (batch_mean, batch_var), lambda: (mu, sigma))
