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_LOSS_MONITOR_H_ 17 #define MINDSPORE_LITE_INCLUDE_TRAIN_LOSS_MONITOR_H_ 18 #include <vector> 19 #include <string> 20 #include <utility> 21 #include <climits> 22 #include <unordered_map> 23 #include "include/train/train_loop_callback.h" 24 25 using GraphPoint = std::pair<int, float>; 26 27 namespace mindspore { 28 namespace lite { 29 30 class LossMonitor : public session::TrainLoopCallBack { 31 public: 32 /// \brief constructor 33 /// 34 /// \param[in] print_every_n_steps prints loss into stdout every n_steps. 35 // print_every_n_steps=0 means never print 36 // print_every_n_steps=INT_MAX will print every epoch 37 /// \param[in] dataset Pointer to MindData Dataset object 38 /// \param[in] cbs A vector of TrainLoopCallBack objects 39 /// 40 /// \return 0 on success or -1 in case of error print_every_n_(print_every_n_steps)41 explicit LossMonitor(int print_every_n_steps = INT_MAX) : print_every_n_(print_every_n_steps) {} 42 virtual ~LossMonitor() = default; 43 void Begin(const session::TrainLoopCallBackData &cb_data) override; 44 void EpochBegin(const session::TrainLoopCallBackData &cb_data) override; 45 int EpochEnd(const session::TrainLoopCallBackData &cb_data) override; 46 void StepEnd(const session::TrainLoopCallBackData &cb_data) override; GetLossPoints()47 const std::vector<GraphPoint> &GetLossPoints() const { return losses_; } 48 49 private: 50 std::vector<GraphPoint> losses_; 51 int print_every_n_; 52 }; 53 54 } // namespace lite 55 } // namespace mindspore 56 #endif // MINDSPORE_LITE_INCLUDE_TRAIN_LOSS_MONITOR_H_ 57