tensorflow可視化Keras框架中Tensorboard使用示例
Tensorboard詳解
該類在存放在keras.callbacks模塊中。擁有許多參數(shù),主要的參數(shù)如下:
1、log_dir: 用來保存Tensorboard的日志文件等內(nèi)容的位置
2、histogram_freq: 對于模型中各個層計算激活值和模型權(quán)重直方圖的頻率。
3、write_graph: 是否在 TensorBoard 中可視化圖像。
4、write_grads: 是否在 TensorBoard 中可視化梯度值直方圖。
5、batch_size: 用以直方圖計算的傳入神經(jīng)元網(wǎng)絡(luò)輸入批的大小。
6、write_images: 是否在 TensorBoard中將模型權(quán)重以圖片可視化。
7、update_freq: 常用的三個值為’batch’ 、 ‘epoch’ 或 整數(shù)。當(dāng)使用 ‘batch’ 時,在每個 batch 之后將損失和評估值寫入到 TensorBoard 中。 ‘epoch’ 類似。如果使用整數(shù),會在每一定個樣本之后將損失和評估值寫入到 TensorBoard 中。
默認(rèn)值如下:
log_dir='./logs', # 默認(rèn)保存在當(dāng)前文件夾下的logs文件夾之下 histogram_freq=0, batch_size=32, write_graph=True, #默認(rèn)是True,默認(rèn)是顯示graph的。 write_grads=False, write_images=False, update_freq='epoch'
使用例子
以手寫體為例子,我們打開histogram_freq和write_grads,也就是在Tensorboard中保存權(quán)值直方圖和梯度直方圖。
打開CMD,利用tensorboard --logdir=logs生成tensorboard觀測網(wǎng)頁。
1、loss和acc


2、權(quán)值直方圖

3、梯度直方圖

實現(xiàn)代碼
import numpy as np
from keras.layers import Input, Dense, Dropout, Activation,Conv2D,MaxPool2D,Flatten
from keras.datasets import mnist
from keras.models import Model
from keras.utils import to_categorical
from keras.callbacks import TensorBoard
if __name__=="__main__":
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train=np.expand_dims(x_train,axis=-1)
x_test=np.expand_dims(x_test,axis=-1)
y_train=to_categorical(y_train,num_classes=10)
y_test=to_categorical(y_test,num_classes=10)
batch_size=128
epochs=10
inputs = Input([28,28,1])
x = Conv2D(32, (5,5), activation='relu')(inputs)
x = Conv2D(64, (5,5), activation='relu')(x)
x = MaxPool2D(pool_size=(2,2))(x)
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(10, activation='softmax')(x)
model = Model(inputs,x)
model.compile(loss='categorical_crossentropy', optimizer="adam",metrics=['acc'])
Tensorboard= TensorBoard(log_dir="./model", histogram_freq=1,write_grads=True)
history=model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, shuffle=True, validation_split=0.2,callbacks=[Tensorboard])
以上就是tensorflow可視化Keras框架中Tensorboard使用示例的詳細(xì)內(nèi)容,更多關(guān)于Keras Tensorboard可視化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?matplotlib如何簡單繪制不同類型的表格
通過Matplotlib,開發(fā)者可以僅需要幾行代碼,便可以生成繪圖,直方圖,功率譜,條形圖,錯誤圖,散點圖等,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib如何簡單繪制不同類型表格的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python基于pywinauto實現(xiàn)的自動化采集任務(wù)
這篇文章主要介紹了Python基于pywinauto實現(xiàn)的自動化采集任務(wù),模擬了輸入單詞, 復(fù)制例句, 獲取例句, 清空剪切板, 然后重復(fù)這個操作,需要的朋友可以參考下2023-04-04
使用pyqt5 實現(xiàn)ComboBox的鼠標(biāo)點擊觸發(fā)事件
這篇文章主要介紹了使用pyqt5 實現(xiàn)ComboBox的鼠標(biāo)點擊觸發(fā)事件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
基于Matplotlib?調(diào)用?pyplot?模塊中?figure()?函數(shù)處理?figure圖形對象
這篇文章主要介紹了基于Matplotlib?調(diào)用?pyplot?模塊中?figure()?函數(shù)處理?figure圖形對象,matplotlib.pyplot模塊能夠快速地生成圖像,但如果使用面向?qū)ο蟮木幊趟枷?,我們就可以更好地控制和自定義圖像,下面就來詳細(xì)介紹其內(nèi)容,需要的朋友可以參考下2022-02-02

