6

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
desertnaut
  • 57,590
  • 26
  • 140
  • 166
noam gaash
  • 337
  • 2
  • 13
  • @Sharky this is the full code. see colab example: https://colab.research.google.com/drive/1sD2nD9dqnh3rqMfgHFZ7dPI6m6dqGtU_ – noam gaash Mar 26 '19 at 19:41

1 Answers1

16

You need to add custom objects

tf.keras.models.load_model('model', custom_objects={'leaky_relu': tf.nn.leaky_relu})
Sharky
  • 4,473
  • 2
  • 19
  • 27