activation_op.cc 19.4 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
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
Q
qijun 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Q
qijun 已提交
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. */
Q
qijun 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/activation_op.h"
K
Krzysztof Binias 已提交
16
#include "paddle/fluid/operators/mkldnn_activation_op.h"
Q
qijun 已提交
17 18 19 20

namespace paddle {
namespace operators {

Q
qijun 已提交
21 22 23 24
class ActivationOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

25
  void InferShape(framework::InferShapeContext *ctx) const override {
F
fengjiayi 已提交
26 27
    ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
    ctx->ShareLoD("X", /*->*/ "Out");
Q
qijun 已提交
28
  }
Q
qijun 已提交
29 30
};

Q
qijun 已提交
31 32 33 34
class ActivationOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

35
  void InferShape(framework::InferShapeContext *ctx) const override {
F
fengjiayi 已提交
36
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("Out"));
Q
qijun 已提交
37 38 39
  }
};

Q
qijun 已提交
40 41
class SigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
42 43
  SigmoidOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
Q
qijun 已提交
44
    AddInput("X", "Input of Sigmoid operator");
F
fengjiayi 已提交
45
    AddOutput("Out", "Output of Sigmoid operator");
K
Kexin Zhao 已提交
46
    AddComment(R"DOC(
47
Sigmoid Activation Operator
K
Kexin Zhao 已提交
48

F
fengjiayi 已提交
49
$$out = \frac{1}{1 + e^{-x}}$$
K
Kexin Zhao 已提交
50 51

)DOC");
Q
qijun 已提交
52 53 54
  }
};

55 56
class LogSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
57 58
  LogSigmoidOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
59
    AddInput("X", "Input of LogSigmoid operator");
F
fengjiayi 已提交
60
    AddOutput("Out", "Output of LogSigmoid operator");
K
Kexin Zhao 已提交
61
    AddComment(R"DOC(
62
Logsigmoid Activation Operator
K
Kexin Zhao 已提交
63

F
fengjiayi 已提交
64
$$out = \log \frac{1}{1 + e^{-x}}$$
K
Kexin Zhao 已提交
65 66

)DOC");
67 68 69
  }
};

Q
qijun 已提交
70 71
class ExpOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
72 73
  ExpOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
Q
qijun 已提交
74
    AddInput("X", "Input of Exp operator");
F
fengjiayi 已提交
75
    AddOutput("Out", "Output of Exp operator");
K
Kexin Zhao 已提交
76
    AddComment(R"DOC(
K
kexinzhao 已提交
77
Exp Activation Operator.
K
Kexin Zhao 已提交
78

F
fengjiayi 已提交
79
$out = e^x$
K
Kexin Zhao 已提交
80 81

)DOC");
Q
qijun 已提交
82 83 84 85 86
  }
};

class ReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
87 88
  ReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
Q
qijun 已提交
89
    AddInput("X", "Input of Relu operator");
F
fengjiayi 已提交
90
    AddOutput("Out", "Output of Relu operator");
91 92 93
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
94
    AddComment(R"DOC(
K
kexinzhao 已提交
95
Relu Activation Operator.
K
Kexin Zhao 已提交
96

F
fengjiayi 已提交
97
$out = \max(x, 0)$
K
Kexin Zhao 已提交
98 99

)DOC");
100 101 102
  }
};

K
Kavya Srinet 已提交
103 104
class LeakyReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
105 106
  LeakyReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
K
Kavya Srinet 已提交
107
    AddInput("X", "Input of LeakyRelu operator");
F
fengjiayi 已提交
108
    AddOutput("Out", "Output of LeakyRelu operator");
109
    AddAttr<float>("alpha", "The small negative slope").SetDefault(0.02f);
K
Kexin Zhao 已提交
110
    AddComment(R"DOC(
K
kexinzhao 已提交
111
LeakyRelu Activation Operator.
K
Kexin Zhao 已提交
112

F
fengjiayi 已提交
113
$out = \max(x, \alpha * x)$
K
Kexin Zhao 已提交
114 115

)DOC");
K
Kavya Srinet 已提交
116 117 118
  }
};

119 120
class SoftShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
121 122
  SoftShrinkOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
123
    AddInput("X", "Input of Softshrink operator");
F
fengjiayi 已提交
124
    AddOutput("Out", "Output of Softshrink operator");
