脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - tensorflow可视化Keras框架中Tensorboard使用示例

tensorflow可视化Keras框架中Tensorboard使用示例

2022-12-21 11:35Bubbliiiing Python

这篇文章主要为大家介绍了tensorflow可视化Keras框架中Tensorboard使用示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

Tensorboard详解

该类在存放在keras.callbacks模块中。拥有许多参数,主要的参数如下:

1、log_dir: 用来保存Tensorboard的日志文件等内容的位置

2、histogram_freq: 对于模型中各个层计算激活值和模型权重直方图的频率。

3、write_graph: 是否在 TensorBoard 中可视化图像。

4、write_grads: 是否在 TensorBoard 中可视化梯度值直方图。

5、batch_size: 用以直方图计算的传入神经元网络输入批的大小。

6、write_images: 是否在 TensorBoard中将模型权重以图片可视化。

7、update_freq: 常用的三个值为’batch’ 、 ‘epoch’ 或 整数。当使用 ‘batch’ 时,在每个 batch 之后将损失和评估值写入到 TensorBoard 中。 ‘epoch’ 类似。如果使用整数,会在每一定个样本之后将损失和评估值写入到 TensorBoard 中。

默认值如下:

log_dir="./logs",  # 默认保存在当前文件夹下的logs文件夹之下
histogram_freq=0,
batch_size=32,
write_graph=True,  #默认是True,默认是显示graph的。
write_grads=False,
write_images=False,
update_freq="epoch"

使用例子

以手写体为例子,我们打开histogram_freq和write_grads,也就是在Tensorboard中保存权值直方图和梯度直方图。

打开CMD,利用tensorboard --logdir=logs生成tensorboard观测网页。

1、loss和acc

tensorflow可视化Keras框架中Tensorboard使用示例

tensorflow可视化Keras框架中Tensorboard使用示例

2、权值直方图

tensorflow可视化Keras框架中Tensorboard使用示例

3、梯度直方图

tensorflow可视化Keras框架中Tensorboard使用示例

实现代码

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使用示例的详细内容,更多关于Keras Tensorboard可视化的资料请关注服务器之家其它相关文章!

原文地址:https://blog.csdn.net/weixin_44791964/article/details/105002793

延伸 · 阅读

精彩推荐