smooth_l1_loss_op.cc 5.5 KB
Newer Older
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

3 4 5
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
6

7
    http://www.apache.org/licenses/LICENSE-2.0
8

9 10 11 12 13
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. */
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/smooth_l1_loss_op.h"
16 17 18 19 20 21 22 23

namespace paddle {
namespace operators {

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

24
  void InferShape(framework::InferShapeContext* ctx) const override {
25 26
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) should not be null.");
27 28 29

    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");
30
    PADDLE_ENFORCE_EQ(x_dims, y_dims);
31
    PADDLE_ENFORCE_GE(x_dims.size(), 2,
32
                      "The tensor rank of Input(X) should not be less than 2.");
33 34 35 36
    if (ctx->HasInput("InsideWeight")) {
      PADDLE_ENFORCE(ctx->HasInput("OutsideWeight"),
                     "If weights are provided, must specify both "
                     "inside and outside weights.");
37 38
      PADDLE_ENFORCE_EQ(ctx->GetInputDim("InsideWeight"), x_dims);
      PADDLE_ENFORCE_EQ(ctx->GetInputDim("OutsideWeight"), x_dims);
39 40
    }

41
    ctx->SetOutputDim("Diff", x_dims);
42
    // loss is a two-rank tensor
43
    ctx->SetOutputDim("Out", {x_dims[0], 1});
44 45 46 47 48 49
  }
};

template <typename AttrType>
class SmoothL1LossOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
50
  SmoothL1LossOpMaker(OpProto* proto, OpAttrChecker* op_checker)
51
      : OpProtoAndCheckerMaker(proto, op_checker) {
Y
yangyaming 已提交
52
    AddInput("X",
53 54 55
             "(Tensor, default Tensor<float>) A tensor with rank at least 2. "
             "The input value of smooth l1 loss op with shape "
             "[batch_size, dim1, ..., dimN].");
Y
yangyaming 已提交
56
    AddInput("Y",
57 58
             "(Tensor, default Tensor<float>) A tensor with rank at least 2. "
             "The target value of smooth l1 loss op with same shape as X.");
Y
yangyaming 已提交
59
    AddInput("InsideWeight",
60 61 62
             "(Tensor, default Tensor<float>) A tensor with rank at least 2. "
             "This input is optional and should have same shape with X. "
             "If provided, the result of (X - Y) will be multiplied "
63 64
             "by this tensor element by element.")
        .AsDispensable();
Y
yangyaming 已提交
65
    AddInput("OutsideWeight",
66 67 68 69
             "(Tensor, default Tensor<float>) A tensor with rank at least 2. "
             "This input is optional and should have same shape with X. "
             "If provided, the out smooth l1 loss will be multiplied by this "
             "tensor element by element.")
70
        .AsDispensable();
71
    AddOutput("Diff", "Intermediate variable to cache InsideWeight * (X - Y).")
72
        .AsIntermediate();
73 74 75
    AddOutput("Out",
              "(Tensor, default Tensor<float>) A tensor with rank be 2. "
              "The output smooth l1 loss with shape [batch_size, 1].");
Y
yangyaming 已提交
76 77 78
    AddAttr<AttrType>("sigma",
                      "Hyper parameter of smooth l1 loss op."
                      "A float scalar with default value 3.0.")
Y
yangyaming 已提交
79
        .SetDefault(3.0);
80
    AddComment(R"DOC(
81 82
Smooth L1 Loss Operator.

83 84
This operator computes the smooth l1 loss for X and Y.
The operator takes the first dimension of X and Y as batch size.
85
For each instance, it computes the smooth l1 loss element by element first
86
and then sums all the losses. So the shape of Out is [batch_size, 1].
87

Y
yangyaming 已提交
88
The equation is:
89 90 91 92 93 94 95 96 97 98 99
$$
Out_{\sigma}(X, Y)_i = \begin{cases}
0.5 * (\sigma * (X_i - Y_i)) ^ 2
\quad |X_i - Y_i| \lt \frac{1} {{\sigma} ^ 2} \\
\frac{|X_i - Y_i| - 0.5}{{\sigma}^2},
\quad otherwise
\end{cases}
$$

In the above equation, $Out_{\sigma}(X, Y)_i$, $X_i$ and $Y_i$ represent the ith
element of Out, X and Y.
100

101 102 103 104 105 106 107 108
)DOC");
  }
};

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

109
  void InferShape(framework::InferShapeContext* ctx) const override {
110 111
    auto in_dims = ctx->GetInputDim("X");
    auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
112

113
    PADDLE_ENFORCE_GE(out_dims.size(), 2,
Y
yangyaming 已提交
114
                      "The tensor rank of Input(Out@Grad) should be 2.");
115
    PADDLE_ENFORCE_EQ(out_dims[0], in_dims[0],
Y
yangyaming 已提交
116 117
                      "The 1st dimension of Input(Out@Grad) must be "
                      "same as input.");
118
    PADDLE_ENFORCE_EQ(out_dims[1], 1,
Y
yangyaming 已提交
119
                      "The 2nd dimension of Input(Out@Grad) must be 1.");
120

121 122 123 124 125 126 127 128
    auto x_grad_name = framework::GradVarName("X");
    auto y_grad_name = framework::GradVarName("Y");
    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, in_dims);
    }
    if (ctx->HasOutput(y_grad_name)) {
      ctx->SetOutputDim(y_grad_name, in_dims);
    }
129 130 131 132 133 134 135 136
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(smooth_l1_loss, ops::SmoothL1LossOp,
Y
yangyaming 已提交
137 138
            ops::SmoothL1LossOpMaker<float>, smooth_l1_loss_grad,
            ops::SmoothL1LossGradOp);
139
REGISTER_OP_CPU_KERNEL(
Q
QI JUN 已提交
140 141
    smooth_l1_loss,
    ops::SmoothL1LossKernel<paddle::platform::CPUDeviceContext, float>);
142 143
REGISTER_OP_CPU_KERNEL(
    smooth_l1_loss_grad,
Q
QI JUN 已提交
144
    ops::SmoothL1LossGradKernel<paddle::platform::CPUDeviceContext, float>);
反馈
建议
客服 返回
顶部