从函数式 API 构建的模型的 Keras 可视化 [英] Keras Visualization of Model Built from Functional API

查看:19
本文介绍了从函数式 API 构建的模型的 Keras 可视化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下是否有一种简单的方法来可视化基于 Functional API 构建的 Keras 模型?

I wanted to ask if there was an easy way to visualize a Keras model built from the Functional API?

现在,对我来说,在高级别的顺序模型上调试的最佳方法是:

Right now, the best ways to debug at a high level a sequential model for me is:

model = Sequential()
model.add(...
...

print(model.summary())
SVG(model_to_dot(model).create(prog='dot', format='svg'))

然而,如果我们构建一个更复杂的非序列模型,我很难找到一种可视化 Keras API 的好方法.

However, I am having a hard time finding a good way to visualize the Keras API if we build a more complex, non-sequential model.

推荐答案

是的,请尝试检查具有方法 plot_model()keras.utils 如解释详情这里.似乎您已经熟悉 keras.utils.vis_utilsmodel_to_dot 方法,但这是另一种选择.它的用法类似于:

Yes there is, try checking the keras.utils which has a method plot_model() as explained on detail here. Seems that you already are familiar with keras.utils.vis_utils and the model_to_dot method, but this is another option. It's usage is something like:

from keras.utils import plot_model
plot_model(model, to_file='model.png')

老实说,这是我找到的最好的仅使用 Keras 的方法.像您一样使用 model.summary() 有时也很有用.我还希望有一些工具可以更好地可视化一个人的模型,甚至可能能够看到每层的权重来决定最佳网络结构和初始化(如果你知道一个,请告诉 :] ).

To be honest, that is the best I have managed to find using Keras only. Using model.summary() as you did is also useful sometimes. I also wished there were some tool to enable for better visualization of one's models, perhaps even to be able to see the weights per layers as to decide on optimal network structures and initializations (if you know about one please tell :] ).

可能您目前拥有的最佳选择是在 Tensorboard,您可以通过 TensorBoard 回调将其包含在 Keras 中.这使您能够可视化您的训练和感兴趣的指标,以及有关层激活、偏差和内核等的一些信息.基本上,您必须在拟合模型之前将此代码添加到您的程序中:

Probably the best option you currently have is to visualize things on Tensorboard, which you an include in Keras with the TensorBoard Callback. This enables you to visualize your training and the metrics of interest, as well as some info on activations of your layers,your biases and kernels, etc.. Basically you have to add this code to your program, before fitting your model:

from keras.callbacks import TensorBoard
#indicate folder to save, plus other options
tensorboard = TensorBoard(log_dir='./logs/run1', histogram_freq=1,
    write_graph=True, write_images=False)  

#save it in your callback list, where you can include other callbacks
callbacks_list = [tensorboard]
#then pass to fit as callback, remember to use validation_data also
regressor.fit(X, Y, callbacks=callbacks_list, epochs=64, 
    validation_data=(X_test, Y_test), shuffle=True)

然后,您可以在终端上使用以下命令运行 Tensorboard(在 Web 服务上本地运行):

You can then run Tensorboard (which runs locally on a webservice) with the following command on your terminal:

tensorboard --logdir=/logs/run1

这将指示您在哪个端口可视化您的训练.如果您有不同的运行,您可以通过 --logdir=/logs 代替,以便能够将它们一起可视化以进行比较.Tensorboard的使用当然有更多选择,所以如果您正在考虑使用它,我建议您检查包含的链接.

This will then indicate you in which port to visualize your training. If you got different runs you can pass --logdir=/logs instead to be able to visualize them together for comparison. There are of course more options on the use of Tensorboard, so I suggest you check the included links if you are considering its use.

这篇关于从函数式 API 构建的模型的 Keras 可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