A similar unanswered question was asked here.
I am testing one deep reinforcement learning algorithm which uses keras backend in tensorflow. I am not very familiar with tf.keras, nevertheless would like to add batch normalization layers. Therefore, I am trying to use tf.keras.layers.BatchNormalization()
, but it does not update average means and variances because update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
is empty.
Using the regular tf.layers.batch_normalization
seem to work fine. However, because the complete algorithm is somewhat complicated, I need to find a way to use tf.keras
.
A standard tf
layer batch_normed = tf.layers.batch_normalization(hidden, training=True)
updates the averages since update_ops
is not empty:
[
<tf.Operation 'batch_normalization/AssignMovingAvg' type=AssignSub>,
<tf.Operation 'batch_normalization/AssignMovingAvg_1' type=AssignSub>,
<tf.Operation 'batch_normalization_1/AssignMovingAvg' type=AssignSub>,
<tf.Operation 'batch_normalization_1/AssignMovingAvg_1' type=AssignSub>
]
Minimal example that does not work:
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
graph = tf.get_default_graph()
tf.keras.backend.set_learning_phase(True)
input_shapes = [(3, )]
hidden_layer_sizes = [16, 16]
inputs = [
tf.keras.layers.Input(shape=input_shape)
for input_shape in input_shapes
]
concatenated = tf.keras.layers.Lambda(
lambda x: tf.concat(x, axis=-1)
)(inputs)
out = concatenated
for units in hidden_layer_sizes:
hidden = tf.keras.layers.Dense(
units, activation=None
)(out)
batch_normed = tf.keras.layers.BatchNormalization()(hidden, training=True)
#batch_normed = tf.layers.batch_normalization(hidden, training=True)
out = tf.keras.layers.Activation('relu')(batch_normed)
out = tf.keras.layers.Dense(
units=1, activation='linear'
)(out)
data = np.random.rand(100,3)
with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer())
for i in range(10):
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
sess.run(update_ops, {inputs[0]: data})
sess.run(out, {inputs[0]: data})
variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
scope='batch_normalization')
bn_gamma, bn_beta, bn_moving_mean, bn_moving_variance = [], [], [], []
for variable in variables:
val = sess.run(variable)
nv = np.linalg.norm(val)
if 'gamma' in variable.name:
bn_gamma.append(nv)
if 'beta' in variable.name:
bn_beta.append(nv)
if 'moving_mean' in variable.name:
bn_moving_mean.append(nv)
if 'moving_variance' in variable.name:
bn_moving_variance.append(nv)
diagnostics = {
'bn_Q_gamma': np.mean(bn_gamma),
'bn_Q_beta': np.mean(bn_beta),
'bn_Q_moving_mean': np.mean(bn_moving_mean),
'bn_Q_moving_variance': np.mean(bn_moving_variance),
}
print(diagnostics)
The output is the following (you can see moving_mean and moving_variance not changing):
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0, 'bn_Q_moving_variance': 4.0}
While the expected output is something like the following (comment the line with batch_normed
calculus using tf.keras
and uncomment the one below it):
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0148749575, 'bn_Q_moving_variance': 3.966927}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.029601166, 'bn_Q_moving_variance': 3.934192}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.04418011, 'bn_Q_moving_variance': 3.9017918}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.05861327, 'bn_Q_moving_variance': 3.8697228}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.0729021, 'bn_Q_moving_variance': 3.8379822}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.08704803, 'bn_Q_moving_variance': 3.8065662}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.10105251, 'bn_Q_moving_variance': 3.7754717}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.11491694, 'bn_Q_moving_variance': 3.7446957}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.12864274, 'bn_Q_moving_variance': 3.7142346}
{'bn_Q_gamma': 4.0, 'bn_Q_beta': 0.0, 'bn_Q_moving_mean': 0.14223127, 'bn_Q_moving_variance': 3.6840856}
There is still something fishy even with tf.layers.batch_normalization
. The standard tf
approach of tf.control_dependencies
:
with tf.control_dependencies(update_ops):
sess.run(out, {inputs[0]: data})
which I place instead of the following two lines in the code above:
sess.run(update_ops, {inputs[0]: data})
sess.run(out, {inputs[0]: data})
produces bn_Q_moving_mean = 0.0
and bn_Q_moving_variance = 4.0
This is because tf.keras.layers.BatchNormalization
inherits from tf.keras.layers.Layer
. Keras API handle update ops as part of its fit and evaluate loops. This in turn means that it won't update tf.GraphKeys.UPDATE_OPS
collection without it.
So in order to make it work, you need to update it manually
hidden = tf.keras.layers.Dense(units, activation=None)(out)
batch_normed = tf.keras.layers.BatchNormalization(trainable=True)
layer = batch_normed(hidden)
This creates separate class instance
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, batch_normed.updates)
And this updates needed collection. Also take a look https://github.com/tensorflow/tensorflow/issues/25525
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, bn1.updates[0])
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, bn1.updates[1])
updates_op = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
this can solve
tf.control_dependencies(update_ops)
error problem.
if use
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, batch_normed.updates)
the return of
tf.get_collection(tf.GraphKeys.UPDATE_OPS)
is a list in list just like [[something]]
and use
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, bn1.updates[0])
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, bn1.updates[1])
updates_op = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
the return of
tf.get_collection(tf.GraphKeys.UPDATE_OPS)
is [something1,something2,...]
i thinks this is the solution.
but the out put is different,and i don't know which is true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With