cross_entropy_op.cc 3.1 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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/cross_entropy_op.h"

namespace paddle {
namespace operators {

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

24
 protected:
D
dongzhihong 已提交
25
  void InferShape(const framework::InferShapeContext &ctx) const override {
26 27
    auto *x = ctx.Input<Tensor>("X");
    auto *label = ctx.Input<Tensor>("Label");
Y
Yu Yang 已提交
28

29 30 31 32 33 34 35 36 37 38
    PADDLE_ENFORCE_EQ(x->dims().size(), 2, "X's rank must be 2.");
    PADDLE_ASSERT(label->dims().size() == 1 || label->dims().size() == 2);
    if (label->dims().size() == 2) {
      // soft cross entropy
      PADDLE_ENFORCE_EQ(x->dims(), label->dims());
    } else {
      // normal cross entropy
      PADDLE_ENFORCE_EQ(x->dims()[0], label->dims()[0]);
    }
    ctx.Output<Tensor>("Y")->Resize({x->dims()[0]});
Q
Qiao Longfei 已提交
39 40 41
  }
};

42
class CrossEntropyGradientOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
43 44 45
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

Y
Yan Chunwei 已提交
46
 protected:
D
dongzhihong 已提交
47
  void InferShape(const framework::InferShapeContext &ctx) const override {
48 49
    auto dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto x = ctx.Input<Tensor>("X");
Y
Yan Chunwei 已提交
50

51
    dx->Resize(x->dims());
Y
Yan Chunwei 已提交
52 53 54
  }
};

55
class CrossEntropyOpMaker : public framework::OpProtoAndCheckerMaker {
56
 public:
57 58
  CrossEntropyOpMaker(framework::OpProto *proto,
                      framework::OpAttrChecker *op_checker)
59
      : OpProtoAndCheckerMaker(proto, op_checker) {
60 61 62
    AddInput("X", "The first input of CrossEntropyOp");
    AddInput("Label", "The second input of CrossEntropyOp");
    AddOutput("Y", "The output of CrossEntropyOp");
Q
Qiao Longfei 已提交
63
    AddComment(R"DOC(
64
CrossEntropy Operator.
Q
Qiao Longfei 已提交
65

66 67 68
The second input (Label tensor) supports two kinds of shapes:
1) Rank(Label) = 1, Label[i] indicates the class index for sample i:
                Y[i] = -log(X[i, Label[i]])
Q
Qiao Longfei 已提交
69

70 71 72 73 74 75
2) Rank(Label) = 2, Label[i, j] indicates the soft label of class j
   for sample i:
                Y[i] = \sum_j{-Label[i, j] * log(X[i, j])}
   Please make sure that in this case the summuation of each row of Label
   equals one. If each row of Label has only one non-zero element (equals 1),
   it degenerates to a standard one-hot representation.
Q
Qiao Longfei 已提交
76 77 78 79 80 81
)DOC");
  }
};
}  // namespace operators
}  // namespace paddle

D
dongzhihong 已提交
82
namespace ops = paddle::operators;
83 84 85 86 87
REGISTER_OP(cross_entropy, ops::CrossEntropyOp, ops::CrossEntropyOpMaker,
            cross_entropy_grad, ops::CrossEntropyGradientOp);
REGISTER_OP_CPU_KERNEL(cross_entropy, ops::CrossEntropyOpKernel<float>);
REGISTER_OP_CPU_KERNEL(cross_entropy_grad,
                       ops::CrossEntropyGradientOpKernel<float>);