SumToOneNormLayer.cpp 3.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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. */

#include "Layer.h"
X
Xin Pan 已提交
16
#include "paddle/legacy/math/Matrix.h"
X
Xin Pan 已提交
17 18
#include "paddle/legacy/utils/Logging.h"
#include "paddle/legacy/utils/Stat.h"
Z
zhangjinchao01 已提交
19 20 21 22

namespace paddle {

/**
23
 * A layer for sum-to-one normalization,
Z
zhangjinchao01 已提交
24 25 26 27 28 29 30 31 32 33 34
 * which is used in NEURAL TURING MACHINE.
 * \f[
 *   out[i] = \frac {in[i]} {\sum_{k=1}^N in[k]}
 * \f]
 * where \f$in\f$ is a (batchSize x dataDim) input vector,
 * and \f$out\f$ is a (batchSize x dataDim) output vector.
 *
 * The config file api is sum_to_one_norm_layer.
 */

class SumToOneNormLayer : public Layer {
W
Wu Yi 已提交
35
 protected:
Z
zhangjinchao01 已提交
36 37 38 39 40
  /// reciprocalRowSum_ = \f$1 / \sum_{k=1}^N in[k]\f$
  MatrixPtr reciprocalRowSum_;
  /// dotSum = output_.grad \f$.*\f$ output_.value
  MatrixPtr dotSum_;

W
Wu Yi 已提交
41
 public:
Z
zhangjinchao01 已提交
42 43
  explicit SumToOneNormLayer(const LayerConfig& config) : Layer(config) {}

Y
Yu Yang 已提交
44 45
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
Z
zhangjinchao01 已提交
46

Y
Yu Yang 已提交
47 48
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback = nullptr) override;
Z
zhangjinchao01 已提交
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
};

REGISTER_LAYER(sum_to_one_norm, SumToOneNormLayer);

bool SumToOneNormLayer::init(const LayerMap& layerMap,
                             const ParameterMap& parameterMap) {
  Layer::init(layerMap, parameterMap);

  CHECK_EQ(inputLayers_.size(), 1U);

  return true;
}

void SumToOneNormLayer::forward(PassType passType) {
  Layer::forward(passType);

  MatrixPtr inV = getInputValue(0);

  /* malloc memory for the output_ if necessary */
  size_t batchSize = inV->getHeight();
  size_t dataDim = getSize();

  CHECK_EQ(dataDim, inV->getWidth());

  {
    REGISTER_TIMER_INFO("FwResetTimer", getName().c_str());
    resetOutput(batchSize, dataDim);
  }

  MatrixPtr outV = getOutputValue();
  {
    REGISTER_TIMER_INFO("FwSumToOneNormTimer", getName().c_str());

    Matrix::resizeOrCreate(reciprocalRowSum_, batchSize, 1, false, useGpu_);
    inV->rowSum(*reciprocalRowSum_);

    // todo: matrix checks
    CHECK_GT(reciprocalRowSum_->getMin(), 0.0);

    reciprocalRowSum_->scalarDiv(*reciprocalRowSum_, 1.0);

    // outV = inV * reciprocalRowSum
    outV->rowScale(0, *inV, *reciprocalRowSum_);
  }
}

void SumToOneNormLayer::backward(const UpdateCallback& callback) {
  MatrixPtr inV = getInputValue(0);
  MatrixPtr inG = getInputGrad(0);
  MatrixPtr outV = getOutputValue();
  MatrixPtr outG = getOutputGrad();

  size_t batchSize = inV->getHeight();

  if (inG) {
    REGISTER_TIMER_INFO("BwSumToOneTimer", getName().c_str());

    Matrix::resizeOrCreate(dotSum_, batchSize, 1, false, useGpu_);

    // dotSum = outG .* outV
    dotSum_->zeroMem();
    dotSum_->rowDotMul(0, *outG, *outV);

    // inG += -1 * (dotSum / rowSum)
    dotSum_->dotMul(*dotSum_, *reciprocalRowSum_);
    inG->rowAdd(0, *inG, *dotSum_, -1.0);
    // inG += outG * (1/rowSum)
    inG->addRowScale(0, *outG, *reciprocalRowSum_);
  }
}

}  // namespace paddle