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