-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Adding log loss operator #5854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Adding log loss operator #5854
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| 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. */ | ||
|
|
||
| #include "paddle/operators/log_loss_op.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| class LogLossOp : public framework::OperatorWithKernel { | ||
| public: | ||
| using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
|
||
| void InferShape(framework::InferShapeContext* ctx) const override { | ||
| PADDLE_ENFORCE(ctx->HasInput("Predicted"), | ||
| "Input(Predicted) must be initialized."); | ||
| PADDLE_ENFORCE(ctx->HasInput("Labels"), | ||
| "Input(Labels) must be initialized."); | ||
|
|
||
| auto pred_dims = ctx->GetInputDim("Predicted"); | ||
| auto label_dims = ctx->GetInputDim("Labels"); | ||
|
|
||
| PADDLE_ENFORCE_EQ(pred_dims, label_dims); | ||
| PADDLE_ENFORCE_EQ(pred_dims.size(), 2, | ||
| "The rank of Input(Predicted) must be 2 and the shape is " | ||
| "[batch_size, 1]."); | ||
| PADDLE_ENFORCE_EQ(pred_dims[1], 1, | ||
| "Each row of Input(Predicted) contains a real value, " | ||
| "so the 2nd dimension of Input(X) must be 1."); | ||
|
|
||
| // ctx->SetOutputDim("Residual", x_dims); | ||
| ctx->SetOutputDim("Loss", {pred_dims[0], 1}); | ||
| ctx->ShareLoD("Predicted", "Loss"); | ||
| } | ||
| }; | ||
|
|
||
| template <typename AttrType> | ||
| class LogLossOpMaker : public framework::OpProtoAndCheckerMaker { | ||
| public: | ||
| LogLossOpMaker(framework::OpProto* proto, | ||
| framework::OpAttrChecker* op_checker) | ||
| : OpProtoAndCheckerMaker(proto, op_checker) { | ||
| AddInput("Predicted", | ||
| "The input value (Predicted) of Log loss op." | ||
| "Predicted is a 2-D tensor with shape [batch_size, 1]."); | ||
| AddInput("Labels", | ||
| "The target value (Labels) of Log loss op." | ||
| "Labels is a 2-D tensor with shape [batch_size, 1]."); | ||
| // AddOutput("Residual", | ||
| // "Intermediate tensor to cache residual value between Y and X." | ||
| // "The shape is same as Input(X) and will be reused in | ||
| // backward.") | ||
| // .AsIntermediate(); | ||
| AddOutput("Loss", | ||
| "The output tensor with shape [batch_size, 1] " | ||
| "which represents the log loss."); | ||
| AddAttr<AttrType>("epsilon", "Epsilon in log loss."); | ||
| AddComment(R"DOC( | ||
| LogLoss Operator. | ||
|
|
||
| Log loss is a loss function used for binary classification. Log Loss quantifies | ||
| the accuracy of a classifier by penalising false classifications. Minimising the | ||
| Log Loss is equivalent to maximising the accuracy of the classifier. We define | ||
| Predicted as the values predicted by our model and Labels as the target ground | ||
| truth value. Log loss can evaluate how close the predicted values are to the | ||
| target. The shapes of Predicted and Labels are both [batch_size, 1]. | ||
| The equation is: | ||
|
|
||
| $$ | ||
| Loss = - Labels * log(Predicted + \epsilon) - | ||
| (1 - Labels) * log(1 - Predicted + \epsilon) | ||
| $$ | ||
|
|
||
| )DOC"); | ||
| } | ||
| }; | ||
|
|
||
| class LogLossGradOp : public framework::OperatorWithKernel { | ||
| public: | ||
| using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
|
||
| void InferShape(framework::InferShapeContext* ctx) const override { | ||
| PADDLE_ENFORCE(ctx->HasInput("Predicted"), | ||
| "Input(Predicted) should not be null."); | ||
| PADDLE_ENFORCE(ctx->HasInput("Labels"), | ||
| "Input(Labels) should not be null."); | ||
| // PADDLE_ENFORCE(ctx->HasInput("Residual"), | ||
| // "Input(Residual) should not be null."); | ||
| PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Loss")), | ||
| "Input(Loss@GRAD) should not be null."); | ||
| PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("Predicted")), | ||
| "Output(Predicted@GRAD) should not be null."); | ||
|
|
||
| auto pred_dims = ctx->GetInputDim("Predicted"); | ||
| auto label_dims = ctx->GetInputDim("Labels"); | ||
| auto loss_grad_dims = ctx->GetInputDim(framework::GradVarName("Loss")); | ||
| // auto residual_dims = ctx->GetInputDim("Residual"); | ||
|
|
||
| // PADDLE_ENFORCE_EQ(residual_dims, x_dims); | ||
| PADDLE_ENFORCE_EQ(loss_grad_dims, pred_dims); | ||
|
|
||
| auto pred_grad_name = framework::GradVarName("Predicted"); | ||
| ctx->SetOutputDim(pred_grad_name, pred_dims); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
| REGISTER_OP(log_loss, ops::LogLossOp, ops::LogLossOpMaker<float>, log_loss_grad, | ||
| ops::LogLossGradOp); | ||
| REGISTER_OP_CPU_KERNEL(log_loss, | ||
| ops::LogLossKernel<paddle::platform::CPUPlace, float>); | ||
| REGISTER_OP_CPU_KERNEL( | ||
| log_loss_grad, ops::LogLossGradKernel<paddle::platform::CPUPlace, float>); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| 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. */ | ||
|
|
||
| #define EIGEN_USE_GPU | ||
| #include "paddle/operators/log_loss_op.h" | ||
|
|
||
| namespace ops = paddle::operators; | ||
| REGISTER_OP_GPU_KERNEL(log_loss, | ||
| ops::LogLossKernel<paddle::platform::GPUPlace, float>); | ||
| REGISTER_OP_GPU_KERNEL( | ||
| log_loss_grad, ops::LogLossGradKernel<paddle::platform::GPUPlace, float>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| 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. */ | ||
|
|
||
| #pragma once | ||
| #include "paddle/framework/eigen.h" | ||
| #include "paddle/framework/op_registry.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| using Tensor = framework::Tensor; | ||
| template <typename T, int MajorType = Eigen::RowMajor, | ||
| typename IndexType = Eigen::DenseIndex> | ||
| using EigenVector = framework::EigenVector<T, MajorType, IndexType>; | ||
|
|
||
| template <typename Place, typename T, typename AttrType = T> | ||
| class LogLossKernel : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& ctx) const override { | ||
| auto* loss_out = ctx.Output<Tensor>("Loss"); | ||
|
|
||
| loss_out->mutable_data<T>(ctx.GetPlace()); | ||
|
|
||
| auto epsilon = static_cast<T>(ctx.Attr<AttrType>("epsilon")); | ||
|
|
||
| auto prediction = EigenVector<T>::Flatten(*ctx.Input<Tensor>("Predicted")); | ||
| auto label = EigenVector<T>::Flatten(*ctx.Input<Tensor>("Labels")); | ||
|
|
||
| auto loss = EigenVector<T>::Flatten(*loss_out); | ||
| auto place = ctx.GetEigenDevice<Place>(); | ||
|
|
||
| loss.device(place) = (-(label * (prediction + epsilon).log()) - | ||
| ((static_cast<T>(1) - label) * | ||
| (static_cast<T>(1) - prediction + epsilon).log())); | ||
| } | ||
| }; | ||
|
|
||
| template <typename Place, typename T, typename AttrType = T> | ||
| class LogLossGradKernel : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& ctx) const override { | ||
| auto epsilon = static_cast<T>(ctx.Attr<AttrType>("epsilon")); | ||
|
|
||
| auto prediction = EigenVector<T>::Flatten(*ctx.Input<Tensor>("Predicted")); | ||
| auto label = EigenVector<T>::Flatten(*ctx.Input<Tensor>("Labels")); | ||
|
|
||
| auto* dloss = ctx.Input<Tensor>(framework::GradVarName("Loss")); | ||
| auto* dpred = ctx.Output<Tensor>(framework::GradVarName("Predicted")); | ||
|
|
||
| auto dl = EigenVector<T>::Flatten(*dloss); | ||
| auto place = ctx.GetEigenDevice<Place>(); | ||
|
|
||
| if (dpred) { | ||
| dpred->mutable_data<T>(ctx.GetPlace()); | ||
| auto dx = framework::EigenVector<T>::Flatten(*dpred); | ||
| dx.device(place) = dl * (-(label / (prediction + epsilon)) + | ||
| ((static_cast<T>(1) - label) / | ||
| (static_cast<T>(1) - prediction + epsilon))); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import unittest | ||
| import numpy as np | ||
| from op_test import OpTest | ||
|
|
||
|
|
||
| class TestLogLossOp(OpTest): | ||
| def setUp(self): | ||
| self.op_type = 'log_loss' | ||
| samples_num = 32 | ||
|
|
||
| predicted = np.random.uniform(0.1, 1.0, | ||
| (samples_num, 1)).astype("float32") | ||
| labels = np.random.randint(0, 2, (samples_num, 1)).astype("float32") | ||
| epsilon = 1e-4 | ||
| self.inputs = { | ||
| 'Predicted': predicted, | ||
| 'Labels': labels, | ||
| } | ||
|
|
||
| self.attrs = {'epsilon': epsilon} | ||
| loss = -labels * np.log(predicted + epsilon) - ( | ||
| 1 - labels) * np.log(1 - predicted + epsilon) | ||
| self.outputs = {'Loss': loss} | ||
|
|
||
| def test_check_output(self): | ||
| self.check_output() | ||
|
|
||
| def test_check_grad(self): | ||
| self.check_grad(['Predicted'], 'Loss', max_relative_error=0.03) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this commented out lines. :)