|
| 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/log_loss_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class LogLossOp : public framework::OperatorWithKernel { |
| 21 | + public: |
| 22 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 23 | + |
| 24 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 25 | + PADDLE_ENFORCE(ctx->HasInput("Predicted"), |
| 26 | + "Input(Predicted) must be initialized."); |
| 27 | + PADDLE_ENFORCE(ctx->HasInput("Labels"), |
| 28 | + "Input(Labels) must be initialized."); |
| 29 | + |
| 30 | + auto pred_dims = ctx->GetInputDim("Predicted"); |
| 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(Predicted) must be 2 and the shape is " |
| 36 | + "[batch_size, 1]."); |
| 37 | + PADDLE_ENFORCE_EQ(pred_dims[1], 1, |
| 38 | + "Each row of Input(Predicted) contains a real value, " |
| 39 | + "so the 2nd dimension of Input(X) must be 1."); |
| 40 | + |
| 41 | + ctx->SetOutputDim("Loss", {pred_dims[0], 1}); |
| 42 | + ctx->ShareLoD("Predicted", "Loss"); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +template <typename AttrType> |
| 47 | +class LogLossOpMaker : public framework::OpProtoAndCheckerMaker { |
| 48 | + public: |
| 49 | + LogLossOpMaker(framework::OpProto* proto, |
| 50 | + framework::OpAttrChecker* op_checker) |
| 51 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 52 | + AddInput("Predicted", |
| 53 | + "The input value (Predicted) of Log loss op." |
| 54 | + "Predicted is a 2-D tensor with shape [batch_size, 1]."); |
| 55 | + AddInput("Labels", |
| 56 | + "The target value (Labels) of Log 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 log loss."); |
| 61 | + AddAttr<AttrType>("epsilon", "Epsilon in log loss."); |
| 62 | + AddComment(R"DOC( |
| 63 | +LogLoss Operator. |
| 64 | +
|
| 65 | +Log loss is a loss function used for binary classification. Log Loss quantifies |
| 66 | +the accuracy of a classifier by penalising false classifications. Minimising the |
| 67 | +Log Loss is equivalent to maximising the accuracy of the classifier. We define |
| 68 | +Predicted as the values predicted by our model and Labels as the target ground |
| 69 | +truth value. Log loss can evaluate how close the predicted values are to the |
| 70 | +target. The shapes of Predicted and Labels are both [batch_size, 1]. |
| 71 | +The equation is: |
| 72 | +
|
| 73 | +$$ |
| 74 | +Loss = - Labels * log(Predicted + \epsilon) - |
| 75 | + (1 - Labels) * log(1 - Predicted + \epsilon) |
| 76 | +$$ |
| 77 | +
|
| 78 | +)DOC"); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +class LogLossGradOp : public framework::OperatorWithKernel { |
| 83 | + public: |
| 84 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 85 | + |
| 86 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 87 | + PADDLE_ENFORCE(ctx->HasInput("Predicted"), |
| 88 | + "Input(Predicted) should not be null."); |
| 89 | + PADDLE_ENFORCE(ctx->HasInput("Labels"), |
| 90 | + "Input(Labels) should not be null."); |
| 91 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Loss")), |
| 92 | + "Input(Loss@GRAD) should not be null."); |
| 93 | + PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("Predicted")), |
| 94 | + "Output(Predicted@GRAD) should not be null."); |
| 95 | + |
| 96 | + auto pred_dims = ctx->GetInputDim("Predicted"); |
| 97 | + auto label_dims = ctx->GetInputDim("Labels"); |
| 98 | + auto loss_grad_dims = ctx->GetInputDim(framework::GradVarName("Loss")); |
| 99 | + PADDLE_ENFORCE_EQ(loss_grad_dims, pred_dims); |
| 100 | + |
| 101 | + auto pred_grad_name = framework::GradVarName("Predicted"); |
| 102 | + ctx->SetOutputDim(pred_grad_name, pred_dims); |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +} // namespace operators |
| 107 | +} // namespace paddle |
| 108 | + |
| 109 | +namespace ops = paddle::operators; |
| 110 | +REGISTER_OP(log_loss, ops::LogLossOp, ops::LogLossOpMaker<float>, log_loss_grad, |
| 111 | + ops::LogLossGradOp); |
| 112 | +REGISTER_OP_CPU_KERNEL(log_loss, |
| 113 | + ops::LogLossKernel<paddle::platform::CPUPlace, float>); |
| 114 | +REGISTER_OP_CPU_KERNEL( |
| 115 | + log_loss_grad, ops::LogLossGradKernel<paddle::platform::CPUPlace, float>); |
0 commit comments