125
    AddAttr<float>("lambda", "non-negative offset").SetDefault(0.5f);
K
Kexin Zhao 已提交
126
    AddComment(R"DOC(
K
kexinzhao 已提交
127
Softshrink Activation Operator.
K
Kexin Zhao 已提交
128 129

$$
F
fengjiayi 已提交
130
out = \begin{cases} 
K
Kexin Zhao 已提交
131 132 133 134 135 136 137
    x - \lambda, \text{if } x > \lambda \\
    x + \lambda, \text{if } x < -\lambda \\
    0,  \text{otherwise}
    \end{cases}
$$

)DOC");
138 139 140
  }
};

141 142
class TanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
143 144
  TanhOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
145
    AddInput("X", "Input of Tanh operator");
F
fengjiayi 已提交
146
    AddOutput("Out", "Output of Tanh operator");
147 148 149
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
150
    AddComment(R"DOC(
K
kexinzhao 已提交
151
Tanh Activation Operator.
K
Kexin Zhao 已提交
152

F
fengjiayi 已提交
153
$$out = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$
K
Kexin Zhao 已提交
154 155

)DOC");
156 157 158
  }
};

K
Kavya Srinet 已提交
159 160
class TanhShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
161 162
  TanhShrinkOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
K
Kavya Srinet 已提交
163
    AddInput("X", "Input of TanhShrink operator");
F
fengjiayi 已提交
164
    AddOutput("Out", "Output of TanhShrink operator");
K
Kexin Zhao 已提交
165
    AddComment(R"DOC(
K
kexinzhao 已提交
166
TanhShrink Activation Operator.
K
Kexin Zhao 已提交
167

F
fengjiayi 已提交
168
$$out = x - \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$
K
Kexin Zhao 已提交
169 170

)DOC");
K
Kavya Srinet 已提交
171 172 173
  }
};

174 175
class HardShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
176 177
  HardShrinkOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
178
    AddInput("X", "Input of HardShrink operator");
F
fengjiayi 已提交
179
    AddOutput("Out", "Output of HardShrink operator");
180 181
    AddAttr<float>("threshold", "The value of threshold for HardShrink")
        .SetDefault(0.5f);
K
Kexin Zhao 已提交
182
    AddComment(R"DOC(
K
kexinzhao 已提交
183
HardShrink Activation Operator.
K
Kexin Zhao 已提交
184 185

$$
F
fengjiayi 已提交
186
out = \begin{cases} 
K
Kexin Zhao 已提交
187 188 189 190 191 192 193
    x, \text{if } x > \lambda \\
    x, \text{if } x < -\lambda \\
    0,  \text{otherwise}
    \end{cases}
$$

)DOC");
194 195 196
  }
};

197 198
class SqrtOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
199 200
  SqrtOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
201
    AddInput("X", "Input of Sqrt operator");
F
fengjiayi 已提交
202
    AddOutput("Out", "Output of Sqrt operator");
203 204 205
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
206
    AddComment(R"DOC(
K
kexinzhao 已提交
207
Sqrt Activation Operator.
K
Kexin Zhao 已提交
208

F
fengjiayi 已提交
209
$out = \sqrt{x}$
K
Kexin Zhao 已提交
210 211

)DOC");
212 213 214 215 216
  }
};

class AbsOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
217 218
  AbsOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
219
    AddInput("X", "Input of Abs operator");
F
fengjiayi 已提交
220
    AddOutput("Out", "Output of Abs operator");
221 222 223
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
224
    AddComment(R"DOC(
K
kexinzhao 已提交
225
Abs Activation Operator.
K
Kexin Zhao 已提交
226

F
fengjiayi 已提交
227
$out = |x|$
K
Kexin Zhao 已提交
228 229

)DOC");
230 231 232
  }
};

D
dzhwinter 已提交
233 234
class CeilOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
235 236
  CeilOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
237
    AddInput("X", "Input of Ceil operator");
F
fengjiayi 已提交
238
    AddOutput("Out", "Output of Ceil operator");
D
dzhwinter 已提交
239 240 241
    AddComment(R"DOC(
Ceil Activation Operator.

F
fengjiayi 已提交
242
$out = ceil(x)$
D
dzhwinter 已提交
243 244 245 246 247 248 249

)DOC");
  }
};

class FloorOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
250 251
  FloorOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
252
    AddInput("X", "Input of Floor operator");
