cross_entropy_op.cc 7.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
Qiao Longfei 已提交
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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/cross_entropy_op.h"
Q
Qiao Longfei 已提交
16 17 18 19

namespace paddle {
namespace operators {

20
class CrossEntropyOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
21 22 23
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

24
  void InferShape(framework::InferShapeContext* ctx) const override {
Q
Qiao Longfei 已提交
25 26 27
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
    PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
    PADDLE_ENFORCE(ctx->HasOutput("Y"), "Output(Y) should be not null.");
28

Q
Qiao Longfei 已提交
29 30
    auto x_dims = ctx->GetInputDim("X");
    auto label_dims = ctx->GetInputDim("Label");
C
caoying03 已提交
31 32 33
    PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "Input(X)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(label_dims.size(), 2UL,
                      "Input(Label)'s rank should be 2.");
Q
Qiao Longfei 已提交
34
    PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0],
C
caoying03 已提交
35
                      "The 1st dimension of Input(X) and Input(Label) should "
36
                      "be equal.");
37
    if (ctx->Attrs().Get<bool>("soft_label")) {
Q
Qiao Longfei 已提交
38
      PADDLE_ENFORCE_EQ(x_dims[1], label_dims[1],
39
                        "If Attr(soft_label) == true, the 2nd dimension of "
C
caoying03 已提交
40
                        "Input(X) and Input(Label) should be equal.");
41
    } else {
C
caoying03 已提交
42
      PADDLE_ENFORCE_EQ(label_dims[1], 1UL,
C
caoying03 已提交
43
                        "If Attr(softLabel) == false, the 2nd dimension of "
C
caoying03 已提交
44
                        "Input(Label) should be 1.");
45
    }
46

Q
Qiao Longfei 已提交
47 48
    ctx->SetOutputDim("Y", {x_dims[0], 1});
    ctx->ShareLoD("X", /*->*/ "Y");
Q
Qiao Longfei 已提交
49
  }
Y
Yu Yang 已提交
50

51
 protected:
C
Cao Ying 已提交
52
  // Explicitly set that the data type of computation kernel of cross_entropy
C
caoying03 已提交
53
  // is determined by its input "X".
54
  framework::OpKernelType GetExpectedKernelType(
Y
Yu Yang 已提交
55
      const framework::ExecutionContext& ctx) const override {
Y
Yu Yang 已提交
56 57 58
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("X")->type()),
        ctx.device_context());
Y
Yu Yang 已提交
59
  }
Q
Qiao Longfei 已提交
60 61
};

62
class CrossEntropyGradientOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
63 64 65
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

66
  void InferShape(framework::InferShapeContext* ctx) const override {
Q
Qiao Longfei 已提交
67 68 69 70 71 72
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
    PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Y")),
                   "Input(Y@GRAD) shoudl be not null.");
    PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
                   "Output(X@GRAD) should be not null.");
73

Q
Qiao Longfei 已提交
74 75 76 77 78 79 80
    auto x_dims = ctx->GetInputDim("X");
    auto label_dims = ctx->GetInputDim("Label");
    auto dy_dims = ctx->GetInputDim(framework::GradVarName("Y"));
    PADDLE_ENFORCE_EQ(x_dims.size(), 2, "Input(X)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(dy_dims.size(), 2, "Input(Y@Grad)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(label_dims.size(), 2, "Input(Label)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0],
C
caoying03 已提交
81
                      "The 1st dimension of Input(X) and Input(Label) should "
82
                      "be equal.");
Q
Qiao Longfei 已提交
83
    PADDLE_ENFORCE_EQ(x_dims[0], dy_dims[0],
C
caoying03 已提交
84
                      "The 1st dimension of Input(X) and Input(Y@Grad) should "
85
                      "be equal.");
Q
Qiao Longfei 已提交
86
    PADDLE_ENFORCE_EQ(dy_dims[1], 1,
C
caoying03 已提交
87
                      "The 2nd dimension of Input(Y@Grad) should be 1.");
88
    if (ctx->Attrs().Get<bool>("soft_label")) {
Q
Qiao Longfei 已提交
89
      PADDLE_ENFORCE_EQ(x_dims[1], label_dims[1],
90
                        "When Attr(soft_label) == true, the 2nd dimension of "
C
caoying03 已提交
91
                        "Input(X) and Input(Label) should be equal.");
92
    } else {
Q
Qiao Longfei 已提交
93
      PADDLE_ENFORCE_EQ(label_dims[1], 1,
94
                        "When Attr(soft_label) == false, the 2nd dimension of "
C
caoying03 已提交
95
                        "Input(Label) should be 1.");
96
    }
Q
Qiao Longfei 已提交
97
    ctx->SetOutputDim(framework::GradVarName("X"), x_dims);
Q
Qiao Longfei 已提交
98
    ctx->ShareLoD("X", framework::GradVarName("X"));
Y
Yan Chunwei 已提交
99
  }
