Keras从功能API可视化模型的可视化 [英] Keras Visualization of Model Built from Functional API

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

问题描述

我想问一下是否有一种简便的方法来可视化通过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,详细说明

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 回调.这使您可以可视化训练和感兴趣的指标,以及有关图层激活,偏差和内核等的一些信息.基本上,在拟合模型之前,必须将以下代码添加到程序中:

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时还有更多选择,因此,如果您考虑使用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.

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

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