macOS下实现Keras模型的可视化及错误处理

in #graphviz7 years ago

第一部分:如何实现可视化

第一种:利用 plot_model 导出模型结构图

keras.utils.vis_utils模块提供了画出Keras模型的函数,依赖于graphviz。

该函数将已经设计好的模型画成结构图,并保存成图片:

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

导出的模型图

plot_model接收两个可选参数:
show_shapes:指定是否显示输出数据的形状,默认为False
show_layer_names:指定是否显示层名称,默认为True

第二种:在ipython直接展示模型结构图
我们也可以直接获取一个pydot.Graph对象,然后按照自己的需要配置它,例如,如果要在ipython中展示图片

from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(model).create(prog='dot', format='svg'))

如下图所示:

在ipython直接展示的模型结构图

如何处理错误

由于上述代码依赖 pydot 和 graphviz,未安装就会出现错误。

  1. 安装 graphviz
    第一步:获取权限
sudo chown -R `whoami`:admin /usr/local/bin
sudo chown -R `whoami`:admin /usr/local/share

第二步:安装

brew install graphviz
brew link --overwrite graphviz
  1. 安装pydot
pip install pydot

完成第二部分的“如何处理错误”,是不是第一部分的代码就不报错了?

完成!撒花🎉

参考文章:https://keras-cn.readthedocs.io/en/latest/other/visualization/https://keras-cn.readthedocs.io/en/latest/other/visualization/

http://blog.csdn.net/qq_36477562/article/details/79324577

https://stackoverflow.com/questions/26647412/homebrew-could-not-symlink-usr-local-bin-is-not-writable

Sort:  

Congratulations @weichencoder! You received a personal award!

1 Year on Steemit

Click here to view your Board of Honor

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @weichencoder! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.28
TRX 0.21
JST 0.039
BTC 96063.98
ETH 3668.51
SBD 3.84