首页 > 解决方案 > 加载 tf.keras 模型,ValueError: The two structure don't have the same nested structure

问题描述

我创建了一个tf.keras model具有BERT的设备,我想对其进行训练并保存以供进一步使用。加载此模型是一个大问题,因为我不断收到错误消息: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:
((., ., ., ., ., ., ., ., ., .), {})

标签: pythontensorflowkerashuggingface-transformersbert-language-model

解决方案


在 GitHub 上查看问题。它应该可以帮助您解决形状问题。


推荐阅读