Skip to content

Commit 1f8243b

Browse files
authored
Refine smooth L1 loss. (#10713)
1 parent 7792c63 commit 1f8243b

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

paddle/fluid/operators/smooth_l1_loss_op.cc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class SmoothL1LossGradOp : public framework::OperatorWithKernel {
105105
using framework::OperatorWithKernel::OperatorWithKernel;
106106

107107
void InferShape(framework::InferShapeContext* ctx) const override {
108-
auto in_dims = ctx->GetInputDim("X");
108+
auto in_dims = ctx->GetInputDim("Diff");
109109
auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
110110

111111
PADDLE_ENFORCE_GE(out_dims.size(), 2,
@@ -127,12 +127,33 @@ class SmoothL1LossGradOp : public framework::OperatorWithKernel {
127127
}
128128
};
129129

130+
class SmoothL1LossGradMaker : public framework::SingleGradOpDescMaker {
131+
public:
132+
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
133+
134+
protected:
135+
std::unique_ptr<framework::OpDesc> Apply() const override {
136+
auto* op = new framework::OpDesc();
137+
op->SetType("smooth_l1_loss_grad");
138+
op->SetInput("InsideWeight", Input("InsideWeight"));
139+
op->SetInput("OutsideWeight", Input("OutsideWeight"));
140+
op->SetInput("Diff", Output("Diff"));
141+
op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
142+
143+
op->SetAttrMap(Attrs());
144+
145+
op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
146+
op->SetOutput(framework::GradVarName("Y"), InputGrad("Y"));
147+
return std::unique_ptr<framework::OpDesc>(op);
148+
}
149+
};
150+
130151
} // namespace operators
131152
} // namespace paddle
132153

133154
namespace ops = paddle::operators;
134155
REGISTER_OPERATOR(smooth_l1_loss, ops::SmoothL1LossOp, ops::SmoothL1LossOpMaker,
135-
paddle::framework::DefaultGradOpDescMaker<true>);
156+
ops::SmoothL1LossGradMaker);
136157
REGISTER_OPERATOR(smooth_l1_loss_grad, ops::SmoothL1LossGradOp);
137158
REGISTER_OP_CPU_KERNEL(
138159
smooth_l1_loss,

python/paddle/fluid/layers/nn.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3263,35 +3263,35 @@ def smooth_l1(x, y, inside_weight=None, outside_weight=None, sigma=None):
32633263
"""
32643264
**Smooth L1 Loss Operator. **
32653265
3266-
This operator computes the smooth l1 loss for X and Y.
3266+
This operator computes the smooth L1 loss for X and Y.
32673267
The operator takes the first dimension of X and Y as batch size.
3268-
For each instance, it computes the smooth l1 loss element by element first
3268+
For each instance, it computes the smooth L1 loss element by element first
32693269
and then sums all the losses. So the shape of Out is [batch_size, 1].
32703270
32713271
Args:
32723272
x (Variable): A tensor with rank at least 2. The input value of smooth
3273-
l1 loss op with shape [batch_size, dim1, ..., dimN].
3273+
L1 loss op with shape [batch_size, dim1, ..., dimN].
32743274
y (Variable): A tensor with rank at least 2. The target value of smooth
3275-
l1 loss op with same shape as x.
3275+
L1 loss op with same shape as x.
32763276
inside_weight (Variable|None): A tensor with rank at least 2. This
32773277
input is optional and should have same shape with x. If provided,
32783278
the result of (x - y) will be multiplied by this tensor element by
32793279
element.
32803280
outside_weight (Variable|None): A tensor with rank at least 2. This
32813281
input is optional and should have same shape with x. If provided,
3282-
the out smooth l1 loss will be multiplied by this tensor element
3282+
the out smooth L1 loss will be multiplied by this tensor element
32833283
by element.
3284-
sigma (float|None): Hyper parameter of smooth l1 loss op. A float scalar
3284+
sigma (float|None): Hyper parameter of smooth L1 loss op. A float scalar
32853285
with default value 1.0.
32863286
Returns:
3287-
Variable: A tensor with rank be 2. The output smooth l1 loss with
3287+
Variable: A tensor with rank be 2. The output smooth L1 loss with
32883288
shape [batch_size, 1].
32893289
32903290
Examples:
32913291
.. code-block:: python
32923292
32933293
data = fluid.layers.data(name='data', shape=[128], dtype='float32')
3294-
label = fluid.layers.data(name='label', shape=[100], dtype='int64')
3294+
label = fluid.layers.data(name='label', shape=[100], dtype='float32')
32953295
fc = fluid.layers.fc(input=data, size=100)
32963296
out = fluid.layers.smooth_l1(x=fc, y=label)
32973297
"""

0 commit comments

Comments
 (0)