log_loss_op.cc 4.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
K
kavyasrinet 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

S
sneaxiy 已提交
15
#include <memory>
16

17
#include "paddle/fluid/framework/infershape_utils.h"
18
#include "paddle/fluid/framework/op_registry.h"
19 20
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/binary.h"
K
kavyasrinet 已提交
21 22 23 24 25 26 27 28 29 30 31 32

namespace paddle {
namespace operators {

class LogLossOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

template <typename AttrType>
class LogLossOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
33
  void Make() override {
K
kavyasrinet 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    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("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 {
69 70 71 72 73 74 75 76
    OP_INOUT_CHECK(ctx->HasInput("Predicted"), "Input", "Predicted",
                   "LogLossGrad");
    OP_INOUT_CHECK(ctx->HasInput("Labels"), "Input", "Labels", "LogLossGrad");
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Loss")), "Input",
                   framework::GradVarName("Loss"), "LogLossGrad");
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("Predicted")),
                   "Output", framework::GradVarName("Predicted"),
                   "LogLossGrad");
K
kavyasrinet 已提交
77 78 79

    auto pred_dims = ctx->GetInputDim("Predicted");
    auto loss_grad_dims = ctx->GetInputDim(framework::GradVarName("Loss"));
80 81 82 83 84 85 86 87
    PADDLE_ENFORCE_EQ(loss_grad_dims, pred_dims,
                      platform::errors::InvalidArgument(
                          "The dimensions of loss_grad must be equal to the "
                          "dimensions of Predicted,"
                          "But received dimensions of loss_grad is [%s], "
                          "received Predicted is "
                          "[%s]",
                          loss_grad_dims, pred_dims));
K
kavyasrinet 已提交
88 89 90 91 92 93

    auto pred_grad_name = framework::GradVarName("Predicted");
    ctx->SetOutputDim(pred_grad_name, pred_dims);
  }
};

H
hong 已提交
94 95
template <typename T>
class LogLossGradMaker : public framework::SingleGradOpMaker<T> {
S
sneaxiy 已提交
96
 public:
H
hong 已提交
97
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
S
sneaxiy 已提交
98 99

 protected:
100
  void Apply(GradOpPtr<T> op) const override {
S
sneaxiy 已提交
101
    op->SetType("log_loss_grad");
H
hong 已提交
102 103 104 105 106 107
    op->SetInput("Predicted", this->Input("Predicted"));
    op->SetInput("Labels", this->Input("Labels"));
    op->SetInput(framework::GradVarName("Loss"), this->OutputGrad("Loss"));
    op->SetOutput(framework::GradVarName("Predicted"),
                  this->InputGrad("Predicted"));
    op->SetAttrMap(this->Attrs());
S
sneaxiy 已提交
108 109 110
  }
};

K
kavyasrinet 已提交
111 112 113 114
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
115 116
DECLARE_INFER_SHAPE_FUNCTOR(log_loss, LogLossInferShapeFunctor,
                            PD_INFER_META(phi::LogLossInferMeta));
Y
Yang Yang 已提交
117
REGISTER_OPERATOR(log_loss, ops::LogLossOp, ops::LogLossOpMaker<float>,
H
hong 已提交
118
                  ops::LogLossGradMaker<paddle::framework::OpDesc>,
119 120
                  ops::LogLossGradMaker<paddle::imperative::OpBase>,
                  LogLossInferShapeFunctor);
121
REGISTER_OPERATOR(log_loss_grad, ops::LogLossGradOp);