12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

KerasのSequentialモデルでInputLayerを明示的に追加する

Last updated at Posted at 2016-12-10

#InputLayer(入力層)を追加

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

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

これだけですが,Sequential modelチュートリアルで触れられていないので記事にしておきます.

Python 3.5.2, Keras 1.1.2 にて確認しました.

###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'))

チュートリアルの通りの書き方です.
この場合,中間層の中で入力層の形状を定義することになってしまいます.
また,Dense(256)層を消すために最初のaddをコメントアウトすると,
入力形状が未定義になってしまいます.

###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'))

入力層→中間層→出力層の流れがわかりやすくなりましたね.

12
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?