|
| 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/huber_loss_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class HuberLossOp : public framework::OperatorWithKernel { |
| 21 | + public: |
| 22 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 23 | + |
| 24 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 25 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must be initialized."); |
| 26 | + PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) must be initialized."); |
| 27 | + |
| 28 | + auto x_dims = ctx->GetInputDim("X"); |
| 29 | + auto y_dims = ctx->GetInputDim("Y"); |
| 30 | + |
| 31 | + PADDLE_ENFORCE_EQ(x_dims, y_dims); |
| 32 | + PADDLE_ENFORCE_EQ(x_dims.size(), 2, |
| 33 | + "The rank of Input(X) must be 2 and the shape is " |
| 34 | + "[batch_size, 1]."); |
| 35 | + PADDLE_ENFORCE_EQ(x_dims[1], 1, |
| 36 | + "Each row of Input(X) contains a real value, " |
| 37 | + "so the 2nd dimension of Input(X) must be 1."); |
| 38 | + |
| 39 | + ctx->SetOutputDim("Residual", x_dims); |
| 40 | + ctx->SetOutputDim("Out", {x_dims[0], 1}); |
| 41 | + ctx->ShareLoD("X", "Out"); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +template <typename AttrType> |
| 46 | +class HuberLossOpMaker : public framework::OpProtoAndCheckerMaker { |
| 47 | + public: |
| 48 | + HuberLossOpMaker(framework::OpProto* proto, |
| 49 | + framework::OpAttrChecker* op_checker) |
| 50 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 51 | + AddInput("X", |
| 52 | + "The input value of huber loss op." |
| 53 | + "X is a 2-D tensor with shape [batch_size, 1]."); |
| 54 | + AddInput("Y", |
| 55 | + "The target value of huber loss op." |
| 56 | + "Y is a 2-D tensor with shape [batch_size, 1]."); |
| 57 | + AddOutput("Residual", |
| 58 | + "Intermediate tensor to cache residual value between Y and X." |
| 59 | + "The shape is same as Input(X) and will be reused in backward.") |
| 60 | + .AsIntermediate(); |
| 61 | + AddOutput("Out", |
| 62 | + "The output tensor with shape [batch_size, 1] which represents " |
| 63 | + "the huber loss."); |
| 64 | + AddAttr<AttrType>("delta", "Hyper parameter in huber loss."); |
| 65 | + AddComment(R"DOC( |
| 66 | +Huber loss is a loss function used in robust regression. We define X as the |
| 67 | +input value and Y as the target value. Huber loss can evaluate the fitness of |
| 68 | +X to Y. Different from MSE loss, Huber loss is more robust for outliers. The |
| 69 | +shape of X and Y are [batch_size, 1]. The equation is: |
| 70 | +
|
| 71 | +L_{\delta}(y, f(x)) = |
| 72 | +\begin{cases} |
| 73 | +0.5 * (y - f(x))^2, \quad |y - f(x)| \leq \delta \\ |
| 74 | +\delta * (|y - f(x)| - 0.5 * \delta), \quad otherwise |
| 75 | +\end{cases} |
| 76 | +
|
| 77 | +)DOC"); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +class HuberLossGradOp : public framework::OperatorWithKernel { |
| 82 | + public: |
| 83 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 84 | + |
| 85 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 86 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null."); |
| 87 | + PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) should not be null."); |
| 88 | + PADDLE_ENFORCE(ctx->HasInput("Residual"), |
| 89 | + "Input(Residual) should not be null."); |
| 90 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")), |
| 91 | + "Input(Out@GRAD) should not be null."); |
| 92 | + |
| 93 | + auto x_dims = ctx->GetInputDim("X"); |
| 94 | + auto y_dims = ctx->GetInputDim("Y"); |
| 95 | + auto residual_dims = ctx->GetInputDim("Residual"); |
| 96 | + auto out_grad_dims = ctx->GetInputDim(framework::GradVarName("Out")); |
| 97 | + |
| 98 | + PADDLE_ENFORCE_EQ(residual_dims, x_dims); |
| 99 | + PADDLE_ENFORCE_EQ(out_grad_dims, x_dims); |
| 100 | + |
| 101 | + auto x_grad_name = framework::GradVarName("X"); |
| 102 | + auto y_grad_name = framework::GradVarName("Y"); |
| 103 | + if (ctx->HasOutput(x_grad_name)) { |
| 104 | + ctx->SetOutputDim(x_grad_name, x_dims); |
| 105 | + } |
| 106 | + if (ctx->HasOutput(y_grad_name)) { |
| 107 | + ctx->SetOutputDim(y_grad_name, y_dims); |
| 108 | + } |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +} // namespace operators |
| 113 | +} // namespace paddle |
| 114 | + |
| 115 | +namespace ops = paddle::operators; |
| 116 | +REGISTER_OP(huber_loss, ops::HuberLossOp, ops::HuberLossOpMaker<float>, |
| 117 | + huber_loss_grad, ops::HuberLossGradOp); |
| 118 | +REGISTER_OP_CPU_KERNEL(huber_loss, |
| 119 | + ops::HuberLossKernel<paddle::platform::CPUPlace, float>); |
| 120 | +REGISTER_OP_CPU_KERNEL( |
| 121 | + huber_loss_grad, |
| 122 | + ops::HuberLossGradKernel<paddle::platform::CPUPlace, float>); |
0 commit comments