/* 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 #include "paddle/operators/type_alias.h" namespace paddle { namespace operators { static const float kCrossEntropyLogThreshold{1e-20}; template class OnehotCrossEntropyOpKernel : public OpKernel { public: void Compute(const ExecutionContext& ctx) const override { auto X = ctx.Input("X"); const T* Xdata = X->data(); const int* label_data = ctx.Input(1)->data(); auto Y = ctx.Output("Y"); Y->mutable_data(ctx.GetPlace()); T* Ydata = Y->data(); int batch_size = X->dims()[0]; int class_num = X->dims()[1]; // Y[i] = -log(X[i][j]) for (int i = 0; i < batch_size; ++i) { Ydata[i] = -std::log(std::max(Xdata[i * class_num + label_data[i]], kCrossEntropyLogThreshold)); } } }; template class OnehotCrossEntropyGradientOpKernel : public OpKernel { public: void Compute(const ExecutionContext& ctx) const override { auto X = ctx.Input("X"); auto dX = ctx.Output(framework::GradVarName("X")); auto dY = ctx.Input(framework::GradVarName("Y")); auto label = ctx.Input("label"); auto* dXdata = dX->template mutable_data(ctx.GetPlace()); auto* dYdata = dY->template data(); auto* Xdata = X->template data(); auto* label_data = label->data(); const int batch_size = X->dims()[0]; const int class_num = X->dims()[1]; for (int i = 0; i < batch_size; ++i) { dXdata[i * class_num + label_data[i]] = -dYdata[i] / std::max(Xdata[i * class_num + label_data[i]], kCrossEntropyLogThreshold); } } }; } // namespace operators } // namespace paddle