You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

保存TensorFlow模型遇get_registered_name属性缺失错误的解决

问题:TensorFlow模型保存时出现AttributeError: module 'tensorflow.python.saved_model.registration' has no attribute 'get_registered_name'

尝试保存自行构建的TensorFlow模型时触发以下错误:

AttributeError: module 'tensorflow.python.saved_model.registration' has no attribute 'get_registered_name'

模型构建、训练及保存代码

model = tf.keras.models.Sequential()

# define the neural network architecture
model.add(
    tf.keras.layers.Dense(50, input_dim=hidden_dim, activation="relu")
)
model.add(tf.keras.layers.Dense(n_classes))

k += 1
model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=lr),
    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
    metrics=["mse", "accuracy"],
)

history = model.fit(
    x_train,
    y_train,
    epochs=epochs,
    batch_size=batch_size,
    validation_data=(x_test, y_test),
    verbose=0,
)

folder = "model_mlp_lm"
file = f"m{k}_model"
os.makedirs(folder, exist_ok=True)
path = f"{folder}/{file}"
if os.path.isfile(path) is False:
    model.save(path)

错误堆栈信息

Traceback (most recent call last):
  File "D:\Anaconda\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "D:\Anaconda\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\hijik\.vscode\extensions\ms-python.python-2023.10.0\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\hijik\.vscode\extensions\ms-python.python-2023.10.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\hijik\.vscode\extensions\ms-python.python-2023.10.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\hijik\.vscode\extensions\ms-python.python-2023.10.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\hijik\.vscode\extensions\ms-python.python-2023.10.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\hijik\.vscode\extensions\ms-python.python-2023.10.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "D:\_lodestar\personality-prediction\finetune_models\MLP_LM.py", line 273, in <module>
  File "D:\Anaconda\lib\site-packages\tensorflow\python\saved_model\save.py", line 1450, in _build_meta_graph_impl
    object_graph_proto = _serialize_object_graph(
  File "D:\Anaconda\lib\site-packages\tensorflow\python\saved_model\save.py", line 1022, in _serialize_object_graph
    _write_object_proto(obj, obj_proto, asset_file_def_index,
  File "D:\Anaconda\lib\site-packages\tensorflow\python\saved_model\save.py", line 1061, in _write_object_proto
    registered_name = registration.get_registered_name(obj)
AttributeError: module 'tensorflow.python.saved_model.registration' has no attribute 'get_registered_name'

原因分析

这个错误的核心是TensorFlow版本不兼容:get_registered_name是TensorFlow 2.8及以上版本新增的API,你的环境中安装的TensorFlow版本过低,导致保存模型时调用了不存在的方法。另外也可能是环境中存在多个TensorFlow版本残留,导致代码实际调用了旧版本的模块。

解决方法

  • 升级TensorFlow到兼容版本:
    在终端执行升级命令:

    pip install --upgrade tensorflow
    

    建议升级到2.8.x及以上稳定版本,确保API完整。

  • 清理环境版本冲突:
    如果是conda环境,先检查当前环境的TensorFlow版本:

    conda list tensorflow
    

    若存在多个版本或旧版本残留,先卸载:

    pip uninstall tensorflow
    

    再重新安装指定版本:

    pip install tensorflow==2.10.0  # 可替换为其他兼容稳定版本
    
  • 临时替代保存方案:
    若暂时无法升级版本,可改用HDF5格式保存模型,避开SavedModel格式的API依赖:

    # 替换原model.save(path)为以下代码
    model.save(f"{path}.h5")
    # 后续加载模型使用:
    # loaded_model = tf.keras.models.load_model(f"{path}.h5")
    

内容的提问来源于stack exchange,提问作者arame3333

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 17:02:18