Skip to content

Serialization with activation layer does not work #21088

Closed
@MathiesW

Description

@MathiesW

Most Keras layer support an activation function. While it is possible to use string identifiers like "relu", we can also use actual activation layers like layers.ReLU().

When using a layer, the deserialization is broken, since the activations.deserialize(activation) in the from_config() method does not support an instance of class layers.Layer. This significantly reduces the flexibility, because using, e.g., LeakyReLU with a negative slope of 0.1 is not possible when you rely on loading the trained model later on.

An easy fix would be to use saving.serialize_keras_object(self.activation) in get_config() and saving.deserialize_keras_object(activation_cfg) in the from_config() method.

from keras import layers

layer = layers.Conv1D(filters=1, kernel_size=1, activation=layers.ReLU())  # works flawless
layer_from_config = layers.Conv1D.from_config(layer.get_config())

The last line throws the following exception (tested on Keras 3.6.0 and 3.9.0)
"Exception encountered: Could not interpret activation function identifier: {'module': 'keras.layers', 'class_name': 'ReLU', 'config': {'name': 're_lu', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}, 'max_value': None, 'negative_slope': 0.0, 'threshold': 0.0}, 'registered_name': None}"

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions