简体   繁体   English

keras > 加载保存的模型后总是相同的预测值

[英]keras > always the same prediction value after loading saved model

I am training some model via keras with tensorflow backend.我正在使用tensorflow后端通过keras训练一些模型。

When I call predict right after training on the same object it works fine and gives different values for different inputs.当我在对同一对象进行训练后立即调用 predict 时,它工作正常并为不同的输入提供不同的值。 But when I save the model to a file, then load it from another python session, predict always returns the same value for different inputs.但是当我将模型保存到文件中,然后从另一个python会话加载它时, predict总是为不同的输入返回相同的值。

I use ModelCheckpoint for saving the model, then load_model for loading.我使用ModelCheckpoint来保存模型,然后使用load_model进行加载。 I have also tried to save and load architecture separately to a json file with to_json and model_from_json functions.我还尝试使用to_jsonmodel_from_json函数将架构分别保存和加载到 json 文件中。 Sample code:示例代码:

Saving part保存部分

with open("model.json", "w") as textFile:
   print(model.to_json(), file = textFile)

model.fit(X_train, y_train, epochs=iterationCount, batch_size=64, validation_split=0.2, callbacks = [ModelCheckpoint(filepath='model.h5', verbose=0, save_best_only=True)])

Loading part装载部分

with open('model.json') as json_file:
    model = model_from_json(json_file.read())

model.load_weights('model.h5')

Any ideas to solve this?有什么想法可以解决这个问题吗? Is there anything that I am missing?有什么我想念的吗?

Unfortunately many people (like me) have been complaining on a keras bug that affects the save_weights and load_weights functions.不幸的是,很多人(像我一样)一直在抱怨影响save_weightsload_weights函数的keras 错误

I ended up avoiding to use these function.我最终避免使用这些功能。 If I want to load/store my model I just do the following:如果我想加载/存储我的模型,我只需执行以下操作:

from keras.models import load_model

trained_model.save(file_path)
loaded_model = load_model(file_path)

The save functions saves your weights, but also your network structure and the state of the optimizer (it makes sure that you can keep training model from exactly where you paused). save函数可以保存您的权重,还可以保存您的网络结构和优化器的状态(它确保您可以将训练模型保持在您暂停的位置)。

keras model save/load has two ways (two ways I feel comfortable to use). keras 模型保存/加载有两种方式(我觉得使用起来很舒服的两种方式)。 Saving both model structure and weights model.save(file_path) and only save model weights model.save_weights(file_path) .保存模型结构和权重model.save(file_path)并且只保存模型权重model.save_weights(file_path) So I save/load model like following:所以我保存/加载模型如下:

from keras.models import load_model
trained_model.save(file_path)
loaded_model = load_model(file_path)

trained_model.save_weights(file_path)
# We have to define model first before loading weights
trained_model.load_weights(file_path)

Here, load_weights(file_path, by_name=True) allows you to load weights into a different structure with some layer names in common.在这里, load_weights(file_path, by_name=True)允许您将权重加载到具有某些层名称相同的不同结构中。

I had the same issue, I solved by setting a fixed seed to tensorflow, numpy and python.我遇到了同样的问题,我通过将固定种子设置为 tensorflow、numpy 和 python 来解决。

import tensorflow as tf
import numpy as np
import random as python_random
tf.random.set_random_seed(42)
np.random.seed(42)
python_random.seed(42)

Be careful!小心! Different versions of tensorflow may required a different way to set a seed!不同版本的 tensorflow 可能需要不同的方式来设置种子!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM