python - 加载 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')
开始回复之前的一些注意事项:
我确实指定了
dtype
.不,我不想只保存重量。
我尝试
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 上查看问题。它应该可以帮助您解决形状问题。
推荐阅读
- javascript - 如何在 React 中动态加载 JSON 文件?
- css - 固定位置的“clearfix”
- asp.net-core-webapi - Asp.net core 3.1 webapi iis 8无法连接到sql server
- javascript - 元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件)但得到:对象。反应原生
- flutter - 如何在 Listtile 的前导和尾随之间对齐小部件?- 颤振
- flutter - Flutter如何让Container展开全高
- javascript - Jasmine Rails,未从子目录加载 src_files
- angular - 如何在不同的组件角度使用标题搜索字段中的搜索过滤器变量
- excel - 通过 Power Query 按列合并 Excel 文件
- r - 我需要帮助在同一列上的两个单元格之间进行增量