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):
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.