Tensorflow学习之TensorBoard
2017-08-21 11:27
573 查看
TensorBoard是Tensorflow的一个可视化工具,可以看见整个网络结构,以及将模型训练过程中的各种汇总数据展示出来,包括标量、图片、音频、计算图、数据分布、直方图和嵌入向量。
下面利用Mnist数据在MLP多层神经网络上训练得到的日志文件logs转入TensorBoard中进行数据可视化。
然后再Terminal中运行
可以看到
之后复制粘贴最后的网址即可进入TensorBoard观看各种数据形式。
下面利用Mnist数据在MLP多层神经网络上训练得到的日志文件logs转入TensorBoard中进行数据可视化。
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data max_step = 1000 learning_rate = 0.001 dropout = 0.9 data_dir = '/usr/local/Cellar/anaconda/lib/python3.6/site-packages/tensorflow/examples/tutorials/mnist/input_data' log_dir = '/usr/local/Cellar/anaconda/lib/python3.6/site-packages/tensorflow/examples/tutorials/mnist/logs/mnist_with_summaries' mnist = input_data.read_data_sets(data_dir,one_hot=True) sess = tf.InteractiveSession() #为了在TensorBoard中展示节点名称,设计网络时经常食用with tf.name scope 限定命名空间,在这个with下的所有节点都会被自动命名为input/xxx这样的格式 #下面定义输入x和y的placeholder,并将输入的一维数据变形为28x28的图片存储到另一个tensor,这样就可以使用tf.summary.image将图片数据汇总给TensorBoard展示了 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('input_reshape'): image_shaped_input = tf.reshape(x,[-1,28,28,1]) #-1代表自动计算的数组元素的个数,28代表元素尺寸为28x28,1代表颜色通道数 tf.summary.image('input',image_shaped_input,10) def weight_variable(shape): initial = tf.truncated_normal(shape,stddev = 0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1,shape = shape) return tf.Variable(initial) #再定义对Variable变量的数据汇总函数,对这些标量数据使用tf.summary.scalar进行记录和汇总,同时使用tf.summary.histogram直接记录变量的直方图数据 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) #利用MLP多层神经网络来训练数据,每一层都对模型参数进行数据汇总 def nn_layer(input_tensor,input_dim,output_dim,layer_name,act = tf.nn.relu): with tf.name_scope(layer_name): with tf.name_scope('weights'): weights = weight_variable([input_dim,output_dim]) variable_summaries(weights) with tf.name_scope('biases'): biases = bias_variable([output_dim]) variable_summaries(biases) with tf.name_scope('WX_plus_b'): preactivate = tf.matmul(input_tensor,weights) + biases tf.summary.histogram('pre_activations',preactivate) activations = act(preactivate,name = 'activation') tf.summary.histogram('activations',activations) return activations hidden1 = nn_layer(x,784,500,'layer1') with tf.name_scope('dropout'): keep_prob = tf.placeholder(tf.float32) tf.summary.scalar('dropout_keep_probability',keep_prob) dropped = tf.nn.dropout(hidden1,keep_prob) y = nn_layer(dropped,500,10,'layer2',act = tf.identity) #identity表示激活函数用全等映射 with tf.name_scope('cross_entropy'): diff = tf.nn.softmax_cross_entropy_with_logits(logits = y,labels = y_) with tf.name_scope('total'): cross_entropy = tf.reduce_mean(diff) tf.summary.scalar('cross_entropy',cross_entropy) #在使用Adma优化器对损失进行优化 with tf.name_scope('train'): train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) with tf.name_scope('accuracy'): with tf.name_scope('correct_prediction'): correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) with tf.name_scope('accuracy'): accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) tf.summary.scalar('accuracy',accuracy) #直接获取所有汇总操作,定义两个文件记录器tf.summary.FileWriter在不同的子目录,分别用来存放训练和测试的日志数据,同时将Session的计算图sess.graph加入训练过程的记录器 merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(log_dir + '/train',sess.graph) test_writer = tf.summary.FileWriter(log_dir + '/test') tf.global_variables_initializer().run() #定义损失函数 def feed_dict(train): if train : xs,ys = mnist.train.next_batch(100) k = dropout else : xs,ys = mnist.test.images,mnist.test.labels k = 1.0 return {x:xs,y_:ys,keep_prob:k} #最后一步,实际执行具体的训练、测试和日志记录的操作 saver = tf.train.Saver() #创建模型的保存器 for i in range(max_step): if i % 10 == 0: summary,acc = sess.run([merged,accuracy],feed_dict=feed_dict(False)) test_writer.add_summary(summary,i) print('Accuracy at step %s: %s' % (i,acc)) else : if i % 100 == 99: run_options = tf.RunOptions(trace_level = tf.RunOptions.FULL_TRACE) #定义运行选项 run_metadata = tf.RunMetadata() summary,_ = sess.run([merged,train_step],feed_dict = feed_dict(True),options=run_options,run_metadata=run_metadata) train_writer.add_run_metadata(run_metadata,'step%03d' % i) train_writer.add_summary(summary,i) saver.save(sess,log_dir + "/model.ckpt" , i) print('Adding run metadata for', i) else : summary, _=sess.run([merged,train_step],feed_dict=feed_dict(True)) train_writer.add_summary(summary,i) train_writer.close() test_writer.close()
然后再Terminal中运行
tensorboard --logdir=/usr/local/Cellar/anaconda/lib/python3.6/site-packages/tensorflow/examples/tutorials/mnist/logs/mnist_with_summaries
可以看到
WARNING:tensorflow:Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events. Overwriting the graph with the newest event. WARNING:tensorflow:Found more than one metagraph event per run. Overwriting the metagraph with the newest event. Starting TensorBoard b'54' at http://NewMac.local:6006 (Press CTRL+C to quit)
之后复制粘贴最后的网址即可进入TensorBoard观看各种数据形式。
相关文章推荐
- TensorFlow、TensorBoard使用学习记录
- TensorFlow学习(4):TensorBoard
- 深度学习框架TensorFlow学习与应用(五)——TensorBoard结构与可视化
- 学习TensorFlow,TensorBoard可视化网络结构和参数
- TensorFlow学习笔记----TensorBoard_1
- Tensorflow实战学习(三十九)【TensorBoard】
- TensorFlow学习笔记----TensorBoard_2
- tensorflow学习笔记十五:tensorflow官方文档学习 TensorBoard: Graph Visualization
- 学习TensorFlow,TensorBoard可视化网络结构和参数
- Tensorflow学习笔记:基础篇(7)——Mnist手写集改进版(Tensorboard可视化)
- Tensorflow学习笔记:基础篇(6)——Mnist手写集改进版(Optimizer与Tensorboard)
- TensorFlow学习--TensorBoard神经网络可视化
- tensorflow学习笔记:运行tensorboard遇到的错误
- Tensorflow实战学习(四十七)【PlayGround,TensorBoard】
- Tensorflow学习教程------tensorboard网络运行和可视化
- 【tensorflow学习】使用tensorboard实现数据可视化
- TensorFlow学习笔记(十二)TensorFLow tensorBoard 总结
- Tensorflow学习:Tensorboard可视化(二)
- TensorFlow学习笔记3——windows下采用Anaconda时使用tensorboard的方法
- [TensorFlow]入门学习笔记(6)-Tensorboard简易教程和模型保存