[PYTHON] Explicitly add an Input Layer in Keras' Sequential model

Added Input Layer

from keras.models import Sequential
from keras.layers import InputLayer

model = Sequential()
model.add(InputLayer(input_shape=(784,)))

This is all, but since it was not mentioned in the Sequential model tutorial, I will write it as an article.

Confirmed with Python 3.5.2, Keras 1.1.2.

No InputLayer

from keras.models import Sequential
from keras.layers.core import Dense

model = Sequential()
model.add(Dense(256, activation='relu', input_dim=784))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='relu'))

How to write according to the tutorial. In this case, the shape of the input layer is defined in the intermediate layer. Also, if you comment out the first add to remove the Dense (256) layer, The input shape will be undefined.

With InputLayer

from keras.models import Sequential
from keras.layers import InputLayer
from keras.layers.core import Dense

model = Sequential()
model.add(InputLayer(input_shape=(784,)))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='relu'))

The flow of input layer → intermediate layer → output layer has become easier to understand.

Recommended Posts

Explicitly add an Input Layer in Keras' Sequential model
Visualize Keras model in Python 3.5
Add a layer using the Keras backend
Getting started with Keras Sequential model Japanese translation