teacher_student_sigmoid_loss_op.cc 10.2 KB
Newer Older
H
heqiaozhi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
/* 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/fluid/operators/teacher_student_sigmoid_loss_op.h"
#include "paddle/fluid/operators/math/math_function.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

class TeacherStudentSigmoidLossOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
    PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
    PADDLE_ENFORCE(ctx->HasOutput("Y"), "Output(Y) should be not null.");

    auto x_dims = ctx->GetInputDim("X");
    auto label_dims = ctx->GetInputDim("Label");
    PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "Input(X)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(label_dims.size(), 2UL,
                      "Input(Label)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0],
                      "The 1st dimension of Input(X) and Input(Label) should "
                      "be equal.");
    PADDLE_ENFORCE_EQ(label_dims[1], 1UL,
                      "The 2nd dimension of "
                      "Input(Label) should be 1.");
    ctx->SetOutputDim("Y", {x_dims[0], 1});
    ctx->ShareLoD("X", /*->*/ "Y");
  }

 protected:
  // Explicitly set that the data type of computation kernel of
  // teacher_student_sigmoid_loss
  // is determined by its input "X".
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(ctx.Input<Tensor>("X")->type(),
                                   ctx.device_context());
  }
};

class TeacherStudentSigmoidLossGradientOp
    : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
    PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Y")),
                   "Input(Y@GRAD) should be not null.");
    PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
                   "Output(X@GRAD) should be not null.");

    auto x_dims = ctx->GetInputDim("X");
    auto label_dims = ctx->GetInputDim("Label");
    auto dy_dims = ctx->GetInputDim(framework::GradVarName("Y"));
    PADDLE_ENFORCE_EQ(x_dims.size(), 2, "Input(X)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(dy_dims.size(), 2, "Input(Y@Grad)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(label_dims.size(), 2, "Input(Label)'s rank should be 2.");
    PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0],
                      "The 1st dimension of Input(X) and Input(Label) should "
                      "be equal.");
    PADDLE_ENFORCE_EQ(x_dims[0], dy_dims[0],
                      "The 1st dimension of Input(X) and Input(Y@Grad) should "
                      "be equal.");
    PADDLE_ENFORCE_EQ(dy_dims[1], 1,
                      "The 2nd dimension of Input(Y@Grad) should be 1.");
    PADDLE_ENFORCE_EQ(label_dims[1], 1,
                      "When Attr(soft_label) == false, the 2nd dimension of "
                      "Input(Label) should be 1.");
    ctx->SetOutputDim(framework::GradVarName("X"), x_dims);
    ctx->ShareLoD("X", framework::GradVarName("X"));
  }

 protected:
  // Explicitly set that the data type of computation kernel of
  // teacher_student_sigmoid_loss
  // is determined by its input "X".
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(ctx.Input<Tensor>("X")->type(),
                                   ctx.device_context());
  }
};

class TeacherStudentSigmoidLossOpMaker
    : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X",
             "(Tensor, default Tensor<float>), a 2-D tensor with shape [N x 1],"
             " where N is the batch size and D is the output. "
             "This input is a probability computed by the previous operator, "
             "which is almost always the result of a softmax operator.");
    AddInput("Label",
             "(Tensor), the ground truth which is a 2-D tensor. "
             "Label is a Tensor<float> with shape [N x 1]. ");
    AddOutput("Y",
              "(Tensor, default Tensor<float>), a 2-D tensor with shape "
              "[N x 1]. The teacher student sigmoid loss.");
    AddAttr<float>("soft_max_up_bound", "fp32, default 15.0").SetDefault(15.0);
    AddAttr<float>("soft_max_lower_bound", "fp32, default -15.0")
        .SetDefault(-15.0);
    AddComment(R"DOC(
TeacherStudentSigmoidLoss Operator.
TeacherStudentSigmoidLoss Operator.

It's similarity to SigmoidCrossEntropyWithLogits Operator. The difference is that
we add another label(z') to original.
        loss = max(x, 0) - x * z + log(1 + exp(-abs(x))) + max(x, 0) - x * z' + log(1 + exp(-abs(x)))
        z is click or not
        z' is value q of feed_fine
        label = {-2, -1, [0, 2]}
        when z' is not exist, clk = 0 : label = -2;
        when z' is not exist, clk = 1 : label = -1;
        when z' is exist    , clk = 0 : label = 0 + z';
        when z' is exist    , clk = 1 : label = 1 + z';

)DOC");
  }
};

