日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tensorflow:Tensorboard使用

發(fā)布時間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow:Tensorboard使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

  • 目錄
  • 前言
  • 可視化第一步
  • 實(shí)現(xiàn)graphs
  • 后記

前言

TensorFlow是以流動的張量為名的神經(jīng)網(wǎng)絡(luò)開發(fā)庫,所以Google為了讓人們更直觀的了解流動的張量的含義,他們做了個TensorBoard讓我們直觀的看到我們寫的框架是怎么個流動法的(純屬YY)。好了玩笑話不說,Google是怎么定義TensorBoard的呢?

The computations you'll use TensorFlow for - like training a massive deep neural network - can be complex and confusing. To make it easier to understand, debug, and optimize TensorFlow programs, we've included a suite of visualization tools called TensorBoard. You can use TensorBoard to visualize your TensorFlow graph,plot quantitative metrics about the execution of your graph,and show additional data like images that pass through it.

大意其實(shí)就是我剛剛說的,我們使用TensorFlow寫的神經(jīng)網(wǎng)絡(luò)直接看上去會顯得十分的復(fù)雜和混亂,為了更加直觀的去調(diào)試、優(yōu)化我們的神經(jīng)網(wǎng)絡(luò),他們開發(fā)了TensorBoard–一套可視化工具,深度學(xué)習(xí)的可視化對于理解它究竟學(xué)到了什么有很大的幫助。

Tips:TensorBoard官方支持的瀏覽器是Chrome和Firefox,所以最好使用這兩個瀏覽器去開TensorBoard吧。

可視化第一步

效果圖

我們可以對其進(jìn)行展開,比如我們展開一個first_layer

可以看出來一個layer里面是多么復(fù)雜的運(yùn)算過程,其實(shí)我們代碼里也就幾行代碼,TensorFlow幫我們完成了很多事情。

看了效果之后有沒有非常想實(shí)現(xiàn)它?那么接下來我們就來學(xué)習(xí)下怎么去用TensorBoard去畫出這么一幅圖~

實(shí)現(xiàn)graphs

我們這里用的代碼是之前講TensorFlow入門里最后的擬合曲線的代碼,代碼如下:

import tensorflow as tf import numpy as np import matplotlib.pyplot as plt def add_layer(layoutname,inputs,in_size,out_size,activatuib_funaction=None):Weights=tf.Variable(tf.random_normal([in_size,out_size]))biases=tf.Variable(tf.zeros([1,out_size]))+0.1Wx_plus_b=tf.matmul(inputs,Weights)+biasesif activatuib_funaction is None:outputs=Wx_plus_belse :outputs=activatuib_funaction(Wx_plus_b)return outputsx_data=np.linspace(-1,1,300)[:,np.newaxis] noise=np.random.normal(0,0.05,x_data.shape) y_data=np.square(x_data)-0.5+noisexs=tf.placeholder(tf.float32,[None,1]) ys=tf.placeholder(tf.float32,[None,1])l1=add_layer('first_layer',xs,1,10,activatuib_funaction=tf.nn.relu) prediction =add_layer('secend_layer',l1,10,1,activatuib_funaction=None)loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)init=tf.global_variables_initializer()with tf.Session() as sess:fig=plt.figure()ax=fig.add_subplot(1,1,1)ax.scatter(x_data,y_data)plt.show(block=False)sess.run(init)for train in range(1000):sess.run(train_step,feed_dict={xs:x_data,ys:y_data})if train%50==0:try:ax.lines.remove(lines[0])except Exception:passprint train,sess.run(loss,feed_dict={xs:x_data,ys:y_data})prediction_value=sess.run(prediction,feed_dict={xs:x_data})lines=ax.plot(x_data, prediction_value,'r-',lw=5)plt.pause(0.1)

我們需要對代碼進(jìn)行寫改動,讓其生成Graphs。

從最開始的縮略圖,我們可以看到我們有input、兩個layer、一個loss、一個train,那我們第一步先來構(gòu)建這幾個東西。

我們想要構(gòu)建這些大的框架,只需要我們在想要顯示的地方加上這句話with tf.name_scope(‘顯示的名字’):然后縮進(jìn)想要放在里面的代碼。如input

我們想要顯示的效果如下圖

我們需要用with tf.name_scope(‘input’):把兩個輸入值縮進(jìn)進(jìn)去:

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")

x_input、y_input這兩個顯示值我們只需要在聲明的時候在最后加一個name=’顯示名’就可以顯示出上面的效果了。

然后兩個layer也是類似,由于我們定義了一個方法去添加層,可是如果我們不給他名字,生成graphs的時候layer就是layer、layer_1、layer_2這樣子,有點(diǎn)不好看,所以我們傳入一個layername。

