tensorflow基础知识8,可视化工具tensorboard运用实例
1、修改之前的mnist程序,利用tensorboard显示算法图形,桃色字体为tensorboard部分
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据
mnist=input_data.read_data_sets('mnist_data',one_hot=True)#noe_hot把像素点都转变成0或1的形式
#每个批次的大小,训练模型时,一次放入一批次
batch_size=100 #一批次100张图
#计算一共有多少个批次
n_batch=mnist.train.num_examples//batch_size# //是整除,得到批次数
#命名空间
with tf.name_scope('input'): #命名随意,比如input,下面的x和y要缩进,表示x,y放在input空间
#定义两个placeholder,配合上面命名空间,给x,y取个名字
x=tf.placeholder(tf.float32,[None,784],name='x-input')#建立一个占位符,None是图片数,784是每幅图的像素个数
y=tf.placeholder(tf.float32,[None,10],name='y-input')# 标签,建立一个占位符,10是指0-9十个数
#创建一个简单的神经网络,输入层784个神经元,输出层10个神经元,不设隐藏层
W=tf.Variable(tf.zeros([784,10]))#权值,设一个变量,置0
b=tf.Variable(tf.zeros([10]))#偏置值
prediction=tf.nn.softmax(tf.matmul(x,W)+b)#信号总和,经过softmax函数(**函数)转化成概率值
#二次代价函数
#loss =tf.reduce_mean(tf.square(y-prediction))
#使用交叉熵代价函数
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#使用梯度下降法
train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#初始化变量
init=tf.global_variables_initializer()
#训练好后求准确率,结果存放在一个布尔型列表中,argmax返回一维张量中最大的值所在的位置
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax函数是对行或列计算最大值,1表示按行,0表示按列,找到最大概率标签的位置。 equal函数是比较两个参数大小,相等的话返回True,不相等返回False
#求准确率
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#cast()是类型转换函数,把布尔型参数转换为32位古典型,然后求平均值。true变成1.0,flse变成0
#Boolean→数值型:True转换为-1,False转换为0。数值型→Boolean:0转换为False,其他转换为True
with tf.Session() as sess:
sess.run(init)#初始化变量
writer=tf.summary.FileWriter('logs/',sess.graph)#'logs/'是路径,graph存在logs文件夹中,如果没有logs文件夹,这里会自动生成
for epoch in range(1):#迭代21个周期,把所有图片训练21次,这里改为1次,用作tensorboard显示
for batch in range(n_batch):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)#一次分配100张图片,图片数据保存在batch_xs,标签保存在batch_ys
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})#x输入测试图片,从而得到prediciton的y,从而和label y 对比
print('Iter'+str(epoch)+',Testing Accuracy'+str(acc))
结果:
Iter0,Testing Accuracy0.8255
2、上面程序运行后,会生成一个events.out.tfevents.1549613324的文件。在命令中cd到该文件保存地址,tensorboard --logdir=加路径,运行得到一个网址, http://LAPTOP-89Q8V158:6006,将其改为http://localhost:6006,即可在浏览器进入tensorboard图形显示。
2、继续修改mnist程序,将程序结构相关的部分都name_scope,从而得到更加直观简洁的tensorboard图形
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据
mnist=input_data.read_data_sets('mnist_data',one_hot=True)#noe_hot把像素点都转变成0或1的形式
#每个批次的大小,训练模型时,一次放入一批次
batch_size=100 #一批次100张图
#计算一共有多少个批次
n_batch=mnist.train.num_examples//batch_size# //是整除,得到批次数
#命名空间
with tf.name_scope('input'): #命名随意,比如input,下面的x和y要缩进,表示x,y放在input空间
#定义两个placeholder,配合上面命名空间,给x,y取个名字
x=tf.placeholder(tf.float32,[None,784],name='x-input')#建立一个占位符,None是图片数,784是每幅图的像素个数
y=tf.placeholder(tf.float32,[None,10],name='y-input')# 标签,建立一个占位符,10是指0-9十个数
with tf.name_scope('layer'):
#创建一个简单的神经网络,输入层784个神经元,输出层10个神经元,不设隐藏层
with tf.name_scope('wights'):
W=tf.Variable(tf.zeros([784,10]),name='W')#权值,设一个变量,置0
with tf.name_scope('biases'):
b=tf.Variable(tf.zeros([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)#信号总和,经过softmax函数(**函数)转化成概率值
#二次代价函数
#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))
#使用梯度下降法
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_prediction'):
#训练好后求准确率,结果存放在一个布尔型列表中,argmax返回一维张量中最大的值所在的位置
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax函数是对行或列计算最大值,1表示按行,0表示按列,找到最大概率标签的位置。 equal函数是比较两个参数大小,相等的话返回True,不相等返回False
with tf.name_scope('accuracy'):
#求准确率
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#cast()是类型转换函数,把布尔型参数转换为32位古典型,然后求平均值。true变成1.0,flse变成0
#Boolean→数值型:True转换为-1,False转换为0。数值型→Boolean:0转换为False,其他转换为True
with tf.Session() as sess:
sess.run(init)#初始化变量
writer=tf.summary.FileWriter('logs/',sess.graph)#'logs/'是路径,graph存在logs文件夹中,如果没有logs文件夹,这里会自动生成
for epoch in range(1):#迭代21个周期,把所有图片训练21次
for batch in range(n_batch):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)#一次分配100张图片,图片数据保存在batch_xs,标签保存在batch_ys
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})#x输入测试图片,从而得到prediciton的y,从而和label y 对比
print('Iter'+str(epoch)+',Testing Accuracy'+str(acc))