F
fengjiayi 已提交
253
    AddOutput("Out", "Output of Floor operator");
D
dzhwinter 已提交
254 255 256
    AddComment(R"DOC(
Floor Activation Operator.

F
fengjiayi 已提交
257
$out = floor(x)$
D
dzhwinter 已提交
258 259 260 261 262 263 264

)DOC");
  }
};

class RoundOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
265 266
  RoundOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
267
    AddInput("X", "Input of Round operator");
F
fengjiayi 已提交
268
    AddOutput("Out", "Output of Round operator");
D
dzhwinter 已提交
269 270 271
    AddComment(R"DOC(
Round Activation Operator.

F
fengjiayi 已提交
272
$out = [x]$
D
dzhwinter 已提交
273 274 275 276 277

)DOC");
  }
};

278 279
class ReciprocalOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
280 281
  ReciprocalOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
282
    AddInput("X", "Input of Reciprocal operator");
F
fengjiayi 已提交
283
    AddOutput("Out", "Output of Reciprocal operator");
K
Kexin Zhao 已提交
284
    AddComment(R"DOC(
K
kexinzhao 已提交
285
Reciprocal Activation Operator.
K
Kexin Zhao 已提交
286

F
fengjiayi 已提交
287
$$out = \frac{1}{x}$$
K
Kexin Zhao 已提交
288 289

)DOC");
290 291 292 293 294
  }
};

class LogOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
295 296
  LogOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
297
    AddInput("X", "Input of Log operator");
F
fengjiayi 已提交
298
    AddOutput("Out", "Output of Log operator");
K
Kexin Zhao 已提交
299
    AddComment(R"DOC(
K
kexinzhao 已提交
300
Log Activation Operator.
K
Kexin Zhao 已提交
301

F
fengjiayi 已提交
302
$out = \ln(x)$
K
Kexin Zhao 已提交
303 304 305 306

Natural logarithm of x.

)DOC");
307 308 309 310 311
  }
};

class SquareOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
312 313
  SquareOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
314
    AddInput("X", "Input of Square operator");
F
fengjiayi 已提交
315
    AddOutput("Out", "Output of Square operator");
K
Kexin Zhao 已提交
316
    AddComment(R"DOC(
K
kexinzhao 已提交
317
Square Activation Operator.
K
Kexin Zhao 已提交
318

F
fengjiayi 已提交
319
$out = x^2$
K
Kexin Zhao 已提交
320 321

)DOC");
322 323 324
  }
};

K
kexinzhao 已提交
325 326
class SoftplusOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
327 328
  SoftplusOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
K
kexinzhao 已提交
329
    AddInput("X", "Input of Softplus operator");
F
fengjiayi 已提交
330
    AddOutput("Out", "Output of Softplus operator");
K
Kexin Zhao 已提交
331
    AddComment(R"DOC(
K
kexinzhao 已提交
332
Softplus Activation Operator.
K
Kexin Zhao 已提交
333

F
fengjiayi 已提交
334
$out = \ln(1 + e^{x})$
K
Kexin Zhao 已提交
335 336

)DOC");
K
kexinzhao 已提交
337 338 339
  }
};

340 341
class SoftsignOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
342 343
  SoftsignOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
344
    AddInput("X", "Input of Softsign operator");
F
fengjiayi 已提交
345
    AddOutput("Out", "Output of Softsign operator");
K
Kexin Zhao 已提交
346
    AddComment(R"DOC(
K
kexinzhao 已提交
347
Softsign Activation Operator.
K
Kexin Zhao 已提交
348

F
fengjiayi 已提交
349
$$out = \frac{x}{1 + |x|}$$
K
Kexin Zhao 已提交
350 351

)DOC");
352 353 354
  }
};

355 356
class BReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
357 358
  BReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
359
    AddInput("X", "Input of BRelu operator");
F
fengjiayi 已提交
360
    AddOutput("Out", "Output of BRelu operator");
361 362 363 364
    AddAttr<float>("t_min", "The min marginal value of BRelu")
        .SetDefault(static_cast<float>(0));
    AddAttr<float>("t_max", "The max marginal value of BRelu")
        .SetDefault(static_cast<float>(24));
K
Kexin Zhao 已提交
365
    AddComment(R"DOC(
K
kexinzhao 已提交
366
BRelu Activation Operator.
K
Kexin Zhao 已提交
367

F
fengjiayi 已提交
368
$out = \max(\min(x, t_{min}), t_{max})$
K
Kexin Zhao 已提交
369 370

)DOC");
371 372 373 374 375
  }
};

