概要
Kerasで構築したニューラルネットワークのモデルを可視化するためにplot_model
を使ってみたいと思います。
keras.utils.plot_model(model, show_shapes=True)
全結合
import keras
inputs = keras.layers.Input(shape=(64,))
x = keras.layers.Dense(64, activation='relu')(inputs)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Dense(10, activation=None)(x)
predictions = keras.layers.Activation('softmax')(x)
model = keras.Model(inputs=inputs, outputs=predictions)
keras.utils.plot_model(model, show_shapes=True)
CNN
import keras
inputs = keras.layers.Input(shape=(28, 28, 1))
x = keras.layers.Conv2D(20, kernel_size=5, padding="same", input_shape=(28, 28, 1))(inputs)
x = keras.layers.Activation('relu')(x)
x = keras.layers.MaxPooling2D(pool_size=(2, 2))(x)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(500)(x)
x = keras.layers.Activation('relu')(x)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Dense(10)(x)
predictions = keras.layers.Activation('softmax')(x)
model = keras.Model(inputs=inputs, outputs=predictions)
keras.utils.plot_model(model, show_shapes=True)
LSTM
import keras
inputs = keras.layers.Input(shape=(50,1))
x = keras.layers.LSTM(128)(inputs)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Dense(1)(x)
predictions = keras.layers.Activation('linear')(x)
model = keras.Model(inputs=inputs, outputs=predictions)
keras.utils.plot_model(model, show_shapes=True)