onehot_cross_entropy_op.h 2.6 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#pragma once
D
dongzhihong 已提交
16
#include "paddle/framework/op_registry.h"
C
caoying03 已提交
17
#include "paddle/operators/math/utils.h"
Q
Qiao Longfei 已提交
18 19 20 21

namespace paddle {
namespace operators {

D
dongzhihong 已提交
22 23
using Tensor = framework::Tensor;

24
template <typename T>
D
dongzhihong 已提交
25
class OnehotCrossEntropyOpKernel : public framework::OpKernel {
26
 public:
D
dongzhihong 已提交
27
  void Compute(const framework::ExecutionContext& ctx) const override {
28 29 30
    PADDLE_ENFORCE(platform::is_cpu_place(ctx.GetPlace()),
                   "It must use CPUPlace.");

Y
Yan Chunwei 已提交
31 32
    auto X = ctx.Input<Tensor>("X");
    const T* Xdata = X->data<T>();
Y
Yu Yang 已提交
33
    const int* label_data = ctx.Input<Tensor>("label")->data<int>();
Y
Yan Chunwei 已提交
34
    auto Y = ctx.Output<Tensor>("Y");
Q
Qiao Longfei 已提交
35

36
    Y->mutable_data<T>(ctx.GetPlace());
Q
Qiao Longfei 已提交
37

Y
Yan Chunwei 已提交
38
    T* Ydata = Y->data<T>();
Q
Qiao Longfei 已提交
39

C
caoying03 已提交
40 41
    const int batch_size = X->dims()[0];
    const int class_num = X->dims()[1];
Q
Qiao Longfei 已提交
42 43

    for (int i = 0; i < batch_size; ++i) {
44
      int index = i * class_num + label_data[i];
C
caoying03 已提交
45
      Ydata[i] = -math::tolerable_value(std::log(Xdata[index]));
Y
Yan Chunwei 已提交
46 47 48 49
    }
  }
};

50
template <typename T>
D
dongzhihong 已提交
51
class OnehotCrossEntropyGradientOpKernel : public framework::OpKernel {
Y
Yan Chunwei 已提交
52
 public:
D
dongzhihong 已提交
53
  void Compute(const framework::ExecutionContext& ctx) const override {
54 55 56
    PADDLE_ENFORCE(platform::is_cpu_place(ctx.GetPlace()),
                   "It must use CPUPlace.");

Y
Yan Chunwei 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69
    auto X = ctx.Input<Tensor>("X");
    auto dX = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto dY = ctx.Input<Tensor>(framework::GradVarName("Y"));
    auto label = ctx.Input<Tensor>("label");

    auto* dXdata = dX->template mutable_data<T>(ctx.GetPlace());
    auto* dYdata = dY->template data<T>();
    auto* Xdata = X->template data<T>();
    auto* label_data = label->data<int>();

    const int batch_size = X->dims()[0];
    const int class_num = X->dims()[1];

70
    // TODO(qingqing): make zero setting an common function.
71
    memset(dXdata, 0, sizeof(T) * batch_size * class_num);
Y
Yan Chunwei 已提交
72
    for (int i = 0; i < batch_size; ++i) {
73
      int index = i * class_num + label_data[i];
C
caoying03 已提交
74
      dXdata[index] = -math::tolerable_value(dYdata[i] / Xdata[index]);
Q
Qiao Longfei 已提交
75 76 77 78 79 80
    }
  }
};

}  // namespace operators
}  // namespace paddle