DataNormLayer.h 1.9 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

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 "Layer.h"
#include "paddle/math/Matrix.h"
#include "paddle/utils/ThreadLocal.h"

namespace paddle {

/**
Q
qijun 已提交
24 25
 * @brief A layer for data normalization
 * - Input: One and only one input layer is accepted. The input layer must
Z
zhangjinchao01 已提交
26
 *        be DataLayer with dense data type.
Q
qijun 已提交
27
 * - Output: The normalization of the input data
Z
zhangjinchao01 已提交
28 29 30 31 32
 *
 * Reference:
 *    LA Shalabi, Z Shaaban, B Kasasbeh. Data mining: A preprocessing engine
 *
 * Three data normalization methoeds are considered
Q
qijun 已提交
33 34 35
 * - z-score: y = (x-mean)/std
 * - min-max: y = (x-min)/(max-min)
 * - decimal-scaling: y = x/10^j, where j is the smallest integer such that
Z
zhangjinchao01 已提交
36 37 38 39 40 41 42 43 44 45 46
 *max(|y|)<1
 */

class DataNormLayer : public Layer {
public:
  enum NormalizationStrategy { kZScore = 0, kMinMax = 1, kDecimalScaling = 2 };

  explicit DataNormLayer(const LayerConfig& config) : Layer(config) {}

  ~DataNormLayer() {}

Y
Yu Yang 已提交
47 48
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
Z
zhangjinchao01 已提交
49

Y
Yu Yang 已提交
50 51
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback = nullptr) override;
Z
zhangjinchao01 已提交
52 53 54 55 56 57 58 59 60 61 62

protected:
  int mode_;
  std::unique_ptr<Weight> weight_;
  MatrixPtr min_;
  MatrixPtr rangeReciprocal_;  // 1/(max-min)
  MatrixPtr mean_;
  MatrixPtr stdReciprocal_;      // 1/std
  MatrixPtr decimalReciprocal_;  // 1/10^j
};
}  // namespace paddle