Tensorflow 自帶可視化Tensorboard使用方法(附項(xiàng)目代碼)
Tensorboard:
如何更直觀的觀察數(shù)據(jù)在神經(jīng)網(wǎng)絡(luò)中的變化,或是已經(jīng)構(gòu)建的神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)。上一篇文章說(shuō)到,可以使用matplotlib第三方可視化,來(lái)進(jìn)行一定程度上的可視化。然而Tensorflow也自帶了可視化模塊Tensorboard,并且能更直觀的看見(jiàn)整個(gè)神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)。

上面的結(jié)構(gòu)圖甚至可以展開,變成:

使用:
結(jié)構(gòu)圖:
with tensorflow .name_scope(layer_name):
直接使用以上代碼生成一個(gè)帶可展開符號(hào)的一個(gè)域,并且支持嵌套操作:
with tf.name_scope(layer_name):
with tf.name_scope('weights'):
節(jié)點(diǎn)一般是變量或常量,需要加一個(gè)“name=‘'”參數(shù),才會(huì)展示和命名,如:
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size,out_size]))

結(jié)構(gòu)圖符號(hào)及意義:

變量:
變量則可使用Tensorflow.histogram_summary()方法:
tf.histogram_summary(layer_name+"/weights",Weights) #name命名,Weights賦值

常量:
常量則可使用Tensorflow.scalar_summary()方法:
tf.scalar_summary('loss',loss) #命名和賦值

展示:
最后需要整合和存儲(chǔ)SummaryWriter:
#合并到Summary中
merged = tf.merge_all_summaries()
#選定可視化存儲(chǔ)目錄
writer = tf.train.SummaryWriter("/目錄",sess.graph)
merged也是需要run的,因此還需要:
result = sess.run(merged) #merged也是需要run的 writer.add_summary(result,i)
執(zhí)行:
運(yùn)行后,會(huì)在相應(yīng)的目錄里生成一個(gè)文件,執(zhí)行:
tensorboard --logdir="/目錄"
會(huì)給出一段網(wǎng)址:

瀏覽器中打開這個(gè)網(wǎng)址即可,因?yàn)橛屑嫒輪?wèn)題,firefox并不能很好的兼容,建議使用Chrome。

常量在Event中,結(jié)構(gòu)圖在Graphs中,變量在最后兩個(gè)Tag中。
附項(xiàng)目代碼:
項(xiàng)目承接自上一篇文章(已更新至最新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中都是隨機(jī)變量
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個(gè)單位,np.newaxis增加維度
noise = np.random.normal(0,0.05,x_data.shape) #噪點(diǎn)
y_data = np.square(x_data)-0.5+noise
with tf.name_scope('inputs'): #結(jié)構(gòu)化
xs = tf.placeholder(tf.float32,[None,1],name='x_input')
ys = tf.placeholder(tf.float32,[None,1],name='y_input')
#三層神經(jīng),輸入層(1個(gè)神經(jīng)元),隱藏層(10神經(jīng)元),輸出層(1個(gè)神經(jīng)元)
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學(xué)習(xí)效率,minimize(loss)減小loss誤差
init = tf.initialize_all_variables()
sess = tf.Session()
#合并到Summary中
merged = tf.summary.merge_all()
#選定可視化存儲(chǔ)目錄
writer = tf.summary.FileWriter("Desktop/",sess.graph)
sess.run(init) #先執(zhí)行init
#訓(xùn)練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軸)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中有關(guān)時(shí)間日期格式轉(zhuǎn)換問(wèn)題
這篇文章主要介紹了python中有關(guān)時(shí)間日期格式轉(zhuǎn)換問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Python unittest 自動(dòng)識(shí)別并執(zhí)行測(cè)試用例方式
這篇文章主要介紹了Python unittest 自動(dòng)識(shí)別并執(zhí)行測(cè)試用例方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Python一行代碼實(shí)現(xiàn)打開各種類型的文件
在處理大量文件時(shí),手動(dòng)一個(gè)個(gè)打開是不是很麻煩,這時(shí)候,Python的os.startfile()就是你的救星啦,本文我們就來(lái)看看如何一行代碼打開各種類型的文件吧2024-12-12
python使用smtplib模塊通過(guò)gmail實(shí)現(xiàn)郵件發(fā)送的方法
這篇文章主要介紹了python使用smtplib模塊通過(guò)gmail實(shí)現(xiàn)郵件發(fā)送的方法,涉及Python使用smtplib模塊發(fā)送郵件的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05
Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值詳解
在實(shí)際處理數(shù)據(jù)中,數(shù)據(jù)預(yù)處理操作中,常常需要去除掉重復(fù)的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值的相關(guān)資料,需要的朋友可以參考下2022-07-07
pytorch 模型的train模式與eval模式實(shí)例
今天小編就為大家分享一篇pytorch 模型的train模式與eval模式實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

