keras.load_model()无法识别Tensorflow的激活功能 [英] keras.load_model() can't recognize Tensorflow's activation functions

查看:200
本文介绍了keras.load_model()无法识别Tensorflow的激活功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 tf.keras.save_model 函数保存了tf.keras模型.为什么 tf.keras.load_model 引发异常?

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

代码示例:

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

推荐答案

您需要添加自定义对象

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

这篇关于keras.load_model()无法识别Tensorflow的激活功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