一、tf.Variable与tf.get_variable
tensorflow提供了通过变量名称来创建或者获取一个变量的机制。通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递。
TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。
当然,变量也可以通过tf.Varivale来创建。当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价。
v = tf.get_variable('v', shape=[1], initializer=tf.constant_initializer(1.0))
v = tf.Variable(tf.constant(1.0, shape=[1], name='v')
两个定义时等价的。
二、tf.get_variable与tf.variable_scope
with tf.variable_scope("g"):
if reuse:
tf.get_variable_scope().reuse_variables()
在这里说明一下reuse问题:
(1)当resuse为false(或者None)时:
同一个
tf.variable_scope()
下面的变量名

本文介绍了TensorFlow中如何通过tf.get_variable和tf.variable_scope创建和获取变量,并探讨了tf.get_variable与tf.Variable的区别,以及tf.variable_scope与tf.name_scope在变量管理中的作用,特别是变量重用和命名空间管理的概念。
最低0.47元/天 解锁文章
475

被折叠的 条评论
为什么被折叠?



