据我所知,tf.keras.initializers.ones()
是初始化权重的初始值设定项之一。
下面的 3 行代码生成相同的张量:
>>> a = tf.keras.initializers.ones()((1, 2))
>>> b = tf.ones((1, 2))
>>> c = tf.constant([[1., 1.]])
>>> a
<tf.Tensor: id=27, shape=(1, 2), dtype=float32, numpy=array([[1., 1.]], dtype=float32)>
>>> b
<tf.Tensor: id=30, shape=(1, 2), dtype=float32, numpy=array([[1., 1.]], dtype=float32)>
>>> c
<tf.Tensor: id=35, shape=(1, 2), dtype=float32, numpy=array([[1., 1.]], dtype=float32)>
看起来我可以使用tf.ones((1, 2))
来获得相同的权重值。但是我仍然应该使用 ones 初始值设定项来初始化权重作为很好的做法。
想确保我在这里没有错过任何重要的初始值设定项。
这是tf.keras.initializers.ones
的来源。主要是对array_ops.ones
的调用,和tf.ones
一样。它还检查类型是数字还是布尔值。
尽管它们看起来是等效的,但我建议使用tf.keras.initializers.ones
,因为它可以更清楚地表明您正在使用这些值来初始化某些内容。