/* 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/math/cross_entropy.h" namespace paddle { namespace operators { namespace math { using Tensor = framework::Tensor; template using EigenMatrix = framework::EigenMatrix; template class CrossEntropyFunctor { public: void operator()(const platform::DeviceContext& ctx, framework::Tensor* out, const framework::Tensor* prob, const framework::Tensor* labels, const bool softLabel) { const int batch_size = prob->dims()[0]; if (softLabel) { auto in = EigenMatrix::From(*prob); auto lbl = EigenMatrix::From(*labels); auto loss = EigenMatrix::From(*out); loss.device(*ctx.GetEigenDevice()) = -((lbl * in.log().unaryExpr(math::TolerableValue())) .sum(Eigen::DSizes(1)) .reshape(Eigen::DSizes(batch_size, 1))); } else { const int class_num = prob->dims()[1]; const T* prob_data = prob->data(); T* loss_data = out->data(); const int* label_data = labels->data(); for (int i = 0; i < batch_size; ++i) { int index = i * class_num + label_data[i]; loss_data[i] = -math::TolerableValue()(std::log(prob_data[index])); } } } }; template class CrossEntropyFunctor; } // namespace math } // namespace operators } // namespace paddle