I am using Python3.5, Anaconda4.3 environment. This is the procedure for visualizing an LSTM model created with Keras with pydot + Graphviz on a Jupyter Notebook. The OS is CentOS 7.3.
Here is how to visualize the Keras model, but I will explain it including the necessary packages. https://keras.io/visualization/
Graphviz itself is a package that has nothing to do with Python. http://www.graphviz.org/About.php
Not limited to CentOS, it works on OS, so install it first.
sudo yum -y install graphviz
You need pydot and graphviz to visualize the model using graphviz in Python. This graphviz is a Python wrapper for OS packages.
pip install pydot graphviz
You have now installed it, but you may need the following to visualize it, depending on your environment:
pip install pydot3 pydot-ng
Launch Jupyter Notebook and write an LSTM model in Keras. This time, I will use the model here. http://machinelearningmastery.com/sequence-classification-lstm-recurrent-neural-networks-python-keras/
import numpy as np
import pydot
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from keras.utils.visualize_util import model_to_dot
from IPython.display import SVG
np.random.seed(7)
top_words = 5000
(x_train, y_train), (x_test, y_test) = imdb.load_data(nb_words=top_words)
max_review_length = 500
x_train = sequence.pad_sequences(x_train, maxlen=max_review_length)
x_test = sequence.pad_sequences(x_test, maxlen=max_review_length)
embedding_vector_length = 32
model = Sequential()
model.add(Embedding(top_words, embedding_vector_length, input_length=max_review_length))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#Now visualize the model.
SVG(model_to_dot(model).create(prog='dot', format='svg'))
model.fit(x_train, y_train, validation_data=(x_test, y_test), nb_epoch=3, batch_size=64)
scores = model.evaluate(x_test, y_test, verbose=0)
print ("accuracy: %.3f%%" % (scores[1]*100))
Now you can visualize the model as shown below.
Recommended Posts