1、tf.Variables()和 tf.get_variable()区别
tf.variable()会直接创建新的变量实例,如果已经存在,系统自己处理,变量名后缀增加0,1,2...
tf.get_variable(“name”) 可以实现变量共享:使用默认值时reuse=False,当有相同变量存在时,系统不会自己处理,报有同名的标量存在的错;如果设置 reuse=True,如果变量已经存在,则返回存在的变量,如果不存在,则新创建一个
2、tf.name_scope和tf.variable_scope区别
均为设置作用域。
tf.name_scope下,仅对tf.Variable()创建的变量前面增加作用域,对tf.get_variable()创建的变量不增加作用域
tf.variable_scope下,对tf.Variable()和tf.get_variable()创建的变量均增加作用域
实例:
with tf.variable_scope("scope"): v1 = tf.get_variable("var1", [1], dtype=tf.float32) // v1 scope/var1:0 v2 = tf.Variable(1, name="var2", dtype=tf.float32) // v2 scope/var2:0 a = tf.add(v1, v2) // a scope/add:0
with tf.name_scope("scope"): v1 = tf.get_variable("var1", [1], dtype=tf.float32) // v1 var1:0 v2 = tf.Variable(1, name="var2", dtype=tf.float32) // v2 scope/var2:0 a = tf.add(v1, v2) // a scope/add:0