|
| 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/sigmoid_cross_entropy_with_logits_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +using framework::Tensor; |
| 21 | + |
| 22 | +class SigmoidCrossEntropyWithLogitsOp : public framework::OperatorWithKernel { |
| 23 | + public: |
| 24 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 25 | + |
| 26 | + protected: |
| 27 | + void InferShape(framework::InferShapeContextBase* ctx) const override { |
| 28 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null."); |
| 29 | + PADDLE_ENFORCE(ctx->HasInput("Labels"), |
| 30 | + "Input(Labels) should be not null."); |
| 31 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) should be not null."); |
| 32 | + |
| 33 | + auto x_dims = ctx->GetInputDim("X"); |
| 34 | + auto labels_dims = ctx->GetInputDim("Labels"); |
| 35 | + PADDLE_ENFORCE_EQ(x_dims.size(), 2, "Input(X)'s rank should be 2."); |
| 36 | + PADDLE_ENFORCE_EQ(labels_dims.size(), 2, |
| 37 | + "Input(Labels)'s rank should be 2."); |
| 38 | + PADDLE_ENFORCE_EQ(x_dims[0], labels_dims[0], |
| 39 | + "The 1st dimension of Input(X) and Input(Labels) should " |
| 40 | + "be equal."); |
| 41 | + PADDLE_ENFORCE_EQ(x_dims[1], labels_dims[1], |
| 42 | + "The 2nd dimension of Input(X) and Input(Labels) should " |
| 43 | + "be equal."); |
| 44 | + |
| 45 | + ctx->SetOutputDim("Out", x_dims); |
| 46 | + ctx->ShareLoD("X", /*->*/ "Out"); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +class SigmoidCrossEntropyWithLogitsGradOp |
| 51 | + : public framework::OperatorWithKernel { |
| 52 | + public: |
| 53 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 54 | + |
| 55 | + protected: |
| 56 | + void InferShape(framework::InferShapeContextBase* ctx) const override { |
| 57 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null."); |
| 58 | + PADDLE_ENFORCE(ctx->HasInput("Labels"), |
| 59 | + "Input(Labels) should be not null."); |
| 60 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")), |
| 61 | + "Input(Out@GRAD) shoudl be not null."); |
| 62 | + PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")), |
| 63 | + "Output(X@GRAD) should be not null."); |
| 64 | + |
| 65 | + auto x_dims = ctx->GetInputDim("X"); |
| 66 | + auto labels_dims = ctx->GetInputDim("Labels"); |
| 67 | + auto dout_dims = ctx->GetInputDim(framework::GradVarName("Out")); |
| 68 | + PADDLE_ENFORCE_EQ(x_dims.size(), 2, "Input(X)'s rank should be 2."); |
| 69 | + PADDLE_ENFORCE_EQ(labels_dims.size(), 2, |
| 70 | + "Input(Labels)'s rank should be 2."); |
| 71 | + PADDLE_ENFORCE_EQ(dout_dims.size(), 2, |
| 72 | + "Input(Out@Grad)'s rank should be 2."); |
| 73 | + PADDLE_ENFORCE_EQ(x_dims[0], labels_dims[0], |
| 74 | + "The 1st dimension of Input(X) and Input(Labels) should " |
| 75 | + "be equal."); |
| 76 | + PADDLE_ENFORCE_EQ(x_dims[1], labels_dims[1], |
| 77 | + "The 2nd dimension of Input(X) and Input(Labels) should " |
| 78 | + "be equal."); |
| 79 | + PADDLE_ENFORCE_EQ(x_dims[0], dout_dims[0], |
| 80 | + "The 1st dimension of Input(X) and Input(Out@Grad) " |
| 81 | + "should be equal."); |
| 82 | + PADDLE_ENFORCE_EQ(x_dims[1], dout_dims[1], |
| 83 | + "The 2nd dimension of Input(X) and Input(Out@Grad) " |
| 84 | + "should be equal."); |
| 85 | + |
| 86 | + ctx->SetOutputDim(framework::GradVarName("X"), x_dims); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +class SigmoidCrossEntropyWithLogitsOpMaker |
| 91 | + : public framework::OpProtoAndCheckerMaker { |
| 92 | + public: |
| 93 | + SigmoidCrossEntropyWithLogitsOpMaker(framework::OpProto* proto, |
| 94 | + framework::OpAttrChecker* op_checker) |
| 95 | + : framework::OpProtoAndCheckerMaker(proto, op_checker) { |
| 96 | + AddInput("X", |
| 97 | + "(Tensor, default Tensor<float>), a 2-D tensor with shape N x D, " |
| 98 | + "where N is the batch size and D is the number of classes. " |
| 99 | + "This input is a tensor of logits computed by the previous " |
| 100 | + " operator. Logits are unscaled log probabilities given as " |
| 101 | + "log(p/(1-p))."); |
| 102 | + AddInput("Labels", |
| 103 | + "(Tensor, default Tensor<float>), a 2-D tensor of the same type " |
| 104 | + "and shape as X. This input is a tensor of probabalistic labels " |
| 105 | + "for each logit"); |
| 106 | + AddOutput("Out", |
| 107 | + "(Tensor, default Tensor<float>), a 2-D tensor with shape N x D " |
| 108 | + " of elementwise logistic losses."); |
| 109 | + AddComment(R"DOC( |
| 110 | +SigmoidCrossEntropyWithLogits Operator. |
| 111 | +
|
| 112 | +This measures the elementwise probability error in discrete classification tasks |
| 113 | +in which each class is independent. This can be thought of as predicting labels |
| 114 | +for a data-point that are not mutually exclusive. For example, a news article |
| 115 | +can be about politics, technology or sports at the same time or none of these. |
| 116 | +
|
| 117 | +The logistic loss is given as follows: |
| 118 | +
|
| 119 | + loss = -Labels * log(sigmoid(X)) - (1 - Labels) * log(1 - sigmoid(X)) |
| 120 | +
|
| 121 | +We know that sigmoid(X) = (1 / (1 + exp(-X))). By substituting this we get |
| 122 | +
|
| 123 | + loss = X - X * Labels + log(1 + exp(-X)) |
| 124 | +
|
| 125 | +For stability and to prevent overflow of exp(-X) when X < 0, |
| 126 | +we can reformulate the loss as follows: |
| 127 | +
|
| 128 | + loss = max(X, 0) - X * Labels + log(1 + exp(-abs(X))) |
| 129 | +
|
| 130 | +Both the input `X` and `Labels` can carry the LoD (Level of Details) information. |
| 131 | +However the output only shares the LoD with input `X`. |
| 132 | +)DOC"); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +} // namespace operators |
| 137 | +} // namespace paddle |
| 138 | + |
| 139 | +namespace ops = paddle::operators; |
| 140 | +REGISTER_OP(sigmoid_cross_entropy_with_logits, |
| 141 | + ops::SigmoidCrossEntropyWithLogitsOp, |
| 142 | + ops::SigmoidCrossEntropyWithLogitsOpMaker, |
| 143 | + sigmoid_cross_entropy_with_logits_grad, |
| 144 | + ops::SigmoidCrossEntropyWithLogitsGradOp); |
| 145 | +REGISTER_OP_CPU_KERNEL(sigmoid_cross_entropy_with_logits, |
| 146 | + ops::SigmoidCrossEntropyWithLogitsKernel< |
| 147 | + paddle::platform::CPUPlace, float>); |
| 148 | +REGISTER_OP_CPU_KERNEL(sigmoid_cross_entropy_with_logits_grad, |
| 149 | + ops::SigmoidCrossEntropyWithLogitsGradKernel< |
| 150 | + paddle::platform::CPUPlace, float>); |
0 commit comments