TensorFlow教程

【笔记】莫烦PYTHON | Tensorflow教程——可视化好助手Tensorboard(第四章)

本文主要是介绍【笔记】莫烦PYTHON | Tensorflow教程——可视化好助手Tensorboard(第四章),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

4.1 Tensorboard 可视化好帮手 1
Event / Scalar: 展示训练过程中的统计数据(最值,均值等)变化情况
Image: 展示训练过程中记录的图像
Audio: 展示训练过程中记录的音频
Histogram: 展示训练过程中记录的数据的分布图
Graphs: 展示神经网络结构图

可视化整个神经网络的结构


代码是前几节的截取
 

from __future__ import print_function
import tensorflow as tf


def add_layer(inputs, in_size, out_size, activation_function=None):
    # add one more layer and return the output of this layer
    with tf.name_scope('layer888'): #为隐藏层添加名字layer888(大框架)
        with tf.name_scope('WWWWWWWWW'): #为weights层添加名字WWWWWWWW(小部件)
            Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W') #为weights取名为W
        with tf.name_scope('biasesss'):
            biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
        with tf.name_scope('Wx_plus_bbbb'):
            Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b, )
        return outputs


# define placeholder for inputs to network
with tf.name_scope('inputsss'):
    xs = tf.placeholder(tf.float32, [None, 1], name='x_input')
    ys = tf.placeholder(tf.float32, [None, 1], name='y_input')

# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediciton and real data
with tf.name_scope('losssss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
                                        reduction_indices=[1]))

with tf.name_scope('trainnnnn'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

sess = tf.Session()

# tf.train.SummaryWriter soon be deprecated, use following
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:  # tensorflow version < 0.12
    writer = tf.train.SummaryWriter('logs/', sess.graph)
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_variables()
else:
    init = tf.global_variables_initializer()
sess.run(init)

# direct to the local dir and run this in terminal:
# $ tensorboard --logdir=logs

代码注释

    with tf.name_scope('layer888'): #为隐藏层添加名字layer888(大框架)
        with tf.name_scope('WWWWWWWWW'): #为weights层添加名字WWWWWWWW(小部件)
            Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W') #为weights取名为W
#------------------------------------------------------------------------
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu) #有系统自带的激励函数的不做命名,因为他自己有名字relu会自动显示
#------------------------------------------------------------------------
sess = tf.Session()
#将上面保存的图添加到一个目录中(这里建立一个logs目录),graph的作用是将前面定义的东西收集起来生成文件,保存在logs文件夹中,因为要使用sess.graph,所以这条语句要放在tf.Session()后面
writer = tf.summary.FileWriter('logs/',sess.graph)  #tf.__version__ > 0.12

运行方法

所在路径下运行文件,运行所在目录下生成的临时文件,所在环境下打开获得的网页链接(若打不开生成的链接,则手动打开备用链接:http://0.0.0.0:6006 or http://localhost:6006)

python ***.py
#---------------------------------
tensorboard --logdir logs

结果:

这里写图片描述

 这里写图片描述

#不加语句:
with tf.name_scope('WWWWWWWWW'):

结果:

这里写图片描述

注意事项

Tensorboard兼容google浏览器,推荐使用google浏览器,实测firefox也可以(基于谷歌内核的都可以?)
由于Tensorflow版本的更新,语句如下改变:

tf.train.SummaryWriter('logs/',sess.graph) #tf.__version__ <= 0.12
tf.summary.FileWriter('logs/',sess.graph)  #tf.__version__ > 0.12

 

可能会遇到的问题

TensorBoard attempted to bind to port 6006, but it was already in use

解决方法:

#终端下
lsof -i:6006
#查看PID
kill -9 PID号码

再次运行,完美解决

4.2 Tensorboard 可视化好帮手 2

可视化训练过程

主要语法

#第一个参数表示图表的名字,第二个参数表示要记录的变量
tf.summary.histogram(layer_name + '/weights', Weights) #一般信息在histogram栏显示
tf.summary.scalar('loss', loss) #loss bleu等,在Tensorboard中的scalar栏显示

sess = tf.Session()
#merge也要放在Session()后面
merged = tf.summary.merge_all() #把所有的summary合并在一起/一张图显示
writer = tf.summary.FileWriter("logs/", sess.graph) #创建一个文件并写入summary
init = tf.global_variables_initializer()
sess.run(init)

for i in range(1000):
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        result = sess.run(merged, feed_dict={xs: x_data, ys: y_data}) #merged也需要run才能发挥作用
        writer.add_summary(result, i) #把result放进writer里/把result写入到writer对应的文件中,i是记录的步数,记录一次

完整代码

"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
from __future__ import print_function
import tensorflow as tf
import numpy as np


def add_layer(inputs, in_size, out_size, n_layer, activation_function=None): #传入一个新的值n_layer=1,2
    # add one more layer and return the output of this layer
    layer_name = 'layer%s' % n_layer #layer1,layer2
    with tf.name_scope(layer_name):
        with tf.name_scope('weights'):
            Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W')
            tf.summary.histogram(layer_name + '/weights', Weights) #第一个参数表示图表的名字,第二个参数表示要记录的变量
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
            tf.summary.histogram(layer_name + '/biases', biases)
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b, )
        tf.summary.histogram(layer_name + '/outputs', outputs)
    return outputs


# Make up some real data
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# define placeholder for inputs to network
with tf.name_scope('inputs'):
    xs = tf.placeholder(tf.float32, [None, 1], name='x_input')
    ys = tf.placeholder(tf.float32, [None, 1], name='y_input')

# add hidden layer
l1 = add_layer(xs, 1, 10, n_layer=1, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, n_layer=2, activation_function=None)

# the error between prediciton and real data
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
                                        reduction_indices=[1]))
    tf.summary.scalar('loss', loss)

with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

sess = tf.Session()
merged = tf.summary.merge_all()

writer = tf.summary.FileWriter("logs/", sess.graph)

init = tf.global_variables_initializer()
sess.run(init)

for i in range(1000):
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        result = sess.run(merged, feed_dict={xs: x_data, ys: y_data})
        writer.add_summary(result, i)

# direct to the local dir and run this in terminal:
# $ tensorboard --logdir logs

这里写图片描述

这里写图片描述 

         之前没有清理掉logs文件夹中的文件,显示的就是这个图,emmmmmmmm,又试了一下,运行完,再训练运行一次,logs文件夹内此时有三个生成的文件,用tensorboard –logdir logs查看,得到的就是这种好几条线的了。。。

这里写图片描述

 

这篇关于【笔记】莫烦PYTHON | Tensorflow教程——可视化好助手Tensorboard(第四章)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!