hinge_loss_op.h 2.7 KB
Newer Older
S
Siddharth Goyal 已提交
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
Y
Yi Wang 已提交
16 17
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
S
Siddharth Goyal 已提交
18 19 20 21

namespace paddle {
namespace operators {

Q
QI JUN 已提交
22
template <typename DeviceContext, typename T, typename AttrType = T>
S
Siddharth Goyal 已提交
23 24 25 26 27 28
class HingeLossKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* pred = context.Input<framework::Tensor>("Logits");
    auto* label = context.Input<framework::Tensor>("Labels");
    auto* loss = context.Output<framework::Tensor>("Loss");
Q
QI JUN 已提交
29 30
    auto& place =
        *context.template device_context<DeviceContext>().eigen_device();
S
Siddharth Goyal 已提交
31 32 33 34 35 36 37 38 39 40 41

    auto x = framework::EigenVector<T>::Flatten(*pred);
    auto y = framework::EigenVector<T>::Flatten(*label);
    loss->mutable_data<T>(context.GetPlace());
    auto l = framework::EigenVector<T>::Flatten(*loss);
    l.device(place) =
        (static_cast<T>(1) - x * (static_cast<T>(2) * y - static_cast<T>(1)))
            .cwiseMax(static_cast<T>(0));
  }
};

Q
QI JUN 已提交
42
template <typename DeviceContext, typename T, typename AttrType = T>
S
Siddharth Goyal 已提交
43 44 45 46 47 48 49 50 51
class HingeLossGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* pred = context.Input<framework::Tensor>("Logits");
    auto* label = context.Input<framework::Tensor>("Labels");
    auto* dloss =
        context.Input<framework::Tensor>(framework::GradVarName("Loss"));
    auto* dpred =
        context.Output<framework::Tensor>(framework::GradVarName("Logits"));
Q
QI JUN 已提交
52 53
    auto& place =
        *context.template device_context<DeviceContext>().eigen_device();
S
Siddharth Goyal 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    auto x = framework::EigenVector<T>::Flatten(*pred);
    auto y = framework::EigenVector<T>::Flatten(*label);
    auto dl = framework::EigenVector<T>::Flatten(*dloss);

    if (dpred) {
      dpred->mutable_data<T>(context.GetPlace());
      auto dx = framework::EigenVector<T>::Flatten(*dpred);
      auto alt_labels = static_cast<T>(2) * y - static_cast<T>(1);
      dx.device(place) =
          dl * ((x * alt_labels) < static_cast<T>(1)).template cast<T>() *
          (-alt_labels);
    }
  }
};

}  // namespace operators
}  // namespace paddle