change tf.contrib.layers.xavier_initializer() to 2.0.0

Viewed 7538

how can I change

tf.contrib.layers.xavier_initializer()

to tf version >= 2.0.0 ??

all codes:

W1 = tf.get_variable("W1", shape=[self.input_size, h_size],
                             initializer=tf.contrib.layers.xavier_initializer())
2 Answers

the TF2 replacement for tf.contrib.layers.xavier_initializer() is tf.keras.initializers.glorot_normal(Xavier and Glorot are 2 names for the same initializer algorithm) documentation link.

if dtype is important for some compatibility reasons - use tf.compat.v1.keras.initializers.glorot_normal

Just to slightly clarify @poe-dator answer's: Using TF slim's tf.contrib.layers.xavier_initializer() without any parameters, returns uniformly distributed weights (uniform=True set by default).

So basically, the mapping between TF Slim and Keras works as follows:

  • tf.contrib.layers.xavier_initializer() should be replaced by tf.keras.initializers.GlorotUniform()
  • tf.contrib.layers.xavier_initializer(uniform=False) should be replaced by tf.keras.initializers.GlorotNormal()

This difference is also noted in another Stack Overflow post, so kudos to the posters there.

Related