Skip to content

Commit ea1a643

Browse files
authored
Add hinge loss op (#5837)
* Add hinge loss op * Update hinge-loss equation for proper latex
1 parent d89061c commit ea1a643

File tree

4 files changed

+233
-0
lines changed

4 files changed

+233
-0
lines changed

paddle/operators/hinge_loss_op.cc

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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/hinge_loss_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
class HingeLossOp : public framework::OperatorWithKernel {
21+
public:
22+
using framework::OperatorWithKernel::OperatorWithKernel;
23+
24+
void InferShape(framework::InferShapeContext* ctx) const override {
25+
PADDLE_ENFORCE(ctx->HasInput("Logits"),
26+
"Input(Logits) must be initialized.");
27+
PADDLE_ENFORCE(ctx->HasInput("Labels"),
28+
"Input(Labels) must be initialized.");
29+
30+
auto pred_dims = ctx->GetInputDim("Logits");
31+
auto label_dims = ctx->GetInputDim("Labels");
32+
33+
PADDLE_ENFORCE_EQ(pred_dims, label_dims);
34+
PADDLE_ENFORCE_EQ(pred_dims.size(), 2,
35+
"The rank of Input(Logits) must be 2 and the shape is "
36+
"[batch_size, 1].");
37+
PADDLE_ENFORCE_EQ(pred_dims[1], 1,
38+
"Each row of Input(Logits) contains a real value, "
39+
"so the 2nd dimension of Input(Logits) must be 1.");
40+
41+
ctx->SetOutputDim("Loss", {pred_dims[0], 1});
42+
ctx->ShareLoD("Logits", "Loss");
43+
}
44+
};
45+
46+
template <typename AttrType>
47+
class HingeLossOpMaker : public framework::OpProtoAndCheckerMaker {
48+
public:
49+
HingeLossOpMaker(framework::OpProto* proto,
50+
framework::OpAttrChecker* op_checker)
51+
: OpProtoAndCheckerMaker(proto, op_checker) {
52+
AddInput("Logits",
53+
"The input value (Logits) of Hinge loss op."
54+
"Logits is a 2-D tensor with shape [batch_size, 1].");
55+
AddInput("Labels",
56+
"The target value (Labels) of Hinge loss op."
57+
"Labels is a 2-D tensor with shape [batch_size, 1].");
58+
AddOutput("Loss",
59+
"The output tensor with shape [batch_size, 1] "
60+
"which represents the hinge loss.");
61+
AddComment(R"DOC(
62+
HingeLoss Operator.
63+
64+
Let x be a logit (prediction) and y be the actual label. The logit can
65+
take any values from (-inf, inf), but the labels should be either -1 or 1.
66+
Then, the hinge loss is computed as follows:
67+
68+
$$
69+
L_(x, y) = max(1 - y.x, 0)
70+
$$
71+
72+
Note that the labels passed as input will have values as either 0 or 1.
73+
74+
)DOC");
75+
}
76+
};
77+
78+
class HingeLossGradOp : public framework::OperatorWithKernel {
79+
public:
80+
using framework::OperatorWithKernel::OperatorWithKernel;
81+
82+
void InferShape(framework::InferShapeContext* ctx) const override {
83+
PADDLE_ENFORCE(ctx->HasInput("Logits"),
84+
"Input(Logits) should not be null.");
85+
PADDLE_ENFORCE(ctx->HasInput("Labels"),
86+
"Input(Labels) should not be null.");
87+
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Loss")),
88+
"Input(Loss@GRAD) should not be null.");
89+
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("Logits")),
90+
"Input(Logits@GRAD) should not be null.");
91+
92+
auto pred_dims = ctx->GetInputDim("Logits");
93+
auto lab_dims = ctx->GetInputDim("Labels");
94+
auto loss_grad_dims = ctx->GetInputDim(framework::GradVarName("Loss"));
95+
96+
PADDLE_ENFORCE_EQ(loss_grad_dims, pred_dims);
97+
98+
auto pred_grad_name = framework::GradVarName("Logits");
99+
ctx->SetOutputDim(pred_grad_name, pred_dims);
100+
}
101+
};
102+
103+
} // namespace operators
104+
} // namespace paddle
105+
106+
namespace ops = paddle::operators;
107+
REGISTER_OP(hinge_loss, ops::HingeLossOp, ops::HingeLossOpMaker<float>,
108+
hinge_loss_grad, ops::HingeLossGradOp);
109+
REGISTER_OP_CPU_KERNEL(hinge_loss,
110+
ops::HingeLossKernel<paddle::platform::CPUPlace, float>);
111+
REGISTER_OP_CPU_KERNEL(
112+
hinge_loss_grad,
113+
ops::HingeLossGradKernel<paddle::platform::CPUPlace, float>);

