cross_entropy_op.h 2.5 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
16
#include "paddle/operators/type_alias.h"
Q
Qiao Longfei 已提交
17 18 19 20

namespace paddle {
namespace operators {

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
template <typename T>
T tolerable_value(T x) {
  static_assert(std::is_floating_point<T>::value,
                "tolerable_value works only on float, "
                "double and double double.");

  const T kApproInf = 1e20;

  if (x == INFINITY) {
    return kApproInf;
  }

  if (x == -INFINITY) {
    return -kApproInf;
  }

  return x;
}
Y
Yan Chunwei 已提交
39

Q
Qiao Longfei 已提交
40
template <typename Place, typename T>
41
class OnehotCrossEntropyOpKernel : public OpKernel {
42
 public:
43
  void Compute(const ExecutionContext& ctx) const override {
Y
Yan Chunwei 已提交
44 45
    auto X = ctx.Input<Tensor>("X");
    const T* Xdata = X->data<T>();
46
    const int* label_data = ctx.Input<Tensor>(1)->data<int>();
Y
Yan Chunwei 已提交
47
    auto Y = ctx.Output<Tensor>("Y");
Q
Qiao Longfei 已提交
48

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

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

53 54
    int batch_size = X->dims()[0];
    int class_num = X->dims()[1];
Q
Qiao Longfei 已提交
55 56

    for (int i = 0; i < batch_size; ++i) {
57 58
      int index = i * class_num + label_data[i];
      Ydata[i] = -tolerable_value(std::log(Xdata[index]));
Y
Yan Chunwei 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    }
  }
};

template <typename Place, typename T>
class OnehotCrossEntropyGradientOpKernel : public OpKernel {
 public:
  void Compute(const ExecutionContext& ctx) const override {
    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];

    for (int i = 0; i < batch_size; ++i) {
81 82
      int index = i * class_num + label_data[i];
      dXdata[index] = -tolerable_value(dYdata[i] / Xdata[index]);
Q
Qiao Longfei 已提交
83 84 85 86 87 88
    }
  }
};

}  // namespace operators
}  // namespace paddle