question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when cloning amodel with custom activation function

See original GitHub issue

Hello,

While I was trying to clone a model I faced the following error:

Traceback (most recent call last):

File “<ipython-input-69-598bf1638a5a>”, line 10, in <module> model_RMSE = clone_model(model)

File “/usr/local/lib/python3.5/dist-packages/keras/models.py”, line 1523, in clone_model if layer not in layer_map:

File “/usr/local/lib/python3.5/dist-packages/keras/models.py”, line 1378, in _clone_functional_model merge_config = self.layers[0].get_config()

File “/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py”, line 1252, in from_config # Returns

File “/usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py”, line 87, in wrapper return func(*args, **kwargs)

File “/usr/local/lib/python3.5/dist-packages/keras/layers/core.py”, line 808, in init super(Dense, self).init(**kwargs)

File “/usr/local/lib/python3.5/dist-packages/keras/activations.py”, line 95, in get if identifier is None:

File “/usr/local/lib/python3.5/dist-packages/keras/activations.py”, line 87, in deserialize def deserialize(name, custom_objects=None):

File “/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py”, line 159, in deserialize_keras_object if fn is None:

ValueError: Unknown activation function:exp Here is a minimal reproducible example:

from keras.models import Model,clone_model
from keras.layers import Input, Dense
from keras.optimizers import Adam
from keras import backend as K

inputs = Input(name='input1', shape=(1,))
model = Dense(15, activation='relu') (inputs)
outputs = Dense(1, activation=K.exp) (model)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='poisson',optimizer=Adam(lr=5e-3))
model_clone = clone_model(model)

When replacing outputs = Dense(1, activation=K.exp) (model) to outputs = Dense(1, activation=None) (model) error is vanishing. Any idea how to solve this problem?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
FedericoMuciacciacommented, Mar 5, 2019

I still have this bug

x = keras.layers.Activation(my_custom_activation_function)(x) is not working. I have to use x = keras.layers.Lambda(my_custom_activation_function)(x) instead

why this strange behaviour?

2reactions
blakedeweycommented, May 5, 2018

This is a little more pressing now, as clone_model is used in multi_gpu_model when cpu_relocation is True.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras – Artificial Neural Networks - Error when using a custom ...
This issue happens because tf.cond expects a scalar for the condition argument (instead of a multi-dimensional tensor).
Read more >
Layer activation functions - Keras
Activations can either be used through an Activation layer, or through the activation argument supported by all forward layers: model.add(layers.
Read more >
How to make a custom activation function in Keras?
Simple example on how to make a custom activation function in Keras? ... Sequential() # Define the first layer model.add(keras.layers.
Read more >
Tutorial 3: Activation Functions - UvA DL Notebooks
In this tutorial, we will take a closer look at (popular) activation functions and investigate their effect on optimization properties in neural networks....
Read more >
Save and load Keras models | TensorFlow Core
How SavedModel handles custom objects. When saving the model and its layers, the SavedModel format stores the class name, call function, losses, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found