【问题标题】:How I can save checkpoint every epoch and load an random saved checkpoint to continue training我如何在每个时期保存检查点并加载随机保存的检查点以继续训练
【发布时间】:2020-08-20 17:43:52
【问题描述】:

您能否帮我编写代码以:在每个时期保存模型(架构和权重),以及如何从第 5 个检查点继续训练我的模型,例如从 1 到 25 的训练时期而不进行检查点(第 5 个模型 I已保存)。

classifier = Sequential()

classifier.add(Conv2D(6, (3, 3), input_shape = (30, 30, 3), data_format="channels_last", activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Conv2D(6, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Flatten())

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 64, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

opt = Adam(learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-08, decay = 0.0)
classifier.compile(optimizer = opt, loss = 'binary_crossentropy', metrics = ['accuracy', precision, recall, fmeasure])

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   horizontal_flip = True,
                                   vertical_flip = True,
                                   rotation_range = 180)

validation_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('/home/dataset/training_set',
                                                 target_size = (30, 30),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

validation_set = validation_datagen.flow_from_directory('/home/dataset/validation_set',
                                                        target_size = (30, 30),
                                                        batch_size = 32,
                                                        class_mode = 'binary')

history = classifier.fit_generator(training_set,
                                   steps_per_epoch = 208170,
                                   epochs = 15,
                                   validation_data = validation_set,
                                   validation_steps = 89140)

【问题讨论】:

    标签: python tensorflow machine-learning keras deep-learning


    【解决方案1】:

    我假设你的意思是你想在每个 epoch 之后保存你的模型和权重,然后在稍后的阶段,加载第五个 epoch 之后保存的模型和权重。

    您通常可以像这样在 TensorFlow 中使用 SaveModel 格式:

    classifier.save()
    

    这将保存架构、权重、有关优化器的信息以及您在 compile() 中设置的配置

    由于您使用的是 fit_generator,因此您可以使用 ModelCheckpoint() 来保存您的模型,如下所示:

    from keras.callbacks import ModelCheckpoint
    
    checkpoint = ModelCheckpoint(path_to_save_to, save_freq = 'epoch', 
                                 save_weights_only = False)
    
    history = classifier.fit_generator(training_set,
                                       steps_per_epoch = 208170,
                                       epochs = 15,
                                       validation_data = validation_set,
                                       validation_steps = 89140,
                                        callbacks = [checkpoint])
    
    

    您可以格式化路径,以便它使用像 path_name + '-{epoch:02d}-{val_loss:.2f}.h5' 这样的纪元/损失详细信息保存模型

    要加载第五个检查点,请执行以下操作:

    from keras.models import load_model
    classifier = load_model(path_to_fifth_checkpoint)
    

    【讨论】:

    • 感谢您的帮助,但我该如何从第 5 个模型继续训练?
    • 一旦您提供了load_model() 的路径并加载了模型,classifier 将在第五个纪元之后保存与模型相关的权重和其他设置。
    • 谢谢,为了从第 5 个 epoch 模型继续训练,我只执行了从 classifier.compile() 到最后的代码?
    • 不完全确定您的意思。使用第五个 epoch 后保存的模型路径加载模型后,当您开始训练时,就好像您在第 5 个 epoch 后恢复训练一样。
    猜你喜欢
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 2020-07-31
    • 2018-03-04
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多