Keras 保存网络权值和优化器optimizer状态,继续上一个周期状态训练

1.若是使用model.save()则不仅会保存权值,也会保存优化器状态

You can use model.save(filepath) to save a Keras model into a single HDF5 file which will contain:

  • the architecture of the model, allowing to re-create the model
  • the weights of the model
  • the training configuration (loss, optimizer)
  • the state of the optimizer, allowing to resume training exactly where you left off.

You can then use keras.models.load_model(filepath) to reinstantiate your model. load_model will also take care of compiling the model using the saved training configuration (unless the model was never compiled in the first place).


2. 用model.optimizer.set_weights和get_weights保存和恢复优化器状态,结合下面一个链接,get之后dump保存,之后再恢复  点击打开链接

Saving state of callbacks and restoring them 点击打开链接


3. 修改optimizer函数(谷歌论坛上的)

To whom it could help:
I fixed the problem by extending the RMSprop class (you could do the same with other optimizers, or maybe better with the super class Optimizer itself):

import numpy as np

class 
MyRMSprop ( RMSprop ):
    def __init__(self, path_weights=None, path_updates=None, lr=0.001, rho=0.9, epsilon=1e-8, **kwargs):
        self.path_weights = path_weights
        self.path_updates = path_updates
        super(MyRMSprop, self).__init__(lr, rho, epsilon, **kwargs)
    
    def get_updates(self, params, constraints, loss):
        tmp = super(MyRMSprop, self).get_updates(params, constraints, loss)
        if self.path_weights is not None:
            weights = np.load(self.path_weights)
            self.set_weights(weights)
        if self.path_updates is not None:
            updates = np.load(self.path_updates)
            self.set_state(updates)
        return tmp
    
    def save_weights(self, path):
        weights = self.get_weights()
        np.save(path, weights)

    def save_updates(self, path):
        updates = self.get_state()
        np.save(path, updates)

So all in all, after training I save the weights and updates (= state of the optimizer) as numpy arrays in a file. When I continue training, I load these files and restore the state of the optimizer.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值