我正在尝试使用Keras可视化模块:
# Begin a model
model = Sequential()
model.add(Convolution2D(4,1,5,input_shape=(1,1,49),init='uniform',weights=None,border_mode='valid') )
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(Flatten())
model.add(Dense(600,init='normal'))
model.add(Activation('tanh'))
model.add(Dense(2, init='normal'))
model.add(Activation('softmax'))
model.summary()
sgd = SGD(lr=list_lr[i], decay=0.0, momentum=0.1, nesterov=False)
model.compile(loss='categorical_crossentropy', metrics=['accuracy'],optimizer=sgd)
# using visualization
from keras.utils.visualize_util import plot
plot(model, to_file='/home/wj/DL/model.png')
model.fit(X_train,train_label, batch_size=list_batch[j], nb_epoch=list_epoch[k],shuffle=False,verbose=2,validation_split=0.2)
我得到以下错误:
Traceback (most recent call last):
File "6.2.4.cnn.py", line 85, in <module>
plot(model, to_file='/home/wj/DL/model.png')
File "/usr/local/lib/python3.4/dist-packages/keras/utils/visualize_util.py", line 67, in plot dot.write_png(to_file)
File "/usr/local/lib/python3.4/dist-packages/pydot.py", line 1809, in <lambda>
lambda path, f=frmt, prog=self.prog : self.write(path, format=f, prog=prog))
File "/usr/local/lib/python3.4/dist-packages/pydot.py", line 1895, in write dot_fd = file(path, "w+b")
NameError: name 'file' is not defined
我做错了什么?
以您的模型为例。
导入并定义您的模型。
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D,Flatten
model = Sequential()
model.add(Convolution2D(4,1,5,input_shape=(1,1,49),init='uniform',weights=None,border_mode='valid') )
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(Flatten())
model.add(Dense(600,init='normal'))
model.add(Activation('tanh'))
model.add(Dense(2, init='normal'))
model.add(Activation('softmax'))
在绘制模型之前,确保你已经安装了库:安装pydot,
pip install git+https://github.com/nlhepler/pydot.git
和graphviz,
sudo apt-get install graphviz
然后,导入必要的库并调用lib。来绘制你的模型
from IPython.display import Image, display, SVG
from keras.utils.visualize_util import model_to_dot
# Show the model in ipython notebook
figure = SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
display(figure)
# Save the model as png file
from keras.utils.visualize_util import plot
plot(model, to_file='model.png', show_shapes=True)