class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
376 377
  SoftReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
378
    AddInput("X", "Input of SoftRelu operator");
F
fengjiayi 已提交
379
    AddOutput("Out", "Output of SoftRelu operator");
380 381
    AddAttr<float>("threshold", "The threshold value of SoftRelu")
        .SetDefault(40.0f);
K
Kexin Zhao 已提交
382
    AddComment(R"DOC(
K
kexinzhao 已提交
383
SoftRelu Activation Operator.
K
Kexin Zhao 已提交
384

F
fengjiayi 已提交
385
$out = \ln(1 + \exp(\max(\min(x, threshold), threshold))$
K
Kexin Zhao 已提交
386 387

)DOC");
388 389 390
  }
};

391 392
class ELUOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
393 394
  ELUOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
K
Kexin Zhao 已提交
395
    AddInput("X", "Input of ELU operator");
F
fengjiayi 已提交
396
    AddOutput("Out", "Output of ELU operator");
397
    AddAttr<float>("alpha", "The alpha value of ELU").SetDefault(1.0f);
398
    AddComment(R"DOC(
K
kexinzhao 已提交
399
ELU Activation Operator.
K
Kexin Zhao 已提交
400 401 402 403

Applies the following element-wise computation on the input according to
https://arxiv.org/abs/1511.07289.

F
fengjiayi 已提交
404
$out = \max(0, x) + \min(0, \alpha * (e^x - 1))$
K
Kexin Zhao 已提交
405 406

)DOC");
407 408 409
  }
};

410 411
class Relu6OpMaker : public framework::OpProtoAndCheckerMaker {
 public:
412 413
  Relu6OpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
414
    AddInput("X", "Input of Relu6 operator");
F
fengjiayi 已提交
415
    AddOutput("Out", "Output of Relu6 operator");
416 417
    AddAttr<float>("threshold", "The threshold value of Relu6")
        .SetDefault(6.0f);
K
Kexin Zhao 已提交
418
    AddComment(R"DOC(
K
kexinzhao 已提交
419
Relu6 Activation Operator.
K
Kexin Zhao 已提交
420

F
fengjiayi 已提交
421
$out = \min(\max(0, x), 6)$
K
Kexin Zhao 已提交
422 423

)DOC");
424 425 426
  }
};

427 428
class PowOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
429 430
  PowOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
431
    AddInput("X", "Input of Pow operator");
F
fengjiayi 已提交
432
    AddOutput("Out", "Output of Pow operator");
433
    AddAttr<float>("factor", "The exponential factor of Pow").SetDefault(1.0f);
K
Kexin Zhao 已提交
434
    AddComment(R"DOC(
K
kexinzhao 已提交
435
Pow Activation Operator.
K
Kexin Zhao 已提交
436

F
fengjiayi 已提交
437
$out = x^{factor}$
K
Kexin Zhao 已提交
438 439

)DOC");
440 441 442 443 444
  }
};

class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
445 446
  STanhOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
447
    AddInput("X", "Input of STanh operator");
F
fengjiayi 已提交
448
    AddOutput("Out", "Output of STanh operator");
449 450 451 452
    AddAttr<float>("scale_a", "The scale parameter of a for the input")
        .SetDefault(2.0f / 3.0f);
    AddAttr<float>("scale_b", "The scale parameter of b for the input")
        .SetDefault(1.7159f);
K
Kexin Zhao 已提交
453
    AddComment(R"DOC(
K
kexinzhao 已提交
454
STanh Activation Operator.
K
Kexin Zhao 已提交
455

F
fengjiayi 已提交
456
$$out = b * \frac{e^{a * x} - e^{-a * x}}{e^{a * x} + e^{-a * x}}$$
K
Kexin Zhao 已提交
457 458

)DOC");
Q
qijun 已提交
459 460 461
  }
};

462 463
class ThresholdedReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
464 465
  ThresholdedReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
466
    AddInput("X", "Input of ThresholdedRelu operator");
F
fengjiayi 已提交
467
    AddOutput("Out", "Output of ThresholdedRelu operator");
468 469
    AddAttr<float>("threshold", "The threshold location of activation")
        .SetDefault(1.0f);
