3

I'm using a lambda function to create a custom activation function but when I try to upload a checkpoint I get the error:

ValueError: Unknown activation function:<lambda> 

The function is:

lrelu = lambda x: tf.keras.activations.relu(x, alpha=0.2)

and used like this:

Conv2D(filters=96, kernel_size=(3,3),strides=(2,2),activation=lrelu)

I tried to add the custom_objects with no luck:

model = load_model(filepath, custom_objects = {"lrelu": lrelu})

I know I can replace my function with an extra layer and avoid this problem, but I was wondering if there is a way to make this work.

Thanks.

2 Answers 2

2

You'll need the Lambda layer wrapper - minimal example below.

from keras.layers import Input, Conv2D, Lambda
from keras.models import Model
import tensorflow as tf
import numpy as np

lrelu = Lambda(lambda x: tf.keras.activations.relu(x, alpha=0.2))

ipt   = Input((4,4,3))
out   = Conv2D(3, 1, activation=lrelu)(ipt)
model = Model(ipt, out)
model.compile('adam', 'mse')

x = np.random.randn(32,4,4,3)
model.fit(x, x)
32/32 [==============================] - 6s 201ms/sample - loss: 2.1475
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, this is using tensorflow.keras which is not the same as just keras. And because I'm using a custom implementation I cannot switch
@joaquin7 It still works - and in the future, best to specify your import sources (e.g. from keras)
0

Unfortunately it seems that this is an issue with Keras.

Work-around is to add a separate Activation Layer:

model.add(Dense(units=128))
model.add(Lambda(lambda x: custom_activation(x, salience_array[0])))

Note: To save and load a model, use Lambda() instead of Activation().

Reference: https://github.com/keras-team/keras/issues/8880

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.