huber_loss_op.cc 4.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
yangyaming 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

15 16 17
#include <memory>
#include <string>
#include <vector>
Y
yangyaming 已提交
18

19 20 21 22
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/infermeta/binary.h"

Y
yangyaming 已提交
23 24 25 26 27 28 29 30 31 32 33
namespace paddle {
namespace operators {

class HuberLossOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

template <typename AttrType>
class HuberLossOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
34
  void Make() override {
35 36
    AddInput("X",
             "The input value of huber loss op."
37
             "X is a N-D tensor with shape [N_1, N_2,..., N_n].");
38 39
    AddInput("Y",
             "The target value of huber loss op."
40
             "Y is a N-D tensor with shape [N_1, N_2,..., N_n].");
41
    AddOutput("Residual",
42
              "Intermediate tensor to cache residual value between Y and X."
43
              "The shape is same as Input(X) and will be reused in backward.")
Y
yangyaming 已提交
44
        .AsIntermediate();
45
    AddOutput("Out",
46
              "The output N-D tensor with shape [N_1, N_2,..., N_n] "
K
kexinzhao 已提交
47
              "which represents the huber loss.");
Y
yangyaming 已提交
48 49
    AddAttr<AttrType>("delta", "Hyper parameter in huber loss.");
    AddComment(R"DOC(
K
kexinzhao 已提交
50 51
HuberLoss Operator.

52 53
Huber loss is a loss function used in robust regression. We define X as the
input value and Y as the target value. Huber loss can evaluate the fitness of
54
X to Y. Different from MSE loss, Huber loss is more robust for outliers. If the
55
shape of X and Y are [batch_size, 1]. The equation is:
Y
yangyaming 已提交
56

57
$$
Y
yangyaming 已提交
58
Out_{\delta}(X, Y)_i =
59
\begin{cases}
Y
yangyaming 已提交
60 61 62
0.5 * (Y_i - X_i)^2,
\quad |Y_i - X_i| \leq \delta \\
\delta * (|Y_i - X_i| - 0.5 * \delta),
63
\quad otherwise
64
\end{cases}
65
$$
Y
yangyaming 已提交
66

Y
yangyaming 已提交
67 68 69
In the above equation, $Out_\delta(X, Y)_i$, $X_i$ and $Y_i$ represent the ith
element of Out, X and Y.

Y
yangyaming 已提交
70 71 72 73 74 75 76 77
)DOC");
  }
};

class HuberLossGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

78
  void InferShape(framework::InferShapeContext* ctx) const override {
79 80
    OP_INOUT_CHECK(ctx->HasInputs(framework::GradVarName("Out")), "Input",
                   "Out@GRAD", "HuberLossGrad");
81 82 83 84 85 86

    auto residual_dims = ctx->GetInputDim("Residual");

    auto x_grad_name = framework::GradVarName("X");
    auto y_grad_name = framework::GradVarName("Y");
    if (ctx->HasOutput(x_grad_name)) {
87
      ctx->SetOutputDim(x_grad_name, residual_dims);
88 89
    }
    if (ctx->HasOutput(y_grad_name)) {
90
      ctx->SetOutputDim(y_grad_name, residual_dims);
91
    }
Y
yangyaming 已提交
92 93 94
  }
};

H
hong 已提交
95 96
template <typename T>
class HuberLossGradOpMaker : public framework::SingleGradOpMaker<T> {
97
 public:
H
hong 已提交
98
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
99 100

 protected:
101
  void Apply(GradOpPtr<T> op) const override {
102
    op->SetType("huber_loss_grad");
H
hong 已提交
103 104 105 106 107
    op->SetInput("Residual", this->Output("Residual"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetOutput(framework::GradVarName("Y"), this->InputGrad("Y"));
    op->SetAttrMap(this->Attrs());
108 109 110
  }
};

Y
yangyaming 已提交
111 112 113 114
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
115 116 117
DELCARE_INFER_SHAPE_FUNCTOR(huber_loss, HuberLossInferShapeFunctor,
                            PT_INFER_META(phi::HuberLossInferMeta));

Y
Yang Yang 已提交
118
REGISTER_OPERATOR(huber_loss, ops::HuberLossOp, ops::HuberLossOpMaker<float>,
H
hong 已提交
119
                  ops::HuberLossGradOpMaker<paddle::framework::OpDesc>,
120 121
                  ops::HuberLossGradOpMaker<paddle::imperative::OpBase>,
                  HuberLossInferShapeFunctor);
122
REGISTER_OPERATOR(huber_loss_grad, ops::HuberLossGradOp);