paddle/operators/hinge_loss_op.cu

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#define EIGEN_USE_GPU
16+
#include "paddle/operators/hinge_loss_op.h"
17+
18+
namespace ops = paddle::operators;
19+
REGISTER_OP_GPU_KERNEL(hinge_loss,
20+
ops::HingeLossKernel<paddle::platform::GPUPlace, float>);
21+
REGISTER_OP_GPU_KERNEL(
22+
hinge_loss_grad,
23+
ops::HingeLossGradKernel<paddle::platform::GPUPlace, float>);

paddle/operators/hinge_loss_op.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
#pragma once
16+
#include "paddle/framework/eigen.h"
17+
#include "paddle/framework/op_registry.h"
18+
19+
namespace paddle {
20+
namespace operators {
21+
22+
template <typename Place, typename T, typename AttrType = T>
23+
class HingeLossKernel : public framework::OpKernel<T> {
24+
public:
25+
void Compute(const framework::ExecutionContext& context) const override {
26+
auto* pred = context.Input<framework::Tensor>("Logits");
27+
auto* label = context.Input<framework::Tensor>("Labels");
28+
auto* loss = context.Output<framework::Tensor>("Loss");
29+
auto place = context.GetEigenDevice<Place>();
30+
31+
auto x = framework::EigenVector<T>::Flatten(*pred);
32+
auto y = framework::EigenVector<T>::Flatten(*label);
33+
loss->mutable_data<T>(context.GetPlace());
34+
auto l = framework::EigenVector<T>::Flatten(*loss);
35+
l.device(place) =
36+
(static_cast<T>(1) - x * (static_cast<T>(2) * y - static_cast<T>(1)))
37+
.cwiseMax(static_cast<T>(0));
38+
}
39+
};
40+
41+
template <typename Place, typename T, typename AttrType = T>
42+
class HingeLossGradKernel : public framework::OpKernel<T> {
43+
public:
44+
void Compute(const framework::ExecutionContext& context) const override {
45+
auto* pred = context.Input<framework::Tensor>("Logits");
46+
auto* label = context.Input<framework::Tensor>("Labels");
47+
auto* dloss =
48+
context.Input<framework::Tensor>(framework::GradVarName("Loss"));
49+
auto* dpred =
50+
context.Output<framework::Tensor>(framework::GradVarName("Logits"));
51+
auto place = context.GetEigenDevice<Place>();
52+
53+
auto x = framework::EigenVector<T>::Flatten(*pred);
54+
auto y = framework::EigenVector<T>::Flatten(*label);
55+
auto dl = framework::EigenVector<T>::Flatten(*dloss);
56+
57+
if (dpred) {
58+
dpred->mutable_data<T>(context.GetPlace());
59+
auto dx = framework::EigenVector<T>::Flatten(*dpred);
60+
auto alt_labels = static_cast<T>(2) * y - static_cast<T>(1);
61+
dx.device(place) =
62+
dl * ((x * alt_labels) < static_cast<T>(1)).template cast<T>() *
63+
(-alt_labels);
64+
}
65+
}
66+
};
67+
68+
} // namespace operators
69+
} // namespace paddle
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
import numpy as np
3+
from op_test import OpTest
4+
5+
6+
class TestHingeLossOp(OpTest):
7+
def setUp(self):
8+
self.op_type = 'hinge_loss'
9+
samples_num = 64
10+
logits = np.random.uniform(-10, 10, (samples_num, 1)).astype('float32')
11+
labels = np.random.randint(0, 2, (samples_num, 1)).astype('float32')
12+
13+
self.inputs = {
14+
'Logits': logits,
15+
'Labels': labels,
16+
}
17+
loss = np.maximum(1.0 - (2 * labels - 1) * logits, 0)
18+
self.outputs = {'Loss': loss}
19+
20+
def test_check_output(self):
21+
self.check_output()
22+
23+
def test_check_grad(self):
24+
self.check_grad(['Logits'], 'Loss', max_relative_error=0.008)
25+
26+
27+
if __name__ == '__main__':
28+
unittest.main()

0 commit comments

Comments
 (0)