Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bidirectional layer rejects custom activations #19737

Open
jm-willy opened this issue May 20, 2024 · 4 comments
Open

Bidirectional layer rejects custom activations #19737

jm-willy opened this issue May 20, 2024 · 4 comments
Assignees
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@jm-willy
Copy link

Every other layer I tried accepts custom activations

Minimal code

import tensorflow as tf


@tf.function
def my_hard_sigmoid(x):
    return x


@tf.function
def my_hard_tanh(x):
    return x


inputs = tf.keras.Input(
    shape=(
        100,
        32,
    )
)
init_layer = tf.keras.layers.LSTM(
    8,
    activation=my_hard_tanh,
    recurrent_activation=my_hard_sigmoid,
)
x = tf.keras.layers.Bidirectional(init_layer)(inputs)
model = tf.keras.Model(inputs=inputs, outputs=x)
model.summary(expand_nested=True, show_trainable=True)

Output
ValueError: Unknown activation function '{'module': 'tensoions.py", line 646, in deserializerflow.python.eager.polymorphic_function.polymorphic_function', 'class_name': 'Function', 'config': 'my_hard_tanh', 'rflow.python.eager.polymorphic_function.polymorphic_function', 'class_name': 'Functregistered_name': 'Function'}' cannot be deserialized.

Versions
python 3.11.9
tensorflow 2.15.1
keras 2.15.0

@fchollet
Copy link
Member

Your custom objects always need to be serializable. You can use @keras.saving.register_keras_serializable for this purpose https://keras.io/api/models/model_saving_apis/serialization_utils/#registerkerasserializable-function

@chunduriv chunduriv added the type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. label May 21, 2024
@jm-willy
Copy link
Author

@tf.keras.utils.register_keras_serializable("custom_activations")
@tf.function
def my_hard_sigmoid(x):
    return x


@tf.keras.utils.register_keras_serializable("custom_activations")
@tf.function
def my_hard_tanh(x):
    return x

Thanks. Doesn't works when @tf.function is added, asks for a class with a from_config() method. What a pain!
However, I using custom layers with no problems, I opened issue for anyone else with the problem.

@fchollet
Copy link
Member

Why do you use tf.function though?

@jm-willy
Copy link
Author

Isn't it a good speedup?

@chunduriv chunduriv added the stat:awaiting keras-eng Awaiting response from Keras engineer label May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

4 participants