Tensorflow 自帶可視化Tensorboard使用方法(附項目代碼)
Tensorboard:
如何更直觀的觀察數(shù)據(jù)在神經網(wǎng)絡中的變化,或是已經構建的神經網(wǎng)絡的結構。上一篇文章說到,可以使用matplotlib第三方可視化,來進行一定程度上的可視化。然而Tensorflow也自帶了可視化模塊Tensorboard,并且能更直觀的看見整個神經網(wǎng)絡的結構。
上面的結構圖甚至可以展開,變成:
使用:
結構圖:
with tensorflow .name_scope(layer_name):
直接使用以上代碼生成一個帶可展開符號的一個域,并且支持嵌套操作:
with tf.name_scope(layer_name): with tf.name_scope('weights'):
節(jié)點一般是變量或常量,需要加一個“name=‘'”參數(shù),才會展示和命名,如:
with tf.name_scope('weights'): Weights = tf.Variable(tf.random_normal([in_size,out_size]))
結構圖符號及意義:
變量:
變量則可使用Tensorflow.histogram_summary()方法:
tf.histogram_summary(layer_name+"/weights",Weights) #name命名,Weights賦值
常量:
常量則可使用Tensorflow.scalar_summary()方法:
tf.scalar_summary('loss',loss) #命名和賦值
展示:
最后需要整合和存儲SummaryWriter:
#合并到Summary中 merged = tf.merge_all_summaries() #選定可視化存儲目錄 writer = tf.train.SummaryWriter("/目錄",sess.graph)
merged也是需要run的,因此還需要:
result = sess.run(merged) #merged也是需要run的 writer.add_summary(result,i)
執(zhí)行:
運行后,會在相應的目錄里生成一個文件,執(zhí)行:
tensorboard --logdir="/目錄"
會給出一段網(wǎng)址:
瀏覽器中打開這個網(wǎng)址即可,因為有兼容問題,firefox并不能很好的兼容,建議使用Chrome。
常量在Event中,結構圖在Graphs中,變量在最后兩個Tag中。
附項目代碼:
項目承接自上一篇文章(已更新至最新Tensorflow版本API r1.2):
import tensorflow as tf import numpy as np def add_layer(inputs,in_size,out_size,n_layer,activation_function=None): #activation_function=None線性函數(shù) layer_name="layer%s" % n_layer with tf.name_scope(layer_name): with tf.name_scope('weights'): Weights = tf.Variable(tf.random_normal([in_size,out_size])) #Weight中都是隨機變量 tf.summary.histogram(layer_name+"/weights",Weights) #可視化觀看變量 with tf.name_scope('biases'): biases = tf.Variable(tf.zeros([1,out_size])+0.1) #biases推薦初始值不為0 tf.summary.histogram(layer_name+"/biases",biases) #可視化觀看變量 with tf.name_scope('Wx_plus_b'): Wx_plus_b = tf.matmul(inputs,Weights)+biases #inputs*Weight+biases tf.summary.histogram(layer_name+"/Wx_plus_b",Wx_plus_b) #可視化觀看變量 if activation_function is None: outputs = Wx_plus_b else: outputs = activation_function(Wx_plus_b) tf.summary.histogram(layer_name+"/outputs",outputs) #可視化觀看變量 return outputs #創(chuàng)建數(shù)據(jù)x_data,y_data x_data = np.linspace(-1,1,300)[:,np.newaxis] #[-1,1]區(qū)間,300個單位,np.newaxis增加維度 noise = np.random.normal(0,0.05,x_data.shape) #噪點 y_data = np.square(x_data)-0.5+noise with tf.name_scope('inputs'): #結構化 xs = tf.placeholder(tf.float32,[None,1],name='x_input') ys = tf.placeholder(tf.float32,[None,1],name='y_input') #三層神經,輸入層(1個神經元),隱藏層(10神經元),輸出層(1個神經元) l1 = add_layer(xs,1,10,n_layer=1,activation_function=tf.nn.relu) #隱藏層 prediction = add_layer(l1,10,1,n_layer=2,activation_function=None) #輸出層 #predition值與y_data差別 with tf.name_scope('loss'): loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1])) #square()平方,sum()求和,mean()平均值 tf.summary.scalar('loss',loss) #可視化觀看常量 with tf.name_scope('train'): train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) #0.1學習效率,minimize(loss)減小loss誤差 init = tf.initialize_all_variables() sess = tf.Session() #合并到Summary中 merged = tf.summary.merge_all() #選定可視化存儲目錄 writer = tf.summary.FileWriter("Desktop/",sess.graph) sess.run(init) #先執(zhí)行init #訓練1k次 for i in range(1000): sess.run(train_step,feed_dict={xs:x_data,ys:y_data}) if i%50==0: result = sess.run(merged,feed_dict={xs:x_data,ys:y_data}) #merged也是需要run的 writer.add_summary(result,i) #result是summary類型的,需要放入writer中,i步數(shù)(x軸)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用rpclib進行Python網(wǎng)絡編程時的注釋問題
這篇文章主要介紹了使用rpclib進行Python網(wǎng)絡編程時的注釋問題,作者講到了自己在編寫服務器時要用unicode注釋等需要注意的地方,需要的朋友可以參考下2015-05-05python利用xpath爬取網(wǎng)上數(shù)據(jù)并存儲到django模型中
這篇文章主要介紹了python利用xpath爬取網(wǎng)上數(shù)據(jù)并存儲到django模型中,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Python中的startswith和endswith函數(shù)使用實例
這篇文章主要介紹了Python中的startswith和endswith函數(shù)使用實例,特別是endswith函數(shù),有了它,判斷文件的擴展名、文件的類型在容易不過了,需要的朋友可以參考下2014-08-08windows及l(fā)inux環(huán)境下永久修改pip鏡像源的方法
不知道有沒有人跟我一樣,在剛接觸Linux時被系統(tǒng)更新源問題搞得暈頭轉向,不同的Linux更新源配置也是不一樣的,另外由于默認安裝時的源大都是外國的更新源,速度相對國內會慢很多,接下來本文主要介紹在windows和linux兩種系統(tǒng)環(huán)境中更新系統(tǒng)源的方法。2016-11-11使用Python對接OpenAi?API實現(xiàn)智能QQ機器人的方法
這篇文章主要介紹了使用Python對接OpenAi?API實現(xiàn)智能QQ機器人的方法,主要是提供一個方法思路,可以根據(jù)實現(xiàn)代碼延申出更多的解決方法,需要的朋友可以參考下2023-03-03