Python machine learning-TensorFlow我爱编程

tensorflow变量命名空间与可视化

2018-05-17  本文已影响45人  阿发贝塔伽马

variable_scope

如果是不可重用的(reuse=False),tf. get_variable函数会查找在当前命名空间下是否存在由tf.get_variable定义的同名变量(而不是tf.Variable定义的),如果不存在,则新建对象,否则会报错

如果是可重用的(reuse=True),如果存在,则会返回之前的对象,否则报错,

tf. Variable不管在什么情况下都是创建新变量,自己解决命名冲突

下面举个例子说明

import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.InteractiveSession()
with tf.variable_scope("scope1"):
    w1 = tf.get_variable("w1", initializer=4.)
    w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope2"):
    w1_p = tf.get_variable("w1", initializer=5.)
    w2_p = tf.Variable(1.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
    w1_reuse = tf.get_variable("w1")
    w2_reuse = tf.Variable(1.0, name="w2")

def compare_var(var1, var2):
    print '-----------------'
    if var1 is var2:
        print sess.run(var2)
    print var1.name, var2.name
sess.run(tf.global_variables_initializer())

compare_var(w1, w1_p)
compare_var(w2, w2_p)
compare_var(w1, w1_reuse)
compare_var(w2, w2_reuse)

name_scope

get_variable不受name_scope命名空间约束

Variable受命名空间约束,但可以自己解决冲突

import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.InteractiveSession()

with tf.name_scope("scope1"):
    w1 = tf.Variable(0.0, name="w1")
    w2 = tf.get_variable("w2", initializer=4.)
with tf.name_scope("scope1"):
    w1_p = tf.Variable(1.0, name="w1")

    w2_p = tf.get_variable("w1", initializer=5.)

def compare_var(var1, var2):
    print '-----------------'
    if var1 is var2:
        print sess.run(var2)
    print var1.name, var2.name
    print '-----------'
sess.run(tf.global_variables_initializer())

compare_var(w1, w2)
compare_var(w1_p, w2_p)

总结两个命名空间的作用不同

variable_scope与get_variable搭配使用可以使得共享变量
name_scope主要用来tensorboard可视化

tensorboard可视化

import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.InteractiveSession()
log_dir = '../datachuli'
def practice_num():
# 练习1: 构建简单的计算图
    input1 = tf.constant([1.0, 2.0, 3.0],name="input1")
    input2 = tf.Variable(tf.random_uniform([3]),name="input2")
    output = tf.add_n([input1,input2],name = "add")

    sess.run(tf.global_variables_initializer())
    sess.run(output)
#生成一个写日志的writer,并将当前的tensorflow计算图写入日志
    writer = tf.summary.FileWriter(log_dir + "/log",tf.get_default_graph())
    writer.close()
    
practice_num()
ops.reset_default_graph()
sess = tf.InteractiveSession()

def practice_num_modify():
    #将输入定义放入各自的命名空间中,从而使得tensorboard可以根据命名空间来整理可视化效果图上的节点
    # 练习1: 构建简单的计算图
    with tf.name_scope("input1"):
        input1 = tf.constant([1.0, 2.0, 3.0],name="input1")
    with tf.name_scope("input2"):
        input2 = tf.Variable(tf.random_uniform([3]),name="input2")
    with tf.name_scope('add1'):
        output = tf.add_n([input1,input2],name = "add")
    sess.run(tf.global_variables_initializer())
    sess.run(output)

#生成一个写日志的writer,并将当前的tensorflow计算图写入日志
    writer = tf.summary.FileWriter(log_dir + "/log_namescope",tf.get_default_graph())
    writer.close()
practice_num_modify()

可以点击add1和input2右上角+号展开


展开前
展开后
上一篇 下一篇

猜你喜欢

热点阅读