// template <typename DeviceContext, typename T>
template <typename T>
class TeacherStudentSigmoidLossOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    PADDLE_ENFORCE(platform::is_cpu_place(context.GetPlace()),
                   "This kernel only runs on CPU.");

    Tensor* y = context.Output<Tensor>("Y");
    const Tensor* x = context.Input<Tensor>("X");
    const Tensor* labels = context.Input<Tensor>("Label");
    T* y_data = y->mutable_data<T>(context.GetPlace());
    const T* x_data = x->data<T>();
    const T* label_data = labels->data<T>();
    int64_t batch_size = x->dims()[0];
    // loss = max(x, 0) - x * z + log(1 + exp(-abs(x))) + max(x, 0) - x * z' +
    // log(1 + exp(-abs(x)))
    // z is click or not
    // z' is value q of feed_fine
    // label = {-2, -1, [0, 2]}
    // when z' is not exist, clk = 0 : label = -2;
    // when z' is not exist, clk = 1 : label = -1;
    // when z' is exist    , clk = 0 : label = 0 + z';
    // when z' is exist    , clk = 1 : label = 1 + z';
    for (int i = 0; i < batch_size; ++i) {
      if (label_data[i] < -1.0) {
        y_data[i] = (x_data[i] > 0 ? x_data[i] : 0.0) +
                    log(1.0 + exp(-fabs(x_data[i])));
      } else if (label_data[i] < 0.0) {
        y_data[i] = (x_data[i] > 0 ? x_data[i] : 0.0) - x_data[i] +
                    log(1.0 + exp(-fabs(x_data[i])));
      } else if (label_data[i] < 1.0) {
        y_data[i] = (x_data[i] > 0 ? x_data[i] : 0.0) +
                    log(1.0 + exp(-fabs(x_data[i]))) +
                    (x_data[i] > 0 ? x_data[i] : 0.0) -
                    x_data[i] * label_data[i] +
                    log(1.0 + exp(-fabs(x_data[i])));
      } else {
        y_data[i] = (x_data[i] > 0 ? x_data[i] : 0.0) - x_data[i] +
                    log(1.0 + exp(-fabs(x_data[i]))) +
                    (x_data[i] > 0 ? x_data[i] : 0.0) -
                    x_data[i] * (label_data[i] - 1.0) +
                    log(1.0 + exp(-fabs(x_data[i])));
      }
    }
  }
};

template <typename T>
class TeacherStudentSigmoidLossGradOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const Tensor* x = context.Input<Tensor>("X");
    const T* x_data = x->data<T>();

    Tensor* dx = context.Output<Tensor>(framework::GradVarName("X"));
    T* dx_data = dx->mutable_data<T>(context.GetPlace());

    const Tensor* labels = context.Input<Tensor>("Label");
    const T* label_data = labels->data<T>();

    T soft_max_up_bound =
        static_cast<T>(context.Attr<float>("soft_max_up_bound"));
    T soft_max_lower_bound =
        static_cast<T>(context.Attr<float>("soft_max_lower_bound"));

    int64_t batch_size = x->dims()[0];

    const framework::Tensor* dOut =
        context.Input<framework::Tensor>(framework::GradVarName("Y"));

    const T* dout_data = dOut->data<T>();

    for (int i = 0; i < batch_size; ++i) {
      T sum_val = x_data[i];
      if (sum_val > soft_max_up_bound) {
        sum_val = soft_max_up_bound;
      } else {
        if (sum_val < soft_max_lower_bound) {
          sum_val = soft_max_lower_bound;
        }
      }

      T pred = 1.0 / (1.0 + exp(-sum_val));
      if (label_data[i] < -1.0) {
        dx_data[i] = 0.0 - pred;
      } else if (label_data[i] < 0.0) {
        dx_data[i] = 1.0 - pred;
      } else {
        dx_data[i] = label_data[i] - 2.0 * pred;
      }
      if (sum_val >= soft_max_up_bound || sum_val <= soft_max_lower_bound) {
        dx_data[i] = 0;
      }
      dx_data[i] *= dout_data[i] * -1;
    }
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(teacher_student_sigmoid_loss,
                  ops::TeacherStudentSigmoidLossOp,
                  ops::TeacherStudentSigmoidLossOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>);

REGISTER_OPERATOR(teacher_student_sigmoid_loss_grad,
                  ops::TeacherStudentSigmoidLossGradientOp);

REGISTER_OP_CPU_KERNEL(teacher_student_sigmoid_loss,
                       ops::TeacherStudentSigmoidLossOpKernel<float>,
                       ops::TeacherStudentSigmoidLossOpKernel<double>);

REGISTER_OP_CPU_KERNEL(teacher_student_sigmoid_loss_grad,
                       ops::TeacherStudentSigmoidLossGradOpKernel<float>,
                       ops::TeacherStudentSigmoidLossGradOpKernel<double>);