OptimizerWithRegularizer.h 5.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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 "FirstOrderOptimizer.h"

namespace paddle {

// add regularizer for objective function to do optimization
class OptimizerWithRegularizer : public ParameterOptimizer {
public:
  static ParameterOptimizer* create(const OptimizationConfig& optConfig,
                                    const ParameterConfig& paraConfig,
26 27
                                    bool isParameterSparse,
                                    bool inPserver);
Z
zhangjinchao01 已提交
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

  OptimizerWithRegularizer(const OptimizationConfig& optConfig,
                           ParameterOptimizer* optimizer,
                           Regularizer* regularizer)
      : ParameterOptimizer(optConfig),
        optimizer_(optimizer),
        regularizer_(regularizer) {
    parameterTypes_ = optimizer_->getParameterTypes();
  }

  virtual void init(size_t numRows, const ParameterConfig* config) {
    optimizer_->init(numRows, config);
  }

  virtual void startPass() {
    optimizer_->startPass();
    timer_ = 0;
  }

  virtual void finishPass() { optimizer_->finishPass(); }

  virtual void startBatch(int64_t numSamplesProcessed) {
    optimizer_->startBatch(numSamplesProcessed);
  }

  virtual void finishBatch() {
    optimizer_->finishBatch();
    ++timer_;
  }

  virtual TraverseCallback needSpecialTraversal(
      const ParameterConfig& config) const {
    return optimizer_->needSpecialTraversal(config);
  }

63 64
  virtual void update(const VectorPtr vecs[],
                      const ParameterConfig& config,
Z
zhangjinchao01 已提交
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 96 97
                      size_t sparseId) const {
    optimizer_->update(vecs, config, sparseId);
    regularizer_->update(vecs, config, optimizer_->getLearningRate(), 0, 1);
  }

protected:
  std::unique_ptr<ParameterOptimizer> optimizer_;
  Regularizer* regularizer_;

  /**
   *  counting batches, clear after catch up with
   *  t(timer_) is current time,
   *  t0(t0Vec_) are last occur time of i rows.
   *  if one block is update by multi threads,
   *  caller should hash sparse ids to avoid write conflict in t0Vec_.
   */
  int timer_;
};

// Regularized Loss function for every num of batches
class OptimizerWithRegularizerEveryNumBatches
    : public OptimizerWithRegularizer {
public:
  OptimizerWithRegularizerEveryNumBatches(const OptimizationConfig& optConfig,
                                          ParameterOptimizer* optimizer,
                                          Regularizer* regularizer)
      : OptimizerWithRegularizer(optConfig, optimizer, regularizer) {}

  virtual void startPass() {
    OptimizerWithRegularizer::startPass();
    baseTimer_ = 0;
  }

98 99
  virtual void update(const VectorPtr vecs[],
                      const ParameterConfig& config,
Z
zhangjinchao01 已提交
100 101 102 103 104 105 106 107
                      size_t sparseId) const {
    optimizer_->update(vecs, config, sparseId);
  }

  virtual TraverseCallback needSpecialTraversal(
      const ParameterConfig& config) const;
  void doTraversal(const VectorPtr vecs[], const ParameterConfig& config) const;

108 109
  void catchUpWith(const VectorPtr vecs[],
                   const ParameterConfig& config,
Z
zhangjinchao01 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                   size_t sparseId) const;

  virtual TraverseCallback startCatchUpWith() const;
  virtual void finishCatchUpWith() { baseTimer_ = timer_; }

protected:
  bool isRegularizationBatch(const ParameterConfig& config) const {
    return ((timer_ + 1) % config.num_batches_regularization() == 0);
  }

  /**
   *  recored the timer_ value while catchUpWith called.
   */
  int baseTimer_;
};

// Regularized Loss function with Sparse support
class OptimizerWithRegularizerSparse : public OptimizerWithRegularizer {
public:
  OptimizerWithRegularizerSparse(const OptimizationConfig& optConfig,
                                 ParameterOptimizer* optimizer,
                                 Regularizer* regularizer)
      : OptimizerWithRegularizer(optConfig, optimizer, regularizer) {}

  virtual void init(size_t numRows, const ParameterConfig* config);

136 137
  virtual void update(const VectorPtr vecs[],
                      const ParameterConfig& config,
Z
zhangjinchao01 已提交
138
                      size_t sparseId) const;
139 140
  void catchUpWith(const VectorPtr vecs[],
                   const ParameterConfig& config,
Z
zhangjinchao01 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                   size_t sparseId) const;
  virtual TraverseCallback startCatchUpWith() const;
  virtual void finishCatchUpWith() {
    timer_ = 0;
    t0Vec_.assign(t0Vec_.size(), 0);
  }

protected:
  /**
   *  t0Vec_ are last occur time of i rows
   *  if one block is update by multi threads,
   *  caller should hash sparse ids to avoid write conflict in t0Vec_.
   */
  mutable std::vector<int32_t> t0Vec_;
};

}  // namespace paddle