cross_entropy.cc 2.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
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
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/math/cross_entropy.h"
16 17 18 19 20 21 22 23 24 25 26

namespace paddle {
namespace operators {
namespace math {

using Tensor = framework::Tensor;
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;

template <typename T>
Q
QI JUN 已提交
27
class CrossEntropyFunctor<platform::CPUDeviceContext, T> {
28
 public:
Q
QI JUN 已提交
29
  void operator()(const platform::CPUDeviceContext& ctx, framework::Tensor* out,
Q
qijun 已提交
30
                  const framework::Tensor* prob,
31
                  const framework::Tensor* labels, const bool softLabel,
32
                  const int ignore_index, const int axis_dim) {
33
    const int batch_size = prob->dims()[0];
34 35 36 37 38
    const int num_classes = prob->dims()[1];
    const int num_remain = num_classes / axis_dim;

    Eigen::DSizes<int, 3> batch_axis_remain(batch_size, axis_dim, num_remain);

39 40 41 42 43
    if (softLabel) {
      auto in = EigenMatrix<T>::From(*prob);
      auto lbl = EigenMatrix<T>::From(*labels);
      auto loss = EigenMatrix<T>::From(*out);

Q
QI JUN 已提交
44
      loss.device(*ctx.eigen_device()) =
45
          -((lbl * in.log().unaryExpr(math::TolerableValue<T>()))
46 47
                .reshape(batch_axis_remain)
                .sum(Eigen::DSizes<int, 1>(1)));
48 49 50 51
    } else {
      const T* prob_data = prob->data<T>();
      T* loss_data = out->data<T>();

52
      const int64_t* label_data = labels->data<int64_t>();
53
      for (int i = 0; i < batch_size; ++i) {
54 55 56 57 58 59 60 61 62 63
        for (int j = 0; j < num_remain; j++) {
          int lbl = label_data[i * num_remain + j];
          PADDLE_ENFORCE((lbl >= 0 && lbl < axis_dim) || lbl == ignore_index);
          int index = i * num_classes + lbl * num_remain + j;
          int loss_idx = i * num_remain + j;
          loss_data[loss_idx] =
              lbl == ignore_index
                  ? 0
                  : -math::TolerableValue<T>()(std::log(prob_data[index]));
        }
64 65 66 67 68
      }
    }
  }
};

Q
QI JUN 已提交
69 70
template class CrossEntropyFunctor<platform::CPUDeviceContext, float>;
template class CrossEntropyFunctor<platform::CPUDeviceContext, double>;
71 72 73
}  // namespace math
}  // namespace operators
}  // namespace paddle