Y
Yu Yang 已提交
100

101
 protected:
C
Cao Ying 已提交
102 103
  // Explicitly set that the data type of computation kernel of cross_entropy
  // is determined by its input "X".
104
  framework::OpKernelType GetExpectedKernelType(
Y
Yu Yang 已提交
105
      const framework::ExecutionContext& ctx) const override {
Y
Yu Yang 已提交
106 107 108
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("X")->type()),
        ctx.device_context());
Y
Yu Yang 已提交
109
  }
Y
Yan Chunwei 已提交
110 111
};

112
class CrossEntropyOpMaker : public framework::OpProtoAndCheckerMaker {
113
 public:
Y
Yu Yang 已提交
114
  void Make() override {
C
caoying03 已提交
115
    AddInput("X",
Y
Yibing Liu 已提交
116 117
             "(Tensor, default Tensor<float>), a 2-D tensor with shape [N x D],"
             " where N is the batch size and D is the number of classes. "
C
caoying03 已提交
118 119
             "This input is a probability computed by the previous operator, "
             "which is almost always the result of a softmax operator.");
C
caoying03 已提交
120 121 122 123
    AddInput("Label",
             "(Tensor), the ground truth which is a 2-D tensor. When "
             "soft_label is set to false, Label is a Tensor<int64> with shape "
             "[N x 1]. When soft_label is set to true, Label is a "
Y
Yibing Liu 已提交
124
             "Tensor<float/double> with shape [N x D].");
C
caoying03 已提交
125
    AddOutput("Y",
C
caoying03 已提交
126
              "(Tensor, default Tensor<float>), a 2-D tensor with shape "
127
              "[N x 1]. The cross entropy loss.");
C
caoying03 已提交
128 129 130
    AddAttr<bool>("soft_label",
                  "(bool, default false), a flag indicating whether to "
                  "interpretate the given labels as soft labels.")
131
        .SetDefault(false);
Q
Qiao Longfei 已提交
132
    AddComment(R"DOC(
133
CrossEntropy Operator.
Q
Qiao Longfei 已提交
134

135 136 137
It supports both standard cross-entropy and soft-label cross-entropy loss
computation.
1) One-hot cross-entropy:
138
    soft_label = false, Label[i, 0] indicates the class index for sample i:
139

K
Kexin Zhao 已提交
140
                $Y[i] = -\log(X[i, Label[i]])$
Q
Qiao Longfei 已提交
141

142
2) Soft-label cross-entropy:
143
    soft_label = true, Label[i, j] indicates the soft label of class j
144
    for sample i:
145

K
Kexin Zhao 已提交
146
                $Y[i] = \sum_j{-Label[i, j] * log(X[i, j])}$
147

148
   Please make sure that in this case the summuation of each row of Label
149 150 151 152 153 154
   equals one.

3) One-hot cross-entropy with vecterized Input(Label):
     As a special case of 2), when each row of Input(Label) has only one
     non-zero element (equals 1), soft-label cross-entropy degenerates to a
     one-hot cross-entropy with one-hot label representation.
D
dangqingqing 已提交
155

K
Kexin Zhao 已提交
156 157 158
Both the input X and Label can carry the LoD (Level of Details) information,
or not. But the output only shares the LoD information with input X.

Q
Qiao Longfei 已提交
159 160 161 162 163 164
)DOC");
  }
};
}  // namespace operators
}  // namespace paddle

D
dongzhihong 已提交
165
namespace ops = paddle::operators;
166 167
using CPUCtx = paddle::platform::CPUDeviceContext;

Y
Yang Yang 已提交
168
REGISTER_OPERATOR(cross_entropy, ops::CrossEntropyOp, ops::CrossEntropyOpMaker,
169 170
                  paddle::framework::DefaultGradOpDescMaker<true>);
REGISTER_OPERATOR(cross_entropy_grad, ops::CrossEntropyGradientOp);
171 172
REGISTER_OP_CPU_KERNEL(cross_entropy, ops::CrossEntropyOpKernel<CPUCtx, float>,
                       ops::CrossEntropyOpKernel<CPUCtx, double>);
173
REGISTER_OP_CPU_KERNEL(cross_entropy_grad,
174 175
                       ops::CrossEntropyGradientOpKernel<CPUCtx, float>,
                       ops::CrossEntropyGradientOpKernel<CPUCtx, double>);