【6.1.1】TensorFlow中的name有什么用

在某些地方,我看到了语法,其中变量用name初始化,有时没有name。 例如:

# With name
var = tf.Variable(0, name="counter")

# Without
one = tf.constant(1)

或者:

aa = tf.placeholder(tf.int32, shape=(), name='batch_edge_type_idx')
bb = tf.sparse_placeholder(tf.float32)

那么变量名var和counter有什么区别呢?

name参数是可选的(您可以创建带或不带它的变量和常量),并且您在程序中使用的变量不依赖于它。 name在以下几个方面很有帮助:

1.当您想要保存或恢复变量时

matrix_1 = tf.Variable([[1, 2], [2, 3]], name="v1")
matrix_2 = tf.Variable([[3, 4], [5, 6]], name="v2")
init = tf.initialize_all_variables()

saver = tf.train.Saver()

sess = tf.Session()
sess.run(init)
save_path = saver.save(sess, "/model.ckpt")
sess.close()

matrix_1和matrix_2保存的时候以v1和v2保存

2.在TensorBoard中使用name来很好地显示边的名称(什么意思,不是太理解呀??)

import tensorflow as tf

with tf.name_scope('hidden') as scope:
  a = tf.constant(5, name='alpha')
  W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')
  b = tf.Variable(tf.zeros([1]), name='biases')

您可以将Python命名空间和TensorFlow命名空间想象为两个平行线。 TensorFlow空间中的名称实际上是属于任何TensorFlow变量的“真实”属性,而Python空间中的名称只是在脚本运行期间指向TensorFlow变量的临时指针。 这就是为什么在保存和恢复变量时,只使用TensorFlow名称的原因,因为脚本终止后Python命名空间不再存在,但Tensorflow命名空间仍然存在于保存的文件中。

参考资料:

https://blog.csdn.net/xiaohuihui1994/article/details/81022043

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn