加载预训练模型后与变量输入维度相关的错误

Error related to variable Input dimensions after loading pretrained 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])

有了这样的 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
_______________________________________

我用这段代码保存了预训练模型:

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

并加载了这个:

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

但是当我从文件加载它时,这个错误发生在 dimensoins 上。我检查了加载的模型摘要并看到了这个:

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

我该如何解决这个问题?我正在使用 tensorflow 1.15 和 keras 2.3.1

我找到了解释 :

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

按如下所示更改您的模型,它应该可以工作:

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()

请注意,输入形状是 (768,) 而不是 (None, 768)。