最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

TensorFlow # Scope,Name,Reuse参数意义及变量重用机制

运维笔记admin55浏览0评论

TensorFlow # Scope,Name,Reuse参数意义及变量重用机制

TensorFlow # Scope,Name,Reuse参数意义及变量重用机制

scope参数用途

tensorflow的执行过程:1)定义Graphs,包括Variables和Operations 。2)创建session,运行Graphs
在定义Variables的时候,Scope相当于C++中的命名空间,可以用Scope来避免命名冲突,以及方便重用Variables。

with tf.variable_scope(scope, reuse=reuse):out = inputout = layers.fully_connected(out, num_outputs=num_units, activation_fn=tf.nn.relu)out = layers.fully_connected(out, num_outputs=num_units, activation_fn=tf.nn.relu)out = layers.fully_connected(out, num_outputs=num_outputs, activation_fn=None)return out

name参数用途

name参数用于保存变量、复用变量。

1 保存或恢复变量

python中的变量名与计算图中的变量名显然无法共享,因此对于计算图有单独的命名

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") # 保存变量以v1,v2命名
sess.close()

Python空间中的名称只是在脚本运行期间指向TensorFlow变量的临时指针。 在保存和恢复变量时,只使用TensorFlow名称的原因,因为脚本终止后Python命名空间不再存在,但Tensorflow命名空间仍然存在于保存的文件中。如果需要保存和恢复,请确保同一命名空间下的命名唯一

Reference

  1. tensorflow scope的作用
  2. TensorFlow中的name有什么用
发布评论

评论列表(0)

  1. 暂无评论