TensorFlow中定义Tensor时的参数name有什么用?

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

# With name
x1 = tf.Variable(0, name="X1")

# Without
one = tf.constant(1)

那么变量名x1X1有什么区别呢?

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

当您想要保存或恢复变量时
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保存

在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变量的临时指针。 (也就是上面代码中的a, W, b都是在Python空间中定义的变量,而name参数定义的如alpha, weights, biases都是在TensorFlow空间中的)这就是为什么在保存和恢复变量时,只使用TensorFlow名称的原因,因为脚本终止后Python命名空间不再存在,但Tensorflow命名空间仍然存在于保存的文件中。

转载自:https://blog.csdn.net/xiaohuihui1994/article/details/81022043

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值