ageron / ageron/handson-ml2

[BUG] Chapter 12: Loading model is failed when using the custom regularisers, activation, etc

Open
#634 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
30k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
In chapter 12, when working with the custom models, the Keras fails loading the model after saving. It is saved safely (not pure safely as it throws warning indicating the save format should be converted to Keras rather than h5) and after loading it throws an exceptions.

notebook name: 12_custom_models_and_training_with_tensorflow.ipynb
cell numbers: 91

**To Reproduce**
Please copy the code that fails here, using code blocks like this:

model = keras.models.load_model(
"my_model_with_many_custom_parts.h5",
custom_objects={
"my_l1_regularizer": my_l1_regularizer,
"my_positive_weights": my_positive_weights,
"my_glorot_initializer": my_glorot_initializer,
"my_softplus": my_softplus,
})

And if you got an exception, please copy the full stacktrace here:

```TypeError Traceback (most recent call last)
[](https://m6gi76vt7u-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab_20240405-060114_RC00_622131336#) in ()
----> 1 model = keras.models.load_model(
2 "my_model_with_many_custom_parts.h5",
3 custom_objects={
4 "my_l1_regularizer": my_l1_regularizer,
5 "my_positive_weights": my_positive_weights,

2 frames
[/usr/local/lib/python3.10/dist-packages/keras/src/engine/base_layer.py](https://m6gi76vt7u-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab_20240405-060114_RC00_622131336#) in from_config(cls, config)
868 return cls(**config)
869 except Exception as e:
--> 870 raise TypeError(
871 f"Error when deserializing class '{cls.__name__}' using "
872 f"config={config}.\n\nException encountered: {e}"

TypeError: Error when deserializing class 'Dense' using config={'name': 'dense_1', 'trainable': True, 'dtype': 'float32', 'units': 1, 'activation': {'module': 'builtins', 'class_name': 'function', 'config': 'my_softplus', 'registered_name': 'function'}, 'use_bias': True, 'kernel_initializer': {'module': 'builtins', 'class_name': 'function', 'config': 'my_glorot_initializer', 'registered_name': 'function'}, 'bias_initializer': {'module': 'keras.initializers', 'class_name': 'Zeros', 'config': {}, 'registered_name': None}, 'kernel_regularizer': {'module': 'builtins', 'class_name': 'function', 'config': 'my_l1_regularizer', 'registered_name': 'function'}, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': {'module': 'builtins', 'class_name': 'function', 'config': 'my_positive_weights', 'registered_name': 'function'}, 'bias_constraint': None}.

Exception encountered: Unknown activation function: 'function'. Please ensure you are using a `keras.utils.custom_object_scope` and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
```

**Expected behavior**
A clear and concise description of what you expected to happen.

I expect to load model without any error.

**Versions (please complete the following information):**
I have run it on google colab
- OS: Nil
- Python: 3.10.12
- TensorFlow: 2.15.0
- Scikit-Learn: 1.2.2

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.