安装graphviz
使用Mac的brew安装即可,命令行:
brew install graphviz
查看graphviz安装到的路径
brew list graphviz
出现下图:
添加环境变量到路径
import os
os.environ["PATH"] += os.pathsep + '/usr/local/Cellar/graphviz'
运行代码
运行绘制模型plot_model代码之前,预先定义好一个model,例如:
from keras.models import Sequential
from tensorflow import *
from keras.layers.embeddings import Embedding
from keras.layers import Conv1D, MaxPooling1D, Flatten, Dropout, Dense, Input, Lambda,BatchNormalization
from keras.models import Model
model = Sequential()
model.add(Embedding(100001, 300, input_length=50)) #使用Embeeding层将每个词编码转换为词向量
model.add(Conv1D(256, 5, padding='same'))
model.add(MaxPooling1D(3, 3, padding='same'))
model.add(Conv1D(128, 5, padding='same'))
model.add(MaxPooling1D(3, 3, padding='same'))
model.add(Conv1D(64, 3, padding='same'))
model.add(Flatten())
model.add(Dropout(0.1))
model.add(BatchNormalization()) # (批)规范化层
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(10, activation='softmax'))
绘制模型图
from keras.utils import plot_model
import pydot
plot_model(model,to_file='CNNmodel.png',show_shapes=True,show_layer_names=False)
此处再补充一个: model.summary函数,可以也可以输出图形结构:
model.summary()