Loading tf.keras model, ValueError: The two structures don't have the same nested structure

Loading tf.keras model, ValueError: The two structures don't have the same nested structure

我创建了一个具有 BERTtf.keras model,我想训练并保存它以供进一步使用。 加载此模型是一个大问题,因为我不断收到错误消息:ValueError: The two structures don't have the same nested structure.

我把模型简化了很多,看看到底是哪里出了问题。代码非常简单:

bert = TFBertModel.from_pretrained("bert-base-german-cased")

model_name = "Model"
txt12_input_ids = tf.keras.layers.Input(shape=(max_length,),  name='txt12_input_ids', dtype='int32')
txt12_mask      = tf.keras.layers.Input(shape=(max_length,),  name='txt12_mask', dtype='int32')
txt12_outputs = bert(txt12_input_ids, txt12_mask).pooler_output

model_K = tf.keras.Model(inputs=(txt12_input_ids,  txt12_mask), outputs=txt12_outputs, name=model_name)
model_K.compile(optimizer=Adam(1e-5), loss="binary_crossentropy", metrics="accuracy")


model_K.save(dir_path+'Prob')
model_2 = tf.keras.models.load_model(dir_path+'Prob')

开始回复前的一些注意事项:

  1. 我确实指定了dtype

  2. 不,我不想只保存权重。

  3. 我试着用 tf.keras.models.save_model(model_K, dir_path+'Prob') 代替,但它给出了同样的错误。

最后一件事,我和 tf version: 2.6.0 一起工作。有谁知道怎么解决吗?

完整的错误信息:

ValueError: The two structures don't have the same nested structure.

First structure: type=tuple str=(({'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='input_ids/input_ids')}, None, None, None, None, None, None, None, None, False), {})

Second structure: type=tuple str=((TensorSpec(shape=(None, 120), dtype=tf.int32, name='input_ids'), TensorSpec(shape=(None, 120), dtype=tf.int32, name='attention_mask'), None, None, None, None, None, None, None, False), {})

More specifically: Substructure "type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='input_ids/input_ids')}" is a sequence, while substructure "type=TensorSpec str=TensorSpec(shape=(None, 120), dtype=tf.int32, name='input_ids')" is not
Entire first structure:
(({'input_ids': .}, ., ., ., ., ., ., ., ., .), {})
Entire second structure:
((., ., ., ., ., ., ., ., ., .), {})

查看 GitHub 上的 issue here。应该能帮你解决形状问题