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'
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