- 我们为什么要对变量命名
举个例子:在迁移学习中我们是通过变量名加载相应的值
# restore variables
# redefine the same shape and same type for your variables
W = tf.Variable(np.arange(6).reshape((2, 3)), dtype=tf.float32, name="weights") # 这个name必须是保存变量时的名字
b = tf.Variable(np.arange(3).reshape((1, 3)), dtype=tf.float32, name="biases")
# not need init step
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, "my_net/save_net.ckpt")
print("weights:", sess.run(W))
print("biases:", sess.run(b))
"""
完整代码:https://github.com/MorvanZhou/tutorials/blob/master/tensorflowTUT/tf19_saver.py
"""
对创建变量的方式有两种
tf.Variable()
tf.get_variable()
和命名相关的还有
tf.name_scope() 与 tf.variable_scope()
提问:
- 在name_scope下,使用Variable命名与使用get_variable命名有什么区别
Variable的name会受name_scope影响
get_variable的name不受name_scope影响
相关结论
- name_scope Variable做不到变量重复使用的效果
进一步思考
- 什么时候需要对变量重复使用