Visualize Keras model in Python 3.5

Purpose

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/

procedure

  1. Install graphviz on the OS with yum.
  2. Install pydot and graphviz with pip.
  3. Visualize the model with Jupyter Notebook.

Install graphviz on OS with yum

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

Install pydot and graphviz with pip

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

Visualize your model with Jupyter Notebook

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. 2017-02-04_graphviz.PNG

Recommended Posts

Visualize Keras model in Python 3.5
General Gaussian state-space model in Python
Custom state space model in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
I tried to implement TOPIC MODEL in Python
Create a simple momentum investment model in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python