【问题标题】:Error related to variable Input dimensions after loading pretrained model加载预训练模型后与可变输入尺寸相关的错误
【发布时间】:2021-12-18 01:29:32
【问题描述】:

我的模型有以下代码:

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

【问题讨论】:

    标签: python tensorflow keras tf.keras


    【解决方案1】:

    我找到了解释in this post

    删除 InputLayer 并在第一层使用 input_shape ... 似乎模型 使用 InputLayer 未正确序列化为 HDF5。

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

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

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 2016-08-16
      • 2017-10-25
      • 1970-01-01
      • 2019-10-20
      • 2018-09-25
      • 2021-04-05
      • 1970-01-01
      相关资源
      最近更新 更多