Tensorboard(内附代码)

这里我们采用Mnist手写数字数据集来讲解如何将训练过程可视化。
https://github.com/MagaretJi/Tensorboard

Tensorboard记录与展示的数据形式
(1)标量Scalars:tf.summary.scalar()
(2)图片Images:tf.summary.image()
(3)音频Audio:tf.summary.audio()
(4)计算图Graph:tf.summary.graph()
(5)数据分布Distribution:tf.summary.distribution()
(6)直方图Histograms:tf.summary.histogram()
(7)嵌入向量Embeddings

Step1
导入需要用到的包
我们直接使用了TensorFlow自带的数据集。

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

数据集准备

mnist=input_data.read_data_sets("MNIST_data/",one_hot=True)

Step2 设置占位符
x:训练集样本特征
y:样本分类标签

with tf.name_scope('input'):
    x=tf.placeholder(tf.float32,[None,784],name='x-input')
    y=tf.placeholder(tf.float32,[None,10],name='y-input')

Step3 设置参数概要
我们知道,在训练的过程在参数是不断地在改变和优化的,我们往往想知道每次迭代后参数都做了哪些变化,可以将参数的信息展现在tenorbord上,因此我们专门写一个方法来收录每次的参数信息。计算出Variable的mean,stddev,max和min。

# 参数概要
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)

Step4 搭建神经网络

with tf.name_scope('layer'):
    with tf.name_scope('weight'):
        w =tf.Variable(tf.zeros([784,10]),name='weight')
        variable_summaries(w)
    
    with tf.name_scope('bias'):
        b=tf.Variable(tf.zeros([10]),name='bias')
        variable_summaries(b)
    
    with tf.name_scope('softmax'):
        prediction=tf.nn.softmax(tf.matmul(x,w)+b,name='softmax')
        variable_summaries(prediction)

Step5
Loss值和梯度下降
tf.summary.scalar(tags, values, collections=None, name=None)
一般在画loss,accuary时会用到这个函数

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)

准确率

with tf.name_scope('accuracy'):
#结果放在布尔型列表中
    with tf.name_scope('correct_prediction'):
        correct_prediction=tf.equal(tf.math.argmax(y,1),tf.math.argmax(prediction,1))
        #tf.summary.histogram('accuracy/correct_prediction',correct_prediction)
    with tf.name_scope('accuracy'):
    #准确率
        accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
        tf.summary.histogram('accuracy/accuracy',accuracy)

Step6 超参数设置

#批次大小
batch_size=100

#批次数
n_batch=mnist.train.num_examples // batch_size

#训练轮数
n_epoch=210

Step7 开始训练
tf.summaries.merge_all()
合并默认图形中的所有汇总
tf.summary.FileWriter()
SummaryWriter是一个类,它可以调用以下成员函数来往event文件中添加相关的数据 addsummary(), add sessionlog(), add_event(), or add_graph()

with tf.Session() as sess:
sess.run(init)
merged = tf.summary.merge_all()
#日志存放路径,会存放所有汇总数据供Tensorflow展示
writer=tf.summary.FileWriter('./logs',sess.graph)
for epoch in range(n_epoch):
    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("Lter " + str(epoch) + ",Testing Accuracy " + str(acc))

参考
https://blog.csdn.net/sinat_33761963/article/details/62433234

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值