keras将模型权重保存到一个文件中 [英] keras save the model weights to one file

查看:433
本文介绍了keras将模型权重保存到一个文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个keras模型,该模型可以保存每个时期的权重,如何制作一个文件

i have a keras model which save weights of each epoch how can i make to one file

这是保存模式的行

我有50个历元,我将得到50个权重,我只希望将1个权重保存在一个文件中

and i have 50 epoch i will get 50 weights which i want only 1 save all of them inside one file

>     model.save_weights('checkpoint_epoch_{}.hdf5'.format(k))

不知道我该怎么做才能将其保存在一个文件中,因为我以后必须将权重转换为张量流模型

any idea what shall i do to save it in one file because i have to convert weights later to tensorflow model

所需重量

checkpoint.h5

推荐答案

您不需要权重在一个文件中,可以保存整个模型并使用

You don't need the weights in one file, you can save the whole model and use the TFLiteConverter to convert your tf.keras model or tf model to lite directly from a .h5 file.

 import tensorflow as tf
 from tf.keras.models import load_model

 model=load_model("model.h5")

 # Convert the model.
 converter = tf.lite.TFLiteConverter.from_keras_model(model)
 tflite_model = converter.convert()

如果您已构建Keras模型,则可以在训练时使用回调在每个时期保存模型>称为ModelCheckpoint.

If you have a Keras model built, you can save the model at each epoch while training using a callback called ModelCheckpoint.

回调是在训练过程的给定阶段要应用的一组功能.您可以在训练期间使用回调来查看模型的内部状态和统计信息.您可以将回调列表(作为关键字参数回调)传递给Sequential或Model类的.fit()方法.然后,在训练的每个阶段都将调用回调的相关方法.

A callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training. You can pass a list of callbacks (as the keyword argument callbacks) to the .fit() method of the Sequential or Model classes. The relevant methods of the callbacks will then be called at each stage of the training.

from keras.callbacks import ModelCheckpoint


'''
saves the model weights after each epoch
'''
checkpointer = ModelCheckpoint(filepath='model-{epoch:02d}-{val_loss:.2f}.h5', verbose=1)
model.fit(x_train, y_train, batch_size=batchsize, epochs=epochs, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])

然后将模型与纪元号和验证丢失信息一起保存在文件名中

Then the model will be saved with the epoch number and the validation loss in the filename

因此,您可以保存模型,然后如上所述进行加载.

SO you can save a model and then later load it as described above.

这篇关于keras将模型权重保存到一个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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