noam gaash
noam gaash

Reputation: 361

keras.load_model() can't recognize Tensorflow's activation functions

I saved a tf.keras model using tf.keras.save_model functions. why tf.keras.load_model throws an exception?

code example:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(8, activation=tf.nn.leaky_relu),
    layers.Dense(8, activation=tf.nn.leaky_relu)
])

tf.keras.models.save_model(
    model,
    'model'
)

tf.keras.models.load_model('model')

I expect this code to load the model, but it throws an exception:

ValueError: Unknown activation function:leaky_relu

Upvotes: 6

Views: 6318

Answers (1)

Sharky
Sharky

Reputation: 4533

You need to add custom objects

tf.keras.models.load_model('model', custom_objects={'leaky_relu': tf.nn.leaky_relu})

Upvotes: 16

Related Questions