K
Kexin Zhao 已提交
470
    AddComment(R"DOC(
K
kexinzhao 已提交
471
ThresholdedRelu Activation Operator.
K
Kexin Zhao 已提交
472 473

$$
F
fengjiayi 已提交
474
out = \begin{cases} 
K
Kexin Zhao 已提交
475 476 477 478 479 480
    x, \text{if } x > threshold \\
    0,  \text{otherwise}
    \end{cases}
$$

)DOC");
481 482 483
  }
};

484 485
class HardSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
486 487
  HardSigmoidOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
488
    AddInput("X", "Input of HardSigmoid operator");
F
fengjiayi 已提交
489
    AddOutput("Out", "Output of HardSigmoid operator");
490 491 492 493
    AddAttr<float>("slope", "Slope for linear approximation of sigmoid")
        .SetDefault(0.2f);
    AddAttr<float>("offset", "Offset for linear approximation of sigmoid")
        .SetDefault(0.5f);
494
    AddComment(R"DOC(
K
kexinzhao 已提交
495
HardSigmoid Activation Operator.
496

K
Kexin Zhao 已提交
497 498
Segment-wise linear approximation of sigmoid(https://arxiv.org/abs/1603.00391), 
which is much faster than sigmoid.
499

F
fengjiayi 已提交
500
$out = \max(0, \min(1, slope * x + shift))$
501 502

The slope should be positive. The offset can be either positive or negative.
K
Kexin Zhao 已提交
503
The default slope and shift are set according to the above reference.
504 505
It is recommended to use the defaults for this activation.

K
Kexin Zhao 已提交
506
)DOC");
507 508 509
  }
};

A
Abhinav Arora 已提交
510 511
class SwishOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
512 513
  SwishOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
A
Abhinav Arora 已提交
514
    AddInput("X", "Input of Swish operator");
F
fengjiayi 已提交
515
    AddOutput("Out", "Output of Swish operator");
A
Abhinav Arora 已提交
516 517 518 519
    AddAttr<float>("beta", "Constant beta of swish operator").SetDefault(1.0f);
    AddComment(R"DOC(
Swish Activation Operator.

F
fengjiayi 已提交
520
$$out = \frac{x}{1 + e^{- \beta x}}$$
A
Abhinav Arora 已提交
521 522 523 524 525

)DOC");
  }
};

Q
qijun 已提交
526 527 528 529
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
530

Q
qijun 已提交
531 532 533
REGISTER_OP(sigmoid, ops::ActivationOp, ops::SigmoidOpMaker, sigmoid_grad,
            ops::ActivationOpGrad);

534 535 536
REGISTER_OP(logsigmoid, ops::ActivationOp, ops::LogSigmoidOpMaker,
            logsigmoid_grad, ops::ActivationOpGrad);

Q
qijun 已提交
537 538
REGISTER_OP(exp, ops::ActivationOp, ops::ExpOpMaker, exp_grad,
            ops::ActivationOpGrad);
Q
qijun 已提交
539

K
Krzysztof Binias 已提交
540 541
REGISTER_OP(relu, ops::ActivationWithMKLDNNOp, ops::ReluOpMaker, relu_grad,
            ops::ActivationWithMKLDNNOpGrad);
542

K
Krzysztof Binias 已提交
543 544
REGISTER_OP(tanh, ops::ActivationWithMKLDNNOp, ops::TanhOpMaker, tanh_grad,
            ops::ActivationWithMKLDNNOpGrad);
545

K
Kavya Srinet 已提交
546 547
REGISTER_OP(tanh_shrink, ops::ActivationOp, ops::TanhShrinkOpMaker,
            tanh_shrink_grad, ops::ActivationOpGrad);
548

549
REGISTER_OP(softshrink, ops::ActivationOp, ops::SoftShrinkOpMaker,
550 551
            softshrink_grad, ops::ActivationOpGrad);

K
Krzysztof Binias 已提交
552 553
REGISTER_OP(sqrt, ops::ActivationWithMKLDNNOp, ops::SqrtOpMaker, sqrt_grad,
            ops::ActivationWithMKLDNNOpGrad);
554

K
Krzysztof Binias 已提交
555 556
REGISTER_OP(abs, ops::ActivationWithMKLDNNOp, ops::AbsOpMaker, abs_grad,
            ops::ActivationWithMKLDNNOpGrad);
557

