0
0

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 1 year has passed since last update.

ValueError: You are trying to load a weight file containing 2 layers into a model with 0 layers

Posted at

kerasにてモデルを保存したh5を読み込もうとするとValueError: You are trying to load a weight file containing 2 layers into a model with 0 layersというエラーが発生しました。

from keras.models import load_model
model.save('model.h5')
model = load_model('model.h5')

環境

keras: version 2.3.1

レイヤーは3層で作成しています。

model = Sequential()
model.add(InputLayer(input_shape=(610, )))
model.add(Dense(300, activation='relu'))
model.add(Dense(265, activation='relu'))

保存したモデルファイルの中身を確かめてみると実際には3層なのにも関わらず2層と認識しているようです。

model.summary()

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense_1 (Dense)              (None, 300)               183300
_________________________________________________________________
dense_2 (Dense)              (None, 265)               79765
=================================================================

原因

入力層を明示的に書いていたことが原因のようです。

model.add(InputLayer(input_shape=(610, )))

下記のように修正するとうまくモデルが保存され読み込むことができました。

model.add(Dense(300, activation='relu', input_dim=610))
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?