|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/operators/hierarchical_sigmoid_op.h" |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | + |
| 21 | +/** |
| 22 | + * Organize the classes into a binary tree. At each node, a sigmoid function |
| 23 | + * is used to calculate the probability of belonging to the right branch. |
| 24 | + * This idea is from "F. Morin, Y. Bengio (AISTATS 05): |
| 25 | + * Hierarchical Probabilistic Neural Network Language Model." |
| 26 | + * |
| 27 | + * Here we uses a simple way of making the binary tree. |
| 28 | + * Assuming the number of classes C = 6, |
| 29 | + * The classes are organized as a binary tree in the following way: |
| 30 | + * |
| 31 | + * @code{.py} |
| 32 | + * *-*-*- 2 |
| 33 | + * | | |- 3 |
| 34 | + * | | |
| 35 | + * | |-*- 4 |
| 36 | + * | |- 5 |
| 37 | + * | |
| 38 | + * |-*- 0 |
| 39 | + * |- 1 |
| 40 | + * @endcode |
| 41 | + * |
| 42 | + * where * indicates an internal node, and each leaf node represents a class. |
| 43 | + * - Node 0 ... C-2 are internal nodes. |
| 44 | + * - Node C-1 ... 2C-2 are leaf nodes. |
| 45 | + * - Class c is represented by leaf node \f$c+C-1\f$. |
| 46 | + * |
| 47 | + * We assign an id for each node: |
| 48 | + * - the id of root be 0. |
| 49 | + * - the left child of a node i is 2*i+1. |
| 50 | + * - the right child of a node i is 2*i+2. |
| 51 | + * |
| 52 | + * It's easy to see that: |
| 53 | + * - the parent of node i is \f$\left\lfloor(i-1)/2\right\rfloor\f$. |
| 54 | + * - the j-th level ancestor of node i is |
| 55 | + * \f$\left\lfloor(i+1)/2^{j+1}\right\rfloor - 1\f$. |
| 56 | + * - A node i is a left child of its parent if \f$(i-1)\%2==0\f$. |
| 57 | + * |
| 58 | + */ |
| 59 | + |
| 60 | +class HierarchicalSigmoidOp : public framework::OperatorWithKernel { |
| 61 | + public: |
| 62 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 63 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 64 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null."); |
| 65 | + PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should not be null."); |
| 66 | + PADDLE_ENFORCE(ctx->HasInput("W"), "Input(W) should not be null."); |
| 67 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) should not be null."); |
| 68 | + PADDLE_ENFORCE(ctx->HasOutput("PreOut"), |
| 69 | + "Output(PreOut) should not be null."); |
| 70 | + const int64_t batch_size = ctx->GetInputDim("X")[0]; |
| 71 | + std::vector<int64_t> output_shape({batch_size, 1}); |
| 72 | + ctx->SetOutputDim("Out", framework::make_ddim(output_shape)); |
| 73 | + } |
| 74 | + |
| 75 | + protected: |
| 76 | + framework::OpKernelType GetExpectedKernelType( |
| 77 | + const framework::ExecutionContext& ctx) const override { |
| 78 | + return framework::OpKernelType( |
| 79 | + framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()), |
| 80 | + ctx.GetPlace()); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +template <typename AttrType> |
| 85 | +class HierarchicalSigmoidOpMaker : public framework::OpProtoAndCheckerMaker { |
| 86 | + public: |
| 87 | + void Make() override { |
| 88 | + AddInput("X", |
| 89 | + "(Tensor, required) The input tensor with shape [N, D], " |
| 90 | + "where N is the size of mini-batch, and D is the feature size."); |
| 91 | + AddInput("W", |
| 92 | + "(Tensor, required), The parameters of hierarchical " |
| 93 | + "sigmoid operator, each of them is a 2-D tensor, the shape is" |
| 94 | + "[num_classes - 1, D]."); |
| 95 | + AddInput("Label", |
| 96 | + "(Tensor, required), The labels of training data. It's a" |
| 97 | + "tensor with shape [N, 1]."); |
| 98 | + AddInput("Bias", |
| 99 | + "(Tensor, optional), The bias is a tensor with shape" |
| 100 | + "[1, num_classes - 1]."); |
| 101 | + AddOutput("Out", |
| 102 | + "(Tensor, required) The output of hierarchical sigmoid operator." |
| 103 | + "The shape is [N, 1]."); |
| 104 | + AddOutput("PreOut", |
| 105 | + "(Tensor, required) A intermedia 2-D tensor with shape " |
| 106 | + "[batch_size, code_length], where code_length represents the " |
| 107 | + "maximum path length from root to leaf nodes.") |
| 108 | + .AsIntermediate(); |
| 109 | + AddAttr<AttrType>("num_classes", "(int, required), The number of classes") |
| 110 | + .SetDefault(2); |
| 111 | + AddComment(R"DOC( |
| 112 | +The hierarchical sigmoid operator organize the classes into a binary tree. |
| 113 | +At each node, a sigmoid function is used to calculate the probability of |
| 114 | +belonging to the right branch. This idea is from |
| 115 | +"F. Morin, Y. Bengio (AISTATS 05): |
| 116 | +Hierarchical Probabilistic Neural Network Language Model." |
| 117 | + )DOC"); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +class HierarchicalSigmoidGradOp : public framework::OperatorWithKernel { |
| 122 | + public: |
| 123 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 124 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 125 | + PADDLE_ENFORCE(ctx->HasInput("W"), "Input(W) should not be null."); |
| 126 | + PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should not be null."); |
| 127 | + PADDLE_ENFORCE(ctx->HasInput("PreOut"), |
| 128 | + "Input(Preout) should not be null."); |
| 129 | + PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("W")), |
| 130 | + "Output(W@Grad should not be null.)"); |
| 131 | + PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X"))); |
| 132 | + if (ctx->HasOutput(framework::GradVarName("Bias"))) { |
| 133 | + ctx->SetOutputDim(framework::GradVarName("Bias"), |
| 134 | + ctx->GetInputDim("Bias")); |
| 135 | + } |
| 136 | + ctx->SetOutputDim(framework::GradVarName("W"), ctx->GetInputDim("W")); |
| 137 | + ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); |
| 138 | + } |
| 139 | + |
| 140 | + protected: |
| 141 | + framework::OpKernelType GetExpectedKernelType( |
| 142 | + const framework::ExecutionContext& ctx) const override { |
| 143 | + return framework::OpKernelType( |
| 144 | + framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()), |
| 145 | + ctx.GetPlace()); |
| 146 | + } |
| 147 | +}; |
| 148 | + |
| 149 | +} // namespace operators |
| 150 | +} // namespace paddle |
| 151 | + |
| 152 | +namespace ops = paddle::operators; |
| 153 | +REGISTER_OPERATOR(hierarchical_sigmoid, ops::HierarchicalSigmoidOp, |
| 154 | + ops::HierarchicalSigmoidOpMaker<int>, |
| 155 | + paddle::framework::DefaultGradOpDescMaker<true>); |
| 156 | +REGISTER_OPERATOR(hierarchical_sigmoid_grad, ops::HierarchicalSigmoidGradOp); |
| 157 | +REGISTER_OP_CPU_KERNEL( |
| 158 | + hierarchical_sigmoid, |
| 159 | + ops::HierarchicalSigmoidOpKernel<paddle::platform::CPUDeviceContext, float>, |
| 160 | + ops::HierarchicalSigmoidOpKernel<paddle::platform::CPUDeviceContext, |
| 161 | + double>); |
| 162 | +REGISTER_OP_CPU_KERNEL( |
| 163 | + hierarchical_sigmoid_grad, |
| 164 | + ops::HierarchicalSigmoidGradOpKernel<paddle::platform::CPUDeviceContext, |
| 165 | + float>, |
| 166 | + ops::HierarchicalSigmoidGradOpKernel<paddle::platform::CPUDeviceContext, |
| 167 | + double>); |
0 commit comments