(x, n_out, phase_train, scope='bn', decay=0.9, eps=1e-5)
return normed
Code taken from http://stackoverflow.com/a/34634291/2267819
Code taken from http://stackoverflow.com/a/34634291/2267819
Code taken from http : // stackoverflow . com / a / 34634291 / 2267819
def batch_norm(x, n_out, phase_train, scope='bn', decay=0.9, eps=1e-5):
    """
    Code taken from http://stackoverflow.com/a/34634291/2267819
    """
    with tf.variable_scope(scope):
        beta = tf.get_variable(name='beta', shape=[n_out], initializer=tf.constant_initializer(0.0)
                               , trainable=True)
        gamma = tf.get_variable(name='gamma', shape=[n_out], initializer=tf.random_normal_initializer(1.0, 0.02),
                                trainable=True)
        batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2], name='moments')
        ema = tf.train.ExponentialMovingAverage(decay=decay)

        def mean_var_with_update():
            ema_apply_op = ema.apply([batch_mean, batch_var])
            with tf.control_dependencies([ema_apply_op]):
                return tf.identity(batch_mean), tf.identity(batch_var)

        mean, var = tf.cond(phase_train,
                            mean_var_with_update,
                            lambda: (ema.average(batch_mean), ema.average(batch_var)))
        normed = tf.nn.batch_normalization(x, mean, var, beta, gamma, eps)
    return normed
def batch_norm ( x , n_out , phase_train , scope = 'bn' , decay = 0.9 , eps = 1e-5 ) : with tf . variable_scope ( scope ) : beta = tf . get_variable ( name = 'beta' , shape = [ n_out ] , initializer = tf . constant_initializer ( 0.0 ) , trainable = True ) gamma = tf . get_variable ( name = 'gamma' , shape = [ n_out ] , initializer = tf . random_normal_initializer ( 1.0 , 0.02 ) , trainable = True ) batch_mean , batch_var = tf . nn . moments ( x , [ 0 , 1 , 2 ] , name = 'moments' ) ema = tf . train . ExponentialMovingAverage ( decay = decay ) def mean_var_with_update ( ) : ema_apply_op = ema . apply ( [ batch_mean , batch_var ] ) with tf . control_dependencies ( [ ema_apply_op ] ) : return tf . identity ( batch_mean ) , tf . identity ( batch_var ) mean , var = tf . cond ( phase_train , mean_var_with_update , lambda : ( ema . average ( batch_mean ) , ema . average ( batch_var ) ) ) normed = tf . nn . batch_normalization ( x , mean , var , beta , gamma , eps ) return normed