Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving a model I get: module 'tensorflow.python.saved_model.registration' has no attribute 'get_registered_name'

When I try to save my ternsorflow model I get this error message. What is the problem here and how do I fix it?

    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)

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

This is the stack trace:

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'
like image 626
arame3333 Avatar asked Sep 05 '25 21:09

arame3333


2 Answers

Check if your tensorflow version is older or up-to-date.

This seems to be a newer module https://www.tensorflow.org/api_docs/python/tf/keras/saving/get_registered_name

Make sure you have this version of tensorflow installed in your environment

pip install tensorflow==2.12.0

I don't know about the dataset so I assumed a small one. This is the code I ran

import tensorflow as tf
import numpy as np
import os

# Define placeholder values
hidden_dim = 2
n_classes = 2
lr = 0.001
epochs = 10
batch_size = 32

# Create a simple dataset
x_train = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
y_train = np.array([0, 1, 0, 1])

# Convert y_train to one-hot encoded format
y_train = tf.keras.utils.to_categorical(y_train, num_classes=n_classes)

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 = 0  # Initialize k
k += 1  # Increment k
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,
    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)

And it ran fine

like image 140
Gaurav Hazra Avatar answered Sep 10 '25 01:09

Gaurav Hazra


You can use tf.saved_model.save on the model object which will work with Keras. Or, you can use the keras saving and loading API, with tf.keras.saving.save_model() or its alias model.save() (not what you're doing now see below). Either formats can be called to save a model to disk. The former, tf.saved_model.save, is the newer means of saving TensorFlow SavedModel format (recommended), and the latter:

tf.keras.saving.save_model(model, filepath, overwrite=True, save_format=None, **kwargs)

supports both saving a model as a TensorFlow SavedModel or (backwards compatible) HDF5 file. In your code, you are calling the save method from the previous API locations keras.models, and not the new namespace, keras.saving, where all saving utilities have been moved. The other locations will exist indefinitely according to the release notes but it's recommended to use one of the newer options. Since you can call tf.keras.Model.save to save the entire model, which in your case looks like this:

model = tf.keras.models.Sequential(...)
model.compile(...)
model.save('model')

...but, model.save() also serves as an alias for tf.keras.saving.save_model(). I think it might be safer to use tf.keras.saving.save_model(model, model_path) instead of trying the alias (if you went the keras api route). Or, you can avoid any name confusion by trying tf.saved_model.save(model, model_path) first.

Also, when you call model.save(path), 'save('path/to/model')' creates a SavedModel directory at 'path/to/model'. So, your conditional: if os.path.isfile(path) is False will always be true because the path, had it been previously created, would not be a file, but a directory. If you did in fact want a file instead of tf SavedModel folder, you could use:

tf.keras.saving.save_model(model, model_path, overwrite=True, save_format='h5')

otherwise, I'd probably recommend trying this first:

tf.saved_model.save(model, model_path)

Regarding the paths at the end: you use the parameter exist_ok=True. By default, os.makedirs() raises an OSError if the directory already exists but setting this to True suppresses the FileExistsError and overwrites the existing directory. After this, you check whether the file exists (even though it'd be a directory) but in either case it would always evaluate to true. If you checked for os.path.isfile(path), that'd be true that it is not a file (and you didn't specify that option by passing a .h5/.keras file). If you checked for os.path.isdir(path), this would also evaluate to true because you just wrote a new empty "model_mlp_lm" over the existing one so checking for a model dir within that dir doesn't make sense (it's empty).

create the output directory outside of whatever loop you're in

then, try this:

output_dir = "model_mlp_lm"
model_dir = f"m{k}_model"
assert os.path.exists(output_dir)
model_path = os.path.join(output_dir, model_dir)
if not os.path.isdir(model_path):
    tf.saved_model.save(model, model_path)

I hope this helps, these are my thoughts without a minimum reproducible snippet or the full stack trace

like image 27
harriet Avatar answered Sep 10 '25 02:09

harriet