如何在Keras中保存训练期间保存的模型列表(仅保存最佳)?

正阳音乐

我正在使用Modelcheckpoint功能根据“仅保存最佳”条件保存模型。

file_name = str(datetime.datetime.now()).split(' ')[0] + f'{model_name}'+ '_{epoch:02d}.hdf5'

checkpoint_main = ModelCheckpoint(filename, monitor='val_acc', verbose=2,
                                save_best_only=True, save_weights_only=False,
                                mode='auto', period=1)

由于我使用的是“仅保存最佳”,因此只会保存某些时期。我想收集实际保存的模型的路径,并将它们保存到列表中,以便在培训结束时访问。该列表将通过管道传输到其他操作。

我尝试查看源代码,但是没有看到返回列表的“ train_end”示例,因此我不确定在培训结束时如何返回某些内容。

https://github.com/keras-team/keras/blob/master/keras/callbacks.py#L360

米哈伊尔·斯蒂芬诺夫(Mikhail Stepanov)

如果您为每个时期存储到保存的模型的所有路径,则可以使用Callback,因为Callback它只是一个python对象,可以收集数据。

例如,它可以在列表中存储模型的路径:

import datetime

class SaveEveryEpoch(Callback):
    def __init__(self, model_name, *args, **kwargs):
        self.model_checkpoint_paths = []
        self.model_name = model_name
        super().__init__(*args, **kwargs)

    def on_epoch_end(self, epoch, logs):
        # I suppose here it's a Functional model
        print(logs['acc'])
        path_to_checkpoint = (
            str(datetime.datetime.now()).split(' ')[0] 
            + f'_{self.model_name}'
            + f'_{epoch:02d}.hdf5'
        )
        self.model.save(path_to_checkpoint)
        self.model_checkpoint_paths.append(path_to_checkpoint)
  • __init__ 初始化一个空列表并存储模型基本名称
  • on_epoch_end在每个时期结束时保存模型;还将模型路径追加到模型路径列表中

使用例

from keras.datasets import mnist
from keras.models import Model
from keras.layers import Dense, Input
import numpy as np

(X_tr, y_tr), (X_te, y_te) = mnist.load_data()
X_tr = (X_tr / 255.).reshape((60000, 784))
X_te = (X_te / 255.).reshape((10000, 784))


def binarize_labels(y):
    y_bin = np.zeros((len(y), len(np.unique(y)))) 
    y_bin[range(len(y)), y] = 1
    return y_bin

y_train_bin, y_test_bin = binarize_labels(y_tr), binarize_labels(y_te)

model = Sequential()
model.add(InputLayer((784,)))
model.add(Dense(784, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))

model = Model(inp, out)
model.compile(loss='categorical_crossentropy', optimizer='adam')

使用检查点运行模型

checkpoints = SaveEveryEpoch('mnist_model')
history = model.fit(X_tr, y_train_bin, callbacks=[checkpoints], epochs=3)
# ... training progress ...
checkpoints.model_checkpoint_paths
Out:
['2019-02-06_mnist_model_00.hdf5',
 '2019-02-06_mnist_model_01.hdf5',
 '2019-02-06_mnist_model_02.hdf5']

ls 输出:

2019-02-06_mnist_model_00.hdf5
2019-02-06_mnist_model_01.hdf5
2019-02-06_mnist_model_02.hdf5

变化

修改on_epoch_end以创建一些集合,该集合可以通过进行排序(loss例如,logs参数包含一个字典,该字典包含,loss并且提供了一个度量,acc如果提供了某个度量,则该度量名为)。因此,您以后可以选择损耗/度量值最小的模型:

class SaveEveryEpoch(Callback):
    def __init__(self, model_name, *args, **kwargs):
        self.model_checkpoints_with_loss = []
        self.model_name = model_name
        super().__init__(*args, **kwargs)

    def on_epoch_end(self, epoch, logs):
        # I suppose here it's a Functional model
        print(logs['acc'])
        path_to_checkpoint = (
            str(datetime.datetime.now()).split(' ')[0] 
            + f'_{self.model_name}'
            + f'_{epoch:02d}.hdf5'
        )
        self.model.save(path_to_checkpoint)
        self.model_checkpoints_with_loss.append((logs['loss'], path_to_checkpoint))

另外,您可以重载默认回调,例如ModelCheckpoint,以保存所有路径,不仅是最佳模型,而且我想在这种情况下没有必要。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章