Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 749
noam gaash Avatar asked Mar 26 '19 19:03

noam gaash


1 Answers

You need to add custom objects

tf.keras.models.load_model('model', custom_objects={'leaky_relu': tf.nn.leaky_relu})
like image 175
Sharky Avatar answered Nov 20 '22 15:11

Sharky