如何使用 keras 加载 tf.keras 模型 [英] How to load tf.keras models with keras
问题描述
我一直在使用 tensorflow 1.12.0 中的 keras 模块来训练和保存模型.我最近遇到了一个看似有用的用于权重/输出可视化的库,但它们需要将模型作为 Keras 模型加载.我在尝试使用 keras 加载我的 tf.keras
模型时遇到错误,希望有人能提供解决方案.Python 3.5.2 版,Keras 2.2.4 版.
I've been using the keras module from tensorflow 1.12.0 for training and saving models. I recently came across a seemingly useful library for visualization of the weights/outputs, but they require the models be loaded as a Keras model. I'm running into an error trying to load my tf.keras
models using keras, and was hoping someone could provide a solution. Python version 3.5.2, Keras version 2.2.4.
我已经为 GlorotUniform
定义了自定义对象,因为 keras 无法识别该初始值设定项.之后,当我尝试加载模型时,出现 TypeError
.
I've defined the custom object for the GlorotUniform
since keras doesn't recognize that initializer. Afterwards, when I try to load the model, I get a TypeError
.
# This works
model = tf.keras.models.load_model('./densenet_model.h5')
# This does not work
model = keras.models.load_model('./densenet_model.h5', custom_objects={"GlorotUniform": tf.keras.initializers.glorot_uniform})
# This is the error that happens
TypeError: tuple indices must be integers or slices, not list
总而言之,我想知道是否有一种简单的方法可以将使用 tf.keras
创建的模型转换为 keras 模型.
In summary, I was wondering if there was a simple way to convert a model created with tf.keras
to a keras model.
推荐答案
我使用了 from tensorflow.python.keras.models import load_model
而不是 from keras.models import load_model
>.问题解决了.
Instead of from keras.models import load_model
I used from tensorflow.python.keras.models import load_model
. The problem is solved.
这篇关于如何使用 keras 加载 tf.keras 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!