简体   繁体   English

将 tf.contrib.layers.xavier_initializer() 更改为 2.0.0

[英]change tf.contrib.layers.xavier_initializer() to 2.0.0

how can I change我该如何改变

tf.contrib.layers.xavier_initializer()

to tf version >= 2.0.0 ??到 tf 版本 >= 2.0.0 ??

all codes:所有代码:

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

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 . tf.contrib.layers.xavier_initializer()的 TF2 替换是tf.keras.initializers.glorot_normal (Xavier 和 Glorot 是同一个初始化算法的两个名称) 文档链接

if dtype is important for some compatibility reasons - use tf.compat.v1.keras.initializers.glorot_normal如果tf.compat.v1.keras.initializers.glorot_normal由于某些兼容性原因很重要 - 使用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 ).只是为了稍微澄清@poe-dator的答案:使用 TF slim 的tf.contrib.layers.xavier_initializer()不带任何参数,返回均匀分布的权重( 默认设置为uniform=True )。

So basically, the mapping between TF Slim and Keras works as follows:所以基本上,TF Slim 和 Keras 之间的映射如下:

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

This difference is also noted in another Stack Overflow post , so kudos to the posters there.在另一个Stack Overflow 帖子中也注意到了这种差异,因此对那里的海报表示敬意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM