Tensorflow学习教程------tensorboard网络运行和可视化
生活随笔
收集整理的這篇文章主要介紹了
Tensorflow学习教程------tensorboard网络运行和可视化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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'): #定義兩個placeholderx = 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 寫入到writeracc = 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的變化折線圖
?
轉載于:https://www.cnblogs.com/cnugis/p/7641133.html
總結
以上是生活随笔為你收集整理的Tensorflow学习教程------tensorboard网络运行和可视化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# DirectoryInfo 类和
- 下一篇: 全栈工程师之路-中级篇之小程序开发-第二