tensorflow共享变量 the difference between tf.Variable() and get_variable()

一般这样用tf.get_variable():

v = tf.get_variable(name, shape, dtype, initializer)

下面内容来源于 http://blog.csdn.net/u012436149/article/details/53696970

当我们需要共享变量的时候,需要使用tf.get_variable()
使用tf.Variable时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()时,系统不会处理冲突,而会报错,例子:

import tensorflow as tf
w_1 = tf.Variable(3,name="w_1")
w_2 = tf.Variable(1,name="w_1")
print w_1.name
print w_2.name
#输出
#w_1:0
#w_1_1:0
import tensorflow as tf

w_1 = tf.get_variable(name="w_1",initializer=1)
w_2 = tf.get_variable(name="w_1",initializer=2)
#错误信息
#ValueError: Variable w_1 already exists, disallowed. Did
#you mean to set reuse=True in VarScope?
原文地址:https://www.cnblogs.com/ZhongliangXiang/p/7931781.html