tensorflow中的名称作用域

背景

在典型的tensorflow应用程序中,可能会有数以千计的计算节点。如此多的节点汇集在一起,难以分析,甚至无法用标准的图表工具来展示。解决这个问题,一个有效方法就是,为Op/Tensor划定名称范围。

在tensorflow中,这个机制叫名称作用域(name scope)。它的作用类似C++中的“命名空间(namespace)”,或java中的“包(package)”。

使用名称作用域后,就可以将一些Op或Tensor划分到某个指定的名称作用域空间,以达到划片管理、各司其职的效果

示例

import tensorflow as tf

with tf.name_scope('sunny') as scope:   #设置名称作用域'sunny'
    a = tf.constant(5, name='forsch')
    print(a.name)
    weights = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')
    print(weights.name)
    bias = tf.Variable(tf.zeros([1]), name='biases')
    print(bias.name)
    
with tf.name_scope('wugui') as scope:   #设置名称作用域'wugui'
    weights = tf.Variable([1.0, 2.0], name='weights')
    print(weights.name)
    bias = tf.Variable([0.3], name='biases')
    print(bias.name)
    
sess = tf.Session()
writer = tf.summary.FileWriter('./my_graph/2', sess.graph)
sunny/forsch:0
sunny/weights:0
sunny/biases:0
wugui/weights:0
wugui/biases:0

猜你喜欢

转载自www.cnblogs.com/forsch/p/9997799.html
今日推荐