import tensorflow as tf


    def __call__(self, x, update=False):
        with tf.variable_scope(self.name) as scope:
            if not update:
                new_coeff = 1. / (self.batch_size + 1.)
                old_coeff = 1. - new_coeff
                new_mean = tf.reduce_mean(x, [1, 2], keep_dims=True)
                new_mean_sq = tf.reduce_mean(tf.square(x), [1, 2], keep_dims=True)
                mean = new_coeff * new_mean + old_coeff * self.mean
                mean_sq = new_coeff * new_mean_sq + old_coeff * self.mean_sq
                out = tf.nn.relu(self._normalize(x, mean, mean_sq, "live"))
            # Update the mean and mean_sq when passing the reference data
            else:
                self.mean = tf.reduce_mean(x, [0, 1, 2], keep_dims=True)
                self.mean_sq = tf.reduce_mean(tf.square(x), [0, 1, 2], keep_dims=True)
                out = tf.nn.relu(self._normalize(x, self.mean, self.mean_sq, "reference"))
            return out

    def _normalize(self, x, mean, mean_sq, message):
        # make sure this is called with a variable scope
        shape = x.get_shape().as_list()
        assert len(shape) == 4
        self.gamma = safe_get("gamma", [shape[-1]],
                                initializer=tf.random_normal_initializer(1., 0.02))
        gamma = tf.reshape(self.gamma, [1, 1, 1, -1])
        self.beta = safe_get("beta", [shape[-1]],
                                initializer=tf.constant_initializer(0.))
        beta = tf.reshape(self.beta, [1, 1, 1, -1])
