tensorboard的应用

1、tensorboard的网络结构
想看到网络结构,就是给每个步骤起名字(tf.name_scope),然后在主函数中写writer=tf.summary.FileWriter(‘logs/’,sess.graph)会在当前目录生成一个tensorboard的网络结构,文件夹名为logs。

先看代码吧,加深一下理解:

#tensorboard结构
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist=input_data.read_data_sets("MNIST_data",one_hot=True)

batch_size=100
n_batch=mnist.train.num_examples #一共有多少批次

#命名空间
with tf.name_scope('input'):
    x=tf.placeholder(tf.float32,[None,784],name='x-input')#28*28的图片拉长一个向量
    y=tf.placeholder(tf.float32,[None,10],name='y-input')

#创建一个两层的神经网络
with tf.name_scope('layer'):
    with tf.name_scope('weights'):
        W=tf.Variable(tf.zeros([784,10]),name='w')
    with tf.name_scope('biases'):
        b=tf.Variable(tf.zeros([1,10]),name='b')
    with tf.name_scope('wx_plus_b'):
        wx_plus_b=tf.matmul(x,W)+b
    with tf.name_scope('softmax'):
        prediction=tf.nn.softmax(tf.matmul(x,W)+b)
with tf.name_scope('loss'):
    loss=tf.reduce_mean(tf.square(y-prediction))
with tf.name_scope('train'):
    train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)

init=tf.global_variables_initializer()
with tf.name_scope('accuracy'):
    with tf.name_scope('correct'):
        correct=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#返回一维张量中最大值的位置
    with tf.name_scope('acc'):
        accuracy=tf.reduce_mean(tf.cast(correct,tf.float32))#bool型转换为float,[1,0,0,1,1]求平均值

with tf.Session() as sess:
    sess.run(init)
    writer=tf.summary.FileWriter('logs/',sess.graph)
    for epoch in range(1):
        for batch in range(n_batch):
            batch_xs,batch_ys=mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter"+str(epoch)+",Tseting accuracy"+str(acc))

运行之后,我们会在当前目录的logs文件夹发现一个图结构,如何打开呢:

我们打开cmd,然后移动到相应的盘,我的在D盘,然后输入命令:
tensorboard --logdir=D:\spyderpy\logs 会返回一个网址。用谷歌或者搜狐(其他的浏览器不一定行)打开,在Graphs里面我们可以看到整个网络的图结构:

在这里插入图片描述

我们点击其中的加号可以看某个模块的细节,我们也可以把每个模块分开,单独看,看个人喜好了。通过我们这个可视化的图结构,我们可以更好地理解我们所创建的网络。

2、重现tensorflow运行时的数据

 这一点在我们分析数据,高效提升代码质量的方面比较重要。我们可以在tensorboad里面查看每个变量的数据变化,以此来看看自己网络的效果在全程是如何变化的。

基本代码还是上面这个,在此基础上做修改:

#tensorboard结构,网络运行时的数据
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()

from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist=input_data.read_data_sets("MNIST_data",one_hot=True)

batch_size=100
n_batch=mnist.train.num_examples #一共有多少批次

#参数概要
def variable_summaries(var):
    with tf.name_scope('summaries'):
        mean=tf.reduce_mean(var)
        tf.summary.scalar('mean',mean)
        with tf.name_scope('stddev'):
            stddev=tf.sqrt(tf.reduce_mean(tf.square(var-mean)))
        tf.summary.scalar('stddev',stddev)
        tf.summary.scalar('max',tf.reduce_max(var))
        tf.summary.scalar('min',tf.reduce_min(var))
        tf.summary.histogram('histogram',var)
#命名空间
with tf.name_scope('input'):
    x=tf.placeholder(tf.float32,[None,784],name='x-input')#28*28的图片拉长一个向量
    y=tf.placeholder(tf.float32,[None,10],name='y-input')

#创建一个两层的神经网络
with tf.name_scope('layer'):
    with tf.name_scope('weights'):
        W=tf.Variable(tf.zeros([784,10]),name='w')
        variable_summaries(W)
    with tf.name_scope('biases'):
        b=tf.Variable(tf.zeros([1,10]),name='b')
        variable_summaries(b)
    with tf.name_scope('wx_plus_b'):
        wx_plus_b=tf.matmul(x,W)+b
    with tf.name_scope('softmax'):
        prediction=tf.nn.softmax(tf.matmul(x,W)+b)
with tf.name_scope('loss'):
    loss=tf.reduce_mean(tf.square(y-prediction))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train'):
    train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)

init=tf.global_variables_initializer()
with tf.name_scope('accuracy'):
    with tf.name_scope('correct'):
        correct=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#返回一维张量中最大值的位置
    with tf.name_scope('acc'):
        accuracy=tf.reduce_mean(tf.cast(correct,tf.float32))#bool型转换为float,[1,0,0,1,1]求平均值
        tf.summary.scalar('accuracy',accuracy)
#合并所有的监测数据
merged=tf.summary.merge_all()
with tf.Session() as sess:
    sess.run(init)
    writer=tf.summary.FileWriter('logs/',sess.graph)
    for epoch in range(11):
        for batch in range(n_batch):
            batch_xs,batch_ys=mnist.train.next_batch(batch_size)
            summary,_=sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})
        writer.add_summary(summary,epoch)
        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter"+str(epoch)+",Tseting accuracy"+str(acc))

我们定义了一个variable_summaries函数,记录了检测变量概要,比如平均值,方差,最大值等。然后在每个变量下面调用这个函数。注意必须是有很多值传入才调用variable_summaries,比如W,b.
对于loss这种一个值的直接用tf.summary.scalar()。我们再合并所有的监测数据merged=tf.summary.merge_all()

在会话中运行这个merged,并且写入writer.add_summary(summary,epoch)

我们来看看效果
在这里插入图片描述
在这里插入图片描述

可以看出准确率在前七个回合波动较大,之后准确率在上升;loss前七个回合有波动,之后一直在下降。说明他们之间联系紧密,但是loss不是影响准确率的唯一因素,所以2,3,4,5回合不能一一对应。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值