D
dzhwinter 已提交
558 559 560 561 562 563 564 565 566
REGISTER_OP(ceil, ops::ActivationOp, ops::CeilOpMaker, ceil_grad,
            ops::ActivationOpGrad);

REGISTER_OP(floor, ops::ActivationOp, ops::FloorOpMaker, floor_grad,
            ops::ActivationOpGrad);

REGISTER_OP(round, ops::ActivationOp, ops::RoundOpMaker, round_grad,
            ops::ActivationOpGrad);

567 568 569 570 571 572 573 574 575
REGISTER_OP(reciprocal, ops::ActivationOp, ops::ReciprocalOpMaker,
            reciprocal_grad, ops::ActivationOpGrad);

REGISTER_OP(log, ops::ActivationOp, ops::LogOpMaker, log_grad,
            ops::ActivationOpGrad);

REGISTER_OP(square, ops::ActivationOp, ops::SquareOpMaker, square_grad,
            ops::ActivationOpGrad);

K
kexinzhao 已提交
576 577 578
REGISTER_OP(softplus, ops::ActivationOp, ops::SoftplusOpMaker, softplus_grad,
            ops::ActivationOpGrad);

579 580 581
REGISTER_OP(softsign, ops::ActivationOp, ops::SoftsignOpMaker, softsign_grad,
            ops::ActivationOpGrad);

582
REGISTER_OP(brelu, ops::ActivationOp, ops::BReluOpMaker, brelu_grad,
583 584
            ops::ActivationOpGrad);

585
REGISTER_OP(leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker,
K
Kavya Srinet 已提交
586
            leaky_relu_grad, ops::ActivationOpGrad);
587

588 589
REGISTER_OP(soft_relu, ops::ActivationOp, ops::SoftReluOpMaker, soft_relu_grad,
            ops::ActivationOpGrad);
590

591
REGISTER_OP(elu, ops::ActivationOp, ops::ELUOpMaker, elu_grad,
592 593
            ops::ActivationOpGrad);

594
REGISTER_OP(relu6, ops::ActivationOp, ops::Relu6OpMaker, relu6_grad,
595 596
            ops::ActivationOpGrad);

597
REGISTER_OP(pow, ops::ActivationOp, ops::PowOpMaker, pow_grad,
598 599
            ops::ActivationOpGrad);

600
REGISTER_OP(stanh, ops::ActivationOp, ops::STanhOpMaker, stanh_grad,
601
            ops::ActivationOpGrad);
602

603
REGISTER_OP(hard_shrink, ops::ActivationOp, ops::HardShrinkOpMaker,
604 605
            hard_shrink_grad, ops::ActivationOpGrad);

606 607
REGISTER_OP(thresholded_relu, ops::ActivationOp, ops::ThresholdedReluOpMaker,
            thresholded_relu_grad, ops::ActivationOpGrad);
608

609
REGISTER_OP(hard_sigmoid, ops::ActivationOp, ops::HardSigmoidOpMaker,
610 611
            hard_sigmoid_grad, ops::ActivationOpGrad);

A
Abhinav Arora 已提交
612 613 614
REGISTER_OP(swish, ops::ActivationOp, ops::SwishOpMaker, swish_grad,
            ops::ActivationOpGrad);

Q
QI JUN 已提交
615 616 617 618 619 620 621 622 623 624 625
#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, functor, grad_functor)   \
  REGISTER_OP_CPU_KERNEL(                                                 \
      act_type, ops::ActivationKernel<paddle::platform::CPUDeviceContext, \
                                      ops::functor<float>>,               \
      ops::ActivationKernel<paddle::platform::CPUDeviceContext,           \
                            ops::functor<double>>);                       \
  REGISTER_OP_CPU_KERNEL(                                                 \
      act_type##_grad,                                                    \
      ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,       \
                                ops::grad_functor<float>>,                \
      ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,       \
Y
Yu Yang 已提交
626
                                ops::grad_functor<double>>);
627 628

FOR_EACH_KERNEL_FUNCTOR(REGISTER_ACTIVATION_CPU_KERNEL);
K
Kexin Zhao 已提交
629 630 631 632 633 634 635 636 637 638 639

REGISTER_OP_CPU_KERNEL(relu,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::ReluFunctor<float>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::ReluFunctor<double>>);
REGISTER_OP_CPU_KERNEL(
    relu_grad, ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                                         ops::ReluGradFunctor<float>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::ReluGradFunctor<double>>);