|
| 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/operators/adagrad_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class AdagradOp : public framework::OperatorWithKernel { |
| 21 | + public: |
| 22 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 23 | + |
| 24 | + protected: |
| 25 | + void InferShape(framework::InferShapeContextBase *ctx) const override { |
| 26 | + PADDLE_ENFORCE(ctx->HasInput("Param"), |
| 27 | + "Input(Param) of AdagradOp should not be null."); |
| 28 | + PADDLE_ENFORCE(ctx->HasInput("Grad"), |
| 29 | + "Input(Grad) of AdagradOp should not be null."); |
| 30 | + PADDLE_ENFORCE(ctx->HasInput("Moment"), |
| 31 | + "Input(Moment) of AdagradOp should not be null."); |
| 32 | + PADDLE_ENFORCE(ctx->HasInput("LearningRate"), |
| 33 | + "Input(LearningRate) of AdagradOp should not be null."); |
| 34 | + |
| 35 | + PADDLE_ENFORCE(ctx->HasOutput("ParamOut"), |
| 36 | + "Output(ParamOut) of AdagradOp should not be null."); |
| 37 | + PADDLE_ENFORCE(ctx->HasOutput("MomentOut"), |
| 38 | + "Output(MomentOut) of AdagradOp should not be null."); |
| 39 | + |
| 40 | + auto lr_dims = ctx->GetInputDim("LearningRate"); |
| 41 | + PADDLE_ENFORCE_EQ(framework::product(lr_dims), 1, |
| 42 | + "LearningRate should have one element"); |
| 43 | + auto param_dims = ctx->GetInputDim("Param"); |
| 44 | + PADDLE_ENFORCE_EQ( |
| 45 | + param_dims, ctx->GetInputDim("Grad"), |
| 46 | + "Param and Grad input of AdagradOp should have the same dimension."); |
| 47 | + PADDLE_ENFORCE_EQ( |
| 48 | + param_dims, ctx->GetInputDim("Moment"), |
| 49 | + "Param and Moment input of AdagradOp should have the same dimension."); |
| 50 | + |
| 51 | + ctx->SetOutputDim("ParamOut", param_dims); |
| 52 | + ctx->SetOutputDim("MomentOut", param_dims); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +class AdagradOpMaker : public framework::OpProtoAndCheckerMaker { |
| 57 | + public: |
| 58 | + AdagradOpMaker(framework::OpProto *proto, |
| 59 | + framework::OpAttrChecker *op_checker) |
| 60 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 61 | + AddInput("Param", "(Tensor) Input parameter"); |
| 62 | + AddInput("Grad", "(Tensor) Input gradient"); |
| 63 | + AddInput("Moment", "(Tensor) Second moment"); |
| 64 | + AddInput("LearningRate", "(Tensor) Learning rate"); |
| 65 | + |
| 66 | + AddOutput("ParamOut", "(Tensor) Output parameter"); |
| 67 | + AddOutput("MomentOut", "(Tensor) Output second moment"); |
| 68 | + |
| 69 | + AddAttr<float>("epsilon", |
| 70 | + "(float, default 1.0e-6) " |
| 71 | + "Constant for numerical stability") |
| 72 | + .SetDefault(1.0e-6f); |
| 73 | + AddComment(R"DOC( |
| 74 | +
|
| 75 | +Adaptive Gradient Algorithm (Adagrad). |
| 76 | +
|
| 77 | +moment_out = moment + grad * grad |
| 78 | +param_out = param - learning_rate * grad / (sqrt(moment_out) + epsilon) |
| 79 | +
|
| 80 | +The original paper(http://www.jmlr.org/papers/volume12/duchi11a/duchi11a.pdf) |
| 81 | +does not have the epsilon attribute. It is added here for numerical stability |
| 82 | +by avoiding division by zero. |
| 83 | +
|
| 84 | +)DOC"); |
| 85 | + } |
| 86 | +}; |
| 87 | +} // namespace operators |
| 88 | +} // namespace paddle |
| 89 | + |
| 90 | +namespace ops = paddle::operators; |
| 91 | +REGISTER_OP_WITHOUT_GRADIENT(adagrad, ops::AdagradOp, ops::AdagradOpMaker); |
| 92 | +REGISTER_OP_CPU_KERNEL(adagrad, |
| 93 | + ops::AdagradOpKernel<paddle::platform::CPUPlace, float>); |
0 commit comments