TrainerInternalConfig.h 5.2 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include "paddle/utils/Util.h"

#include <stdio.h>

#include "hl_gpu.h"
#include "paddle/gserver/gradientmachines/GradientMachine.h"

#include "TrainerConfig.pb.h"

#include "ParameterUpdater.h"
#include <fstream>
#include <sstream>
#include <stdlib.h>

namespace paddle {
/**
 * @brief TrainerStats object will statistics sample processed and total cost.
 *
 * There are two stats in it, the 'AvgCost' and 'CurrentAvgCost'. 'AvgCost'
 * means cost through one pass(all mini-batches). 'CurrentAvgCost' means cost
 * through one mini-batch.
 */
class TrainerStats {
public:
  /**
   * @brief reset all stats.
   *
   * often used before pass start.
   */
  inline void reset() {
    numProcessed_ = 0;
    totalCost_ = .0;
    this->resetCurrentStat();
  }

  /**
   * @brief reset current stat.
   *
   * 'current' means the most recent --log_period mini-batches
   */
  inline void resetCurrentStat() {
    currentCost_ = .0;
    currentSamples_ = 0;
  }

  /**
   * @brief add cost to stat.
   * @param numProcessed current mini-batch size
   * @param cost current mini-batch cost
   */
  inline void addCost(int64_t numProcessed, real cost) {
    this->numProcessed_ += numProcessed;
    this->totalCost_ += cost;
    this->currentSamples_ += numProcessed;
    this->currentCost_ += cost;
  }

  /**
   * @brief get average cost through on pass(all processed mini-batches)
   * @return pass average cost
   */
  inline real getAvgCost() const {
    CHECK_NE(this->numProcessed_, 0);
    return this->totalCost_ / this->numProcessed_;
  }

  /**
   * @brief get current mini-batch's average cost.
   * @return mini-batch average cost
   */
  inline real getCurrentAvgCost() const {
    CHECK_NE(this->currentSamples_, 0);
    return this->currentCost_ / this->currentSamples_;
  }

  /**
   * @brief get all processed samples' number
   * @return all processed samples' number
   */
96
  inline int64_t getNumProcessed() const { return this->numProcessed_; }
Z
zhangjinchao01 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110

  /**
   * @brief same function as addCost. But it is simple to invoke.
   * For example:
   *
   * @code{.cpp}
   * TrainerStats stat;
   * cost = neuralNetwork.forward(batchSize);
   * stat += {batchSize, cost};
   * @endcode
   *
   * @param p a pair of parameter, first is numProcessed, second is cost.
   * @return *this
   */
111
  inline TrainerStats& operator+=(const std::pair<int64_t, real>& p) {
Z
zhangjinchao01 已提交
112 113 114 115 116 117 118 119 120
    this->addCost(p.first, p.second);
    return *this;
  }

  /**
   * @brief TrainerStats Constructor.
   *
   * reset stat when constructed.
   */
121
  inline TrainerStats() { this->reset(); }
Z
zhangjinchao01 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134

  /**
   * @brief show stats to ostream.
   *
   * If there is no need to print current cost, set withCurrentCost to False.
   *
   * @param os output stream.
   * @param withCurrentCost print current cost or not.
   */
  void showStats(std::ostream& os, bool withCurrentCost = true) const {
    os << "samples=" << this->getNumProcessed()
       << " AvgCost=" << this->getAvgCost();
    if (withCurrentCost) {
135
      os << " CurrentCost=" << this->getCurrentAvgCost();
Z
zhangjinchao01 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    }
  }

  /**
   * @brief get stats to std::string
   * @param withCurrentCost return current cost or not
   * @return stats string
   */
  std::string getStats(bool withCurrentCost = true) const {
    std::ostringstream os;
    this->showStats(os, withCurrentCost);
    return os.str();
  }

private:
  int64_t numProcessed_;
  real totalCost_;
  real currentCost_;
  int64_t currentSamples_;
};

inline std::ostream& operator<<(std::ostream& os, const TrainerStats& stats) {
  stats.showStats(os);
  return os;
}

/**
 * TrainerInternalConfig
 * general configs for training
 */
struct TrainerInternalConfig {
  /**
   * @brief Create TrainerInternalConfig from GradientMachine::CreateMode and
   * command line arguments.
   * @param mode
   * @return
   */
  static std::unique_ptr<TrainerInternalConfig> createFromMode(
      GradientMachine::CreateMode mode);

  /**
   * indicate whether the training is local
   * if local, no parameter server is used
   */
  bool local;

  /**
   * indicate whether training uses GPU
   */
  bool use_gpu;

  /**
   * indicate number of trainer
   */
  int trainer_count;

  /**
   * how frequently to show param stats
   */
  int show_param_stats_period;

  /**
   * current trainer id
   */
  int trainer_id;

  /**
   * frequency to dump log
   */
  int log_period;

  /**
   * dot period
   */
  int dot_period;

  /**
   * num passes for training
   */
  int num_passes;

  /**
   * use old updater
   */
  bool use_old_updater;

  /**
   * whether to load and save parameter in pserver
   */
  bool loadsave_parameters_in_pserver;

  /**
   * training mode
   */
  GradientMachine::CreateMode mode;
};

}  //  namespace paddle