DataNormLayer.cpp 4.8 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 "DataNormLayer.h"
X
Xin Pan 已提交
16 17
#include "paddle/legacy/utils/Logging.h"
#include "paddle/legacy/utils/Stat.h"
Z
zhangjinchao01 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

namespace paddle {

REGISTER_LAYER(data_norm, DataNormLayer);

bool DataNormLayer::init(const LayerMap& layerMap,
                         const ParameterMap& parameterMap) {
  /* Initialize the basic parent class */
  Layer::init(layerMap, parameterMap);

  /* initialize the weight */
  CHECK(!biasParameter_) << "DataNormLayer does not need bias";
  CHECK(inputLayers_.size() == 1 && inputLayers_[0]->getType() == "data")
      << "DataNormLayer accepts one and only one DataLayer as its input layer";
  CHECK_EQ(inputLayers_.size(), parameters_.size());
  CHECK_EQ(inputLayers_[0]->getSize(), getSize());
  CHECK_EQ(parameters_[0]->getSize(), 5 * getSize());
  CHECK(parameters_[0]->isStatic())
      << "The parameter of DataNormLayer must be static";

  weight_ = std::unique_ptr<Weight>(new Weight(5, getSize(), parameters_[0]));
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  min_ = Matrix::create(
      nullptr, /* height= */ 1, getSize(), /* trans= */ false, useGpu_);
  rangeReciprocal_ = Matrix::create(nullptr,
                                    /* height= */ 1,
                                    getSize(),
                                    /* trans= */ false,
                                    useGpu_);
  mean_ = Matrix::create(nullptr,
                         /* height= */ 1,
                         getSize(),
                         /* trans= */ false,
                         useGpu_);
  stdReciprocal_ = Matrix::create(nullptr,
                                  /* height= */ 1,
                                  getSize(),
                                  /* trans= */ false,
                                  useGpu_);
  decimalReciprocal_ = Matrix::create(nullptr,
                                      /* height= */ 1,
                                      getSize(),
                                      /* trans= */ false,
                                      useGpu_);
Z
zhangjinchao01 已提交
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

  min_->setData(weight_->getW()->getData());
  rangeReciprocal_->setData(weight_->getW()->getData() + getSize());
  mean_->setData(weight_->getW()->getData() + 2 * getSize());
  stdReciprocal_->setData(weight_->getW()->getData() + 3 * getSize());
  decimalReciprocal_->setData(weight_->getW()->getData() + 4 * getSize());

  /* normalization strategy */
  if (config_.data_norm_strategy() == "z-score") {
    mode_ = kZScore;
  } else if (config_.data_norm_strategy() == "min-max") {
    mode_ = kMinMax;
  } else if (config_.data_norm_strategy() == "decimal-scaling") {
    mode_ = kDecimalScaling;
  } else {
    LOG(FATAL) << "Unknown data normalization strategy: "
               << config_.data_norm_strategy();
  }

  return true;
}

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

  /* malloc memory for the output_ if necessary */
  int batchSize = getInput(0).getBatchSize();
  int size = getSize();
  reserveOutput(batchSize, size);

  const MatrixPtr inValue = getInputValue(0);
  MatrixPtr outValue = getOutputValue();
  outValue->copyFrom(*inValue);
  switch (mode_) {
    case kZScore: {
      outValue->addBias(*mean_, -1.0);
      outValue->colScale(0, *outValue, *stdReciprocal_);
      break;
    }
    case kMinMax: {
      outValue->addBias(*min_, -1.0);
      outValue->colScale(0, *outValue, *rangeReciprocal_);
      break;
    }
    case kDecimalScaling: {
      outValue->colScale(0, *outValue, *decimalReciprocal_);
      break;
    }
    default:
      LOG(FATAL) << "should not reach here";
  }
}

void DataNormLayer::backward(const UpdateCallback& callback) {
  // The parameter for DataNormLayer is static, and does not need to be updated
  (void)callback;

  /* Calculate the input layers error */
  const MatrixPtr& outGrad = getOutputGrad();
  MatrixPtr inGrad = getInputGrad(0);
  if (inGrad) {
    switch (mode_) {
      case kZScore: {
        inGrad->addColScale(0, *outGrad, *stdReciprocal_);
        break;
      }
      case kMinMax: {
        inGrad->addColScale(0, *outGrad, *rangeReciprocal_);
        break;
      }
      case kDecimalScaling: {
        inGrad->addColScale(0, *outGrad, *decimalReciprocal_);
        break;
      }
      default: { LOG(FATAL) << "should not reach here"; }
    }
  }
}

}  // namespace paddle