def add_layer(layoutname,inputs,in_size,out_size,activatuib_funaction=None,):with tf.name_scope(layoutname):with tf.name_scope('weights'):Weights=tf.Variable(tf.random_normal([in_size,out_size]),name='W')with tf.name_scope('biases'):biases=tf.Variable(tf.zeros([1,out_size])+0.1,name='b')with tf.name_scope('Wx_plus_b'):Wx_plus_b=tf.add(tf.matmul(inputs,Weights),biases)if activatuib_funaction is None:outputs=Wx_plus_belse :outputs=activatuib_funaction(Wx_plus_b)return outputs

layer圖層里面還有weights和biases、Wx_plus_b等,所以他們也是小圖層,所以我們對他們也要單獨(dú)定義,另外給weights,biases給個name值。 激勵函數(shù)這些會自動生成,我們不需要去管。

loss和train也是類似,寫完tf.name_scope(”):之后,我們還需要寫文件,寫文件的代碼TensorFlow也幫我們封裝好了,我們只需要調(diào)用writer=tf.summary.FileWriter(“文件保存路徑如:logs/”,sess.graph),完整代碼:

import tensorflow as tf import numpy as np def add_layer(layoutname,inputs,in_size,out_size,activatuib_funaction=None,):with tf.name_scope(layoutname):with tf.name_scope('weights'):Weights=tf.Variable(tf.random_normal([in_size,out_size]),name='W')with tf.name_scope('biases'):biases=tf.Variable(tf.zeros([1,out_size])+0.1,name='b')with tf.name_scope('Wx_plus_b'):Wx_plus_b=tf.add(tf.matmul(inputs,Weights),biases)if activatuib_funaction is None:outputs=Wx_plus_belse :outputs=activatuib_funaction(Wx_plus_b)return outputs x_data=np.linspace(-1,1,300)[:,np.newaxis] noise=np.random.normal(0,0.09,x_data.shape) y_data=np.square(x_data)-0.05+noise with tf.name_scope('inputs'):xs=tf.placeholder(tf.float32,[None,1],name="x_in")ys=tf.placeholder(tf.float32,[None,1],name="y_in") l1=add_layer("first_layer",xs,1,10,activatuib_funaction=tf.nn.relu) prediction =add_layer('second_layout',l1,10,1,activatuib_funaction=None) with tf.name_scope('loss'):loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1])) with tf.name_scope('train'):train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)init=tf.global_variables_initializer()with tf.Session() as sess:writer=tf.summary.FileWriter("logs/",sess.graph)sess.run(init)

由于Graphs展示的是整個神經(jīng)網(wǎng)絡(luò)的框架,所以我們可以刪去測試數(shù)據(jù)。

到這里代碼已經(jīng)寫好了,運(yùn)行代碼,生成文件,我們會發(fā)現(xiàn)項目的logs目錄下會多一個文件

生成文件之后,首先是進(jìn)入cmd,輸入下面的指令來啟動TensorBoard

python tensorflow/tensorboard/tensorboard.py –logdir=path/to/log-directory

這里的參數(shù) logdir 指向 SummaryWriter 序列化數(shù)據(jù)的存儲路徑。如果logdir目錄的子目錄中包含另一次運(yùn)行時的數(shù)據(jù),那么 TensorBoard 會展示所有運(yùn)行的數(shù)據(jù)。一旦 TensorBoard 開始運(yùn)行,你可以通過在瀏覽器中輸入 localhost:6006 來查看 TensorBoard。

如果你已經(jīng)通過pip安裝了 TensorBoard,你可以通過執(zhí)行更為簡單地命令來訪問 TensorBoard

tensorboard –logdir=/path/to/log-directory

進(jìn)入 TensorBoard 的界面時,你會在右上角看到導(dǎo)航選項卡,每一個選項卡將展現(xiàn)一組可視化的序列化數(shù)據(jù)集 。對于你查看的每一個選項卡,如果 TensorBoard 中沒有數(shù)據(jù)與這個選項卡相關(guān)的話,則會顯示一條提示信息指示你如何序列化相關(guān)數(shù)據(jù)。
注意命令:tensorboard –logdir=E:/tensorFlow/Mycode/logs/
然后我們就可以訪問127.0.0.1:6006去查看TensorBoard,如果沒有數(shù)據(jù)的話就刷新一下。到此,我們就成功的使用可視化的方式查看我們的神經(jīng)網(wǎng)絡(luò)框架了。

后記

TensorFlow配合TensorBoard的Graphs來構(gòu)建我們的框架確實(shí)非常好用,牛逼的Google肯定不會只賦予TensorBoard這一個功能,他能監(jiān)控我們的訓(xùn)練過程,讓我們可視化的看到訓(xùn)練過程中參數(shù)的變化,甚至是圖像、音頻的變化,還能展示訓(xùn)練過程的數(shù)據(jù)分布圖,所以tensorboard、TensorFlow博大精深,慢慢學(xué)習(xí)吧!
PS:感謝周莫煩大神出的[機(jī)器學(xué)習(xí)系列]
需要翻墻觀看

總結(jié)

以上是生活随笔為你收集整理的Tensorflow:Tensorboard使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。