HierarchicalSigmoidLayer.h 2.6 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

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"

namespace paddle {

/**
22
 * Organize the classes into a binary tree. At each node, a sigmoid function
Z
zhangjinchao01 已提交
23
 * is used to calculate the probability of belonging to the right branch.
24
 * This idea is from "F. Morin, Y. Bengio (AISTATS 05):
Z
zhangjinchao01 已提交
25 26 27 28 29
 * Hierarchical Probabilistic Neural Network Language Model."
 *
 * Here we uses a simple way of making the binary tree.
 * Assuming the number of classes C = 6,
 * The classes are organized as a binary tree in the following way:
30
 *
Z
zhangjinchao01 已提交
31 32 33 34 35 36 37 38
 * @code{.py}
 * *-*-*- 2
 * | | |- 3
 * | |
 * | |-*- 4
 * |   |- 5
 * |
 * |-*- 0
Y
Yu Yang 已提交
39
 *   |- 1
Z
zhangjinchao01 已提交
40 41 42 43 44 45
 * @endcode
 *
 * where * indicates an internal node, and each leaf node represents a class.
 * - Node 0 ... C-2 are internal nodes.
 * - Node C-1 ... 2C-2 are leaf nodes.
 * - Class c is represented by leaf node \f$c+C-1\f$.
46
 *
Z
zhangjinchao01 已提交
47 48 49 50 51 52
 * We assign an id for each node:
 * - the id of root be 0.
 * - the left child of a node i is 2*i+1.
 * - the right child of a node i is 2*i+2.
 *
 * It's easy to see that:
53 54
 * - the parent of node i is \f$\left\lfloor(i-1)/2\right\rfloor\f$.
 * - the j-th level ancestor of node i is
Z
zhangjinchao01 已提交
55 56 57 58 59 60 61 62 63
 * \f$\left\lfloor(i+1)/2^{j+1}\right\rfloor - 1\f$.
 * - A node i is a left child of its parent if \f$(i-1)\%2==0\f$.
 *
 * The config file api is hsigmod_layer.
 */
class HierarchicalSigmoidLayer : public Layer {
public:
  explicit HierarchicalSigmoidLayer(const LayerConfig& config)
      : Layer(config) {}
Y
Yu Yang 已提交
64 65 66 67
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback) override;
Z
zhangjinchao01 已提交
68 69 70 71

protected:
  /**
   * The last of inputs is label layer.
72
   */
Z
zhangjinchao01 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
  LayerPtr getLabelLayer() { return inputLayers_.back(); }

  WeightList weights_;
  std::unique_ptr<Weight> biases_;
  /// number of classes
  size_t numClasses_;
  /// codeLength_ = \f$1 + \left\lfloor log_{2}(numClasses-1)\right\rfloor\f$
  int codeLength_;
  /// temporary result of output_
  Argument preOutput_;
};

}  // namespace paddle