model_checkpoint.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from federatedml.callbacks.callback_base import CallbackBase
  16. from federatedml.util import LOGGER
  17. class ModelCheckpoint(CallbackBase):
  18. def __init__(self, model, save_freq):
  19. self.model = model
  20. if save_freq == "epoch":
  21. save_freq = 1
  22. self.save_freq = save_freq
  23. self.save_count = 0
  24. def add_checkpoint(self, step_index, step_name=None, to_save_model=None):
  25. step_name = step_name if step_name is not None else self.model.step_name
  26. to_save_model = to_save_model if to_save_model is not None else self.model.export_serialized_models()
  27. _checkpoint = self.model.checkpoint_manager.new_checkpoint(step_index=step_index, step_name=step_name)
  28. _checkpoint.save(to_save_model)
  29. LOGGER.debug(f"current checkpoint num: {self.model.checkpoint_manager.checkpoints_number}")
  30. return _checkpoint
  31. def on_epoch_end(self, model, epoch):
  32. if epoch % self.save_freq == 0:
  33. self.add_checkpoint(step_index=epoch)
  34. self.save_count += 1