TensorBoard是TensorFlow的可视化工具,它可以通过TensorFlow程序运行过程中输出的日志文件可视化TensorFlow程序的运行状态。TensorFlow和TensorBoard程序跑在不同的进程中,TensorBoard会自动读取最新的TensorFlow日志文件,并呈现当前程序运行的最新状态。下面通过使用MNIS数据集创建一个简单的神经网络实现对某一参数的可视化。
下载方式(1)登录MNIS数据集官网选择性下载The Mnist Database of handwritten digits
下载方式(2)本地直接导出;
train-images-idx3-ubyte.gz :train-images
train-labels-idx1-ubyte.gz:train-labels
t10k-images-idx3-ubyte.gz:test-images
10k-labels-idx1-ubyte.gz:test-labelst
把下载的数据集放到自己定义文件夹下,我这里定义的文件夹是:MNIST_data
代码中读入数据集方式有两种如下:
mnist = input_data.read_data_sets("MNIST_data",one_hot=True) #数据集和代码在一个目录下
mnist = input_data.read_data_sets(r"C:\Users\Administrator\Desktop\MNIST_data",one_hot=True) #数据集路径
(1)确定Windows或者Linux下TensorFlow框架可行,如果没有安装可参照Windows下Anaconda和TensorFlow安装
(2)新建一个可视化.py文件,我这里命名为 Visualization .py,保存到和数据集一个目录下,其中
writer = tf.summary.FileWriter('/tmp/log', sess.graph) #写入到的位置(C盘根目录)
测试代码如下:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
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"):
x = tf.placeholder(tf.float32,[None,784], name = "x_input")
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([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(wx_plus_b)
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)
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'):
#结果存放在一个布尔型列表中
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)
merged = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter('/tmp/log', sess.graph)
for epoch in range(101):
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("epoch= " + str(epoch)+ " accuracy=" +str(acc))
(3)测试迭代100次结果如下
(4)运行完毕后,打开终端:Anaconda Prompt并切换到tensorflow输入
activate tensorflow
tensorboard --logdir=/tmp/log
就会运行出一行网址:如下
(5)打开浏览器(版本尽量高一点,这里用的UC)在地址栏输入网址如下:
http://PC-20180711ZhaoZunqiang:6006
(6)可视化结果展示
如果最后一步复制链接到浏览器没有出现可视化图:请再次确定是否切换到输出所在路径!!这里展示一个寻找路径方法请参考如下:
tensorboard --logdir=C:\Users\Administrator\Desktop\log
以上内容编辑:赵尊强