You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

train_loop_callback.h 3.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  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. */
  16. #ifndef MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_LOOP_CALLBACK_H_
  17. #define MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_LOOP_CALLBACK_H_
  18. #include <vector>
  19. #include <string>
  20. #include <tuple>
  21. #include <unordered_map>
  22. namespace mindspore {
  23. namespace session {
  24. class LiteSession;
  25. class TrainLoop;
  26. struct TrainLoopCallBackData {
  27. TrainLoopCallBackData(bool train_mode, unsigned int epoch, LiteSession *session, TrainLoop *loop)
  28. : train_mode_(train_mode), epoch_(epoch), session_(session), loop_(loop) {}
  29. bool train_mode_; /**< training mode of LiteSession object */
  30. unsigned int epoch_; /**< the current training epoch (starts at 0) */
  31. unsigned int step_ = 0; /**< the current step within the epoch */
  32. LiteSession *session_; /**< pointer to the LiteSession */
  33. TrainLoop *loop_;
  34. };
  35. constexpr int RET_CONTINUE = 0;
  36. constexpr int RET_STOP_TRAINING = 1;
  37. constexpr int RET_EXIT = 2;
  38. class TrainLoopCallBack {
  39. public:
  40. virtual ~TrainLoopCallBack() = default;
  41. /// \brief This method is called once before the network executing
  42. ///
  43. /// \param[in] cb_data info about current execution
  44. virtual void Begin(const TrainLoopCallBackData &cb_data) {}
  45. /// \brief This method is called once following the network execution
  46. ///
  47. /// \param[in] cb_data info about current execution
  48. virtual void End(const TrainLoopCallBackData &cb_data) {}
  49. /// \brief This method is called at the beginning of each epoch
  50. ///
  51. /// \param[in] cb_data info about current execution
  52. virtual void EpochBegin(const TrainLoopCallBackData &cb_data) {}
  53. /// \brief This method is called after the run of each epoch
  54. ///
  55. /// \param[in] cb_data info about current execution
  56. ///
  57. /// \return indication if to continue in the train loop:
  58. /// RET_CONTINUE -- continue training
  59. /// RET_STOP_TRAINING -- stop training (e.g., due to achieved accuracy)
  60. /// RET_EXIT -- Exit training (due to error of some sort)
  61. virtual int EpochEnd(const TrainLoopCallBackData &cb_data) { return RET_CONTINUE; }
  62. /// \brief This method is called at the beginning of each step
  63. ///
  64. /// \param[in] cb_data info about current execution
  65. virtual void StepBegin(const TrainLoopCallBackData &cb_data) {}
  66. /// \brief This method is called after each step is ran
  67. ///
  68. /// \param[in] cb_data info about current execution
  69. virtual void StepEnd(const TrainLoopCallBackData &cb_data) {}
  70. };
  71. } // namespace session
  72. } // namespace mindspore
  73. #endif // MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_LOOP_CALLBACK_H_