使用这两个函数似乎得到相同的结果。
t4 = tf.get_variable('t4', initializer=tf.random_normal((2,), seed=0)) t5 = tf.get_variable('t5', shape=(2,), initializer=tf.random_normal_initializer(seed=0))
而且我发现里面random_normal_initializer()
也使用了random_normal()
。
我不清楚地意识到它们之间的区别。该random_normal
会返回一个常数张量,但random_normal_initializer
初始化后,将返回值。
我想更多地了解如何在正确的时间使用这两个功能。
它是否random_normal
用于初始化变量,实际上会两次初始化(在初始化变量之后)?换句话说,如果它们之间存在性能问题。
Maxim对这个问题的回答非常好,但是我想回答一个稍微更简单的问题(包括一些示例),OP可能会问:
最基本的答案:tf.random_normal
是一个Tensor
;但是tf.random_normal_initializer
是一个RandomNormal
,不是一个Tensor
。我认为简单的代码可以最好地阐明这两者之间的区别:
# Simple examples to clarify tf.random_normal from tf.random_normal_initializer
tf.reset_default_graph()
# OP's code
t4 = tf.get_variable('t4', initializer=tf.random_normal((2,), seed=0))
t5 = tf.get_variable('t5', shape=(2,), initializer=tf.random_normal_initializer(seed=0))
# clarifying Tensor vs Initializer outside the context of get_variable.
t6 = tf.random_normal((2,),seed=0)
t7 = tf.random_normal_initializer(seed=0)
# types
print(type(t6)) #
print(type(t7)) #
# run the graph...
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# OP's code
print(sess.run(t4)) #[-0.39915761 2.10443926]
print(sess.run(t5)) #[-0.39915761 2.10443926]
# tf.random_normal is a Tensor
print(sess.run(t6)) #[-0.39915761 2.10443926]
# tf.random_normal_initializer returns a tf.RandomNormal, not a Tensor or Op, so can't be sess.run()!
try:
print(sess.run(t7)) # Exception!
except:
print("Exception!")
# But notice that you don't need to initialize an initializer, just a variable.
t8 = tf.random_normal_initializer(seed=0)
t9 = tf.get_variable('t9',shape=(2,), initializer=t8)
sess.run(t9.initializer) # still need to initialize the variable
print(sess.run(t9)) #[-0.39915761 2.10443926]
在您的设置中:现在,就您要调用的代码而言,并没有真正的区别。该initializer
关键字被重载,同时接受并表现为马克西姆表示。从tf / ops / variable_scope文档:
if initializer is None:
init, initializing_from_value = self._get_default_initializer(
name=name, shape=shape, dtype=dtype)
if initializing_from_value:
init_shape = None
else:
init_shape = var_shape
elif callable(initializer):
init = initializer
init_shape = var_shape
elif isinstance(initializer, ops.Tensor):
init = array_ops.slice(initializer, var_offset, var_shape)
# Use the dtype of the given tensor.
dtype = init.dtype.base_dtype
init_shape = None
else:
init = ops.convert_to_tensor(initializer, dtype=dtype)
init = array_ops.slice(init, var_offset, var_shape)
init_shape = None