tensorboard可以将训练过程中的一些参数可视化,比如我们最关注的loss值和accuracy值,简单来说就是把这些值的变化记录在日志里,然后将日志里的这些数据可视化。

首先运行训练代码

#coding:utf-8
import tensorflow as tf
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 // batch_size #参数概要 传入一个参数可以计算这个参数的各个相关值
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'):
#定义两个placeholder
x = tf.placeholder(tf.float32, [None,784],name='x-input') #输入图像
y = tf.placeholder(tf.float32, [None,10],name='y-input') #输入标签
#创建一个简单的神经网络 784个像素点对应784个数 因此输入层是784个神经元 输出层是10个神经元 不含隐层
#最后准确率在92%左右
with tf.name_scope('layer'):
with tf.name_scope('wights'):
W = tf.Variable(tf.zeros([784,10]),name = 'W') #生成784行 10列的全0矩阵
variable_summaries(W)
with tf.name_scope('biases'):
b = tf.Variable(tf.zeros([1,10]),name='b')
variable_summaries(b)
with tf.name_scope('softmax'):
prediction = tf.nn.softmax(tf.matmul(x,W)+b) #二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
#交叉熵损失
with tf.name_scope('loss'):
loss =tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels =y,logits = prediction))
tf.summary.scalar('loss',loss)
#使用梯度下降法
#train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
train_step = tf.train.AdamOptimizer(1e-3).minimize(loss) #学习率一般设置比较小 收敛速度快 #初始化变量
init = tf.global_variables_initializer() #结果存放在布尔型列表中
#argmax能给出某个tensor对象在某一维上的其数据最大值所在的索引值
with tf.name_scope('accuracy'):
with tf.name_scope('correct_prediction'):
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction,1))
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
tf.summary.scalar('accuracy',accuracy)
#合并所有的summary
merged = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter('/home/xxx/logs/',sess.graph) #定义记录日志的位置
for epoch in range(50):
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) #将summary epoch 写入到writer
acc = sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels})
print ("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))

注意我将训练日志保存在 /home/xxx/logs/ 路径下,打开终端,输入以下命令 tensorboard --logdir=/home/xxx/logs/ 如下图所示

在浏览器中输入127.0.0.1:6006,可以看到可视化效果,如loss和accuracy的变化折线图

Tensorflow学习教程------tensorboard网络运行和可视化的更多相关文章

  1. Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例

    紧接上篇Tensorflow学习教程------tfrecords数据格式生成与读取,本篇将数据读取.建立网络以及模型训练整理成一个小样例,完整代码如下. #coding:utf-8 import t ...

  2. Tensorflow学习教程------过拟合

    Tensorflow学习教程------过拟合   回归:过拟合情况 / 分类过拟合 防止过拟合的方法有三种: 1 增加数据集 2 添加正则项 3 Dropout,意思就是训练的时候隐层神经元每次随机 ...

  3. Tensorflow学习教程------代价函数

    Tensorflow学习教程------代价函数   二次代价函数(quadratic cost): 其中,C表示代价函数,x表示样本,y表示实际值,a表示输出值,n表示样本的总数.为简单起见,使用一 ...

  4. tensorflow学习笔记----TensorBoard讲解

    TensorBoard简介 TensorBoard是TensorFlow自带的一个强大的可视化工具,也是一个Web应用程序套件.TensorBoard目前支持7种可视化,Scalars,Images, ...

  5. tensorflow 学习教程

    tensorflow 学习手册 tensorflow 学习手册1:https://cloud.tencent.com/developer/section/1475687 tensorflow 学习手册 ...

  6. Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_利用训练好的模型进行分类

    #coding:utf-8 import tensorflow as tf from PIL import Image,ImageFilter from tensorflow.examples.tut ...

  7. Tensorflow学习教程------创建图启动图

    Tensorflow作为目前最热门的机器学习框架之一,受到了工业界和学界的热门追捧.以下几章教程将记录本人学习tensorflow的一些过程. 在tensorflow这个框架里,可以讲是若数据类型,也 ...

  8. Tensorflow学习教程------lenet多标签分类

    本文在上篇的基础上利用lenet进行多标签分类.五个分类标准,每个标准分两类.实际来说,本文所介绍的多标签分类属于多任务学习中的联合训练,具体代码如下. #coding:utf-8 import te ...

  9. Tensorflow学习教程------非线性回归

    自己搭建神经网络求解非线性回归系数 代码 #coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pypl ...

随机推荐

  1. mysql 权限管理 grant revoke

    grant all privileges  on  database.table to 'user'@'ip' identified by 'passwd' with grant  option; g ...

  2. JDK8中的HashMap实现原理及源码分析

    大纲 一.什么是Hash?什么是HashMap? 二.HashMap的内部实现机制 1.HashMap基本元素 ①DEFAULT_INITIAL_CAPACITY&MAXIMUM_CAPACI ...

  3. Spark Storage 模块

    http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ 大神写的太好了,我就不重复造轮子了. Spark ...

  4. openssl生成CA签署 及 加密解密基础

    openssl  生成私有CA 及签署证书 openssl 配置文件: /etc/pki/tls/openssl.cnf 1. 在openssl CA 服务器端生成私钥 cd /etc/pki/CA/ ...

  5. python假设一段楼梯共 n(n>1)个台阶,小朋友一步最多能上 3 个台阶,那么小朋友上这段楼 梯一共有多少种方法

    我们先把前四节种数算出来(自己想是哪几类,如果你不会算,那就放弃写代码吧,干一些在街上卖肉夹馍的小生意,也挣得不少) 标号 1    2    3     4 种类 1    2    4     7 ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-file

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  7. windowsXP下 使用live555搭建视频服务器,并使用ffplay和VLC播放

    首先在官网下载live555:http://www.live555.com/mediaServer/#downloading当然是现在windows的版本了!!上官网下载FFmpeg:http://f ...

  8. eclipse 安装spring tools suite插件

    之前使用idea进行springboot项目的开发学习,但是由于idea是收费的,总是用着用着说验证码到期之类的,总之还是很不爽,于是就想重新采用eclipse开发springboot项目,为了方便s ...

  9. JS+CSS - table 表格固定表头和第一列、内容可滚动 (转载)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  10. DEDECMS打开网站后台系统首页卡解决方法

    找到根目录下(一般是dede) templets文件夹下找到index_body.htm文件,将第25行至第41行部分注释或删除 保存文件,然后再打开后台,就不会有这个问题了.