Error related to variable Input dimensions after loading pretrained model

Viewed 20

I have the following code of my model:

model = keras.Sequential()
model.add(L.InputLayer(batch_input_shape= (None, 768)))
model.add(L.Dense(input_shape = (None,768), activation='relu', units = 256))
model.add(L.Dense(input_shape = (None,256), activation='relu', units = 128))
model.add(L.Dense(input_shape=(None,128), activation='relu', units = 301))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', precision, recall, f1])

With such resalt of model.summury():

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 256)               196864    
_________________________________________________________________
dense_2 (Dense)              (None, 128)               32896     
_________________________________________________________________
dense_3 (Dense)              (None, 301)               38829     
=================================================================
Total params: 268,589
Trainable params: 268,589
Non-trainable params: 0
_______________________________________

I've saved pretrained model with this code:

model.save('./tag_prediction_model.h5')

And loaded with this:

dependincies = {
    'precision': precision,
    'recall': recall,
    'f1': f1
}
model1 = load_model('./tag_prediction_model.h5', custom_objects=dependincies)

But when I loaded it from the file, I this error occured with dimensoins. I checked loaded model summary and saw this:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, None, 256)         196864    
_________________________________________________________________
dense_2 (Dense)              (None, None, 128)         32896     
_________________________________________________________________
dense_3 (Dense)              (None, None, 301)         38829     
=================================================================
Total params: 268,589
Trainable params: 268,589
Non-trainable params: 0
_________________________________________________________________

How can I fix this? I'm using tensorflow 1.15 and keras 2.3.1

1 Answers

I found the explanation in this post:

Drop InputLayer and use input_shape in first layer ... It seems models with InputLayer are not serialized to HDF5 correctly.

By changing your model as shown below it should work :

model = keras.Sequential()
model.add(L.Dense(input_shape= (768,), activation='relu', units = 256))
model.add(L.Dense(activation='relu', units = 128))
model.add(L.Dense(activation='relu', units = 301))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

Remark that the input shape is (768,) and not (None, 768).

Related