activation_op.cc 22.0 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

)DOC");
  }
};

C
add cos  
chengduoZH 已提交
263 264 265 266
class CosOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  CosOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
C
add sin  
chengduoZH 已提交
267 268
    AddInput("X", "Input of Cosine operator");
    AddOutput("Out", "Output of Cosine operator");
C
add cos  
chengduoZH 已提交
269
    AddComment(R"DOC(
C
add sin  
chengduoZH 已提交
270
Cosine Activation Operator.
C
add cos  
chengduoZH 已提交
271 272 273 274 275 276 277

$out = cos(x)$

)DOC");
  }
};

C
add sin  
chengduoZH 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
class SinOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  SinOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "Input of Sine operator");
    AddOutput("Out", "Output of Sine operator");
    AddComment(R"DOC(
Sine Activation Operator.

$out = sin(x)$

)DOC");
  }
};

D
dzhwinter 已提交
293 294
class RoundOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
295 296
  RoundOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
297
    AddInput("X", "Input of Round operator");
F
fengjiayi 已提交
298
    AddOutput("Out", "Output of Round operator");
D
dzhwinter 已提交
299 300 301
    AddComment(R"DOC(
Round Activation Operator.

F
fengjiayi 已提交
302
$out = [x]$
D
dzhwinter 已提交
303 304 305 306 307

)DOC");
  }
};

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

F
fengjiayi 已提交
317
$$out = \frac{1}{x}$$
K
Kexin Zhao 已提交
318 319

)DOC");
320 321 322 323 324
  }
};

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

F
fengjiayi 已提交
332
$out = \ln(x)$
K
Kexin Zhao 已提交
333 334 335 336

Natural logarithm of x.

)DOC");
337 338 339 340 341
  }
};

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

F
fengjiayi 已提交
349
$out = x^2$
K
Kexin Zhao 已提交
350 351

)DOC");
352 353 354
  }
};

K
kexinzhao 已提交
355 356
class SoftplusOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
357 358
  SoftplusOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
K
kexinzhao 已提交
359
    AddInput("X", "Input of Softplus operator");
F
fengjiayi 已提交
360
    AddOutput("Out", "Output of Softplus operator");
K
Kexin Zhao 已提交
361
    AddComment(R"DOC(
K
kexinzhao 已提交
362
Softplus Activation Operator.
K
Kexin Zhao 已提交
363

F
fengjiayi 已提交
364
$out = \ln(1 + e^{x})$
K
Kexin Zhao 已提交
365 366

)DOC");
K
kexinzhao 已提交
367 368 369
  }
};

370 371
class SoftsignOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
372 373
  SoftsignOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
374
    AddInput("X", "Input of Softsign operator");
F
fengjiayi 已提交
375
    AddOutput("Out", "Output of Softsign operator");
K
Kexin Zhao 已提交
376
    AddComment(R"DOC(
K
kexinzhao 已提交
377
Softsign Activation Operator.
K
Kexin Zhao 已提交
378

F
fengjiayi 已提交
379
$$out = \frac{x}{1 + |x|}$$
K
Kexin Zhao 已提交
380 381

)DOC");
382 383 384
  }
};

385 386
class BReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
387 388
  BReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
389
    AddInput("X", "Input of BRelu operator");
F
fengjiayi 已提交
390
    AddOutput("Out", "Output of BRelu operator");
391 392 393 394
    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 已提交
395
    AddComment(R"DOC(
K
kexinzhao 已提交
396
BRelu Activation Operator.
K
Kexin Zhao 已提交
397

F
fengjiayi 已提交
398
$out = \max(\min(x, t_{min}), t_{max})$
K
Kexin Zhao 已提交
399 400

)DOC");
401 402 403 404 405
  }
};

class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
406 407
  SoftReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
408
    AddInput("X", "Input of SoftRelu operator");
F
fengjiayi 已提交
409
    AddOutput("Out", "Output of SoftRelu operator");
410 411
    AddAttr<float>("threshold", "The threshold value of SoftRelu")
        .SetDefault(40.0f);
K
Kexin Zhao 已提交
412
    AddComment(R"DOC(
K
kexinzhao 已提交
413
SoftRelu Activation Operator.
K
Kexin Zhao 已提交
414

F
fengjiayi 已提交
415
$out = \ln(1 + \exp(\max(\min(x, threshold), threshold))$
K
Kexin Zhao 已提交
416 417

)DOC");
418 419 420
  }
};

421 422
class ELUOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
423 424
  ELUOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
K
Kexin Zhao 已提交
425
    AddInput("X", "Input of ELU operator");
F
fengjiayi 已提交
426
    AddOutput("Out", "Output of ELU operator");
427
    AddAttr<float>("alpha", "The alpha value of ELU").SetDefault(1.0f);
428
    AddComment(R"DOC(
K
kexinzhao 已提交
429
ELU Activation Operator.
K
Kexin Zhao 已提交
430 431 432 433

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

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

)DOC");
437 438 439
  }
};

440 441
class Relu6OpMaker : public framework::OpProtoAndCheckerMaker {
 public:
442 443
  Relu6OpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
444
    AddInput("X", "Input of Relu6 operator");
F
fengjiayi 已提交
445
    AddOutput("Out", "Output of Relu6 operator");
446 447
    AddAttr<float>("threshold", "The threshold value of Relu6")
        .SetDefault(6.0f);
K
Kexin Zhao 已提交
448
    AddComment(R"DOC(
K
kexinzhao 已提交
449
Relu6 Activation Operator.
K
Kexin Zhao 已提交
450

F
fengjiayi 已提交
451
$out = \min(\max(0, x), 6)$
K
Kexin Zhao 已提交
452 453

)DOC");
454 455 456
  }
};

457 458
class PowOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
459 460
  PowOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
461
    AddInput("X", "Input of Pow operator");
F
fengjiayi 已提交
462
    AddOutput("Out", "Output of Pow operator");
463
    AddAttr<float>("factor", "The exponential factor of Pow").SetDefault(1.0f);
K
Kexin Zhao 已提交
464
    AddComment(R"DOC(
K
kexinzhao 已提交
465
Pow Activation Operator.
K
Kexin Zhao 已提交
466

F
fengjiayi 已提交
467
$out = x^{factor}$
K
Kexin Zhao 已提交
468 469

)DOC");
470 471 472 473 474
  }
};

class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
475 476
  STanhOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
477
    AddInput("X", "Input of STanh operator");
F
fengjiayi 已提交
478
    AddOutput("Out", "Output of STanh operator");
479 480 481 482
    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 已提交
483
    AddComment(R"DOC(
K
kexinzhao 已提交
484
STanh Activation Operator.
K
Kexin Zhao 已提交
485

F
fengjiayi 已提交
486
$$out = b * \frac{e^{a * x} - e^{-a * x}}{e^{a * x} + e^{-a * x}}$$
K
Kexin Zhao 已提交
487 488

)DOC");
Q
qijun 已提交
489 490 491
  }
};

492 493
class ThresholdedReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
494 495
  ThresholdedReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
496
    AddInput("X", "Input of ThresholdedRelu operator");
F
fengjiayi 已提交
497
    AddOutput("Out", "Output of ThresholdedRelu operator");
498 499
    AddAttr<float>("threshold", "The threshold location of activation")
        .SetDefault(1.0f);
K
Kexin Zhao 已提交
500
    AddComment(R"DOC(
K
kexinzhao 已提交
501
ThresholdedRelu Activation Operator.
K
Kexin Zhao 已提交
502 503

$$
F
fengjiayi 已提交
504
out = \begin{cases} 
K
Kexin Zhao 已提交
505 506 507 508 509 510
    x, \text{if } x > threshold \\
    0,  \text{otherwise}
    \end{cases}
$$

)DOC");
511 512 513
  }
};

514 515
class HardSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
516 517
  HardSigmoidOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
518
    AddInput("X", "Input of HardSigmoid operator");
F
fengjiayi 已提交
519
    AddOutput("Out", "Output of HardSigmoid operator");
520 521 522 523
    AddAttr<float>("slope", "Slope for linear approximation of sigmoid")
        .SetDefault(0.2f);
    AddAttr<float>("offset", "Offset for linear approximation of sigmoid")
        .SetDefault(0.5f);
524
    AddComment(R"DOC(
K
kexinzhao 已提交
525
HardSigmoid Activation Operator.
526

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

F
fengjiayi 已提交
530
$out = \max(0, \min(1, slope * x + shift))$
531 532

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

K
Kexin Zhao 已提交
536
)DOC");
537 538 539
  }
};

A
Abhinav Arora 已提交
540 541
class SwishOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
542 543
  SwishOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
A
Abhinav Arora 已提交
544
    AddInput("X", "Input of Swish operator");
F
fengjiayi 已提交
545
    AddOutput("Out", "Output of Swish operator");
A
Abhinav Arora 已提交
546 547 548 549
    AddAttr<float>("beta", "Constant beta of swish operator").SetDefault(1.0f);
    AddComment(R"DOC(
Swish Activation Operator.

F
fengjiayi 已提交
550
$$out = \frac{x}{1 + e^{- \beta x}}$$
A
Abhinav Arora 已提交
551 552 553 554 555

)DOC");
  }
};

Q
qijun 已提交
556 557 558 559
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
560

Y
Yang Yang 已提交
561 562 563
REGISTER_OPERATOR(sigmoid, ops::ActivationOp, ops::SigmoidOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(sigmoid_grad, ops::ActivationOpGrad)
Q
qijun 已提交
564

Y
Yang Yang 已提交
565 566 567
REGISTER_OPERATOR(logsigmoid, ops::ActivationOp, ops::LogSigmoidOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(logsigmoid_grad, ops::ActivationOpGrad)
568

Y
Yang Yang 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
REGISTER_OPERATOR(exp, ops::ActivationOp, ops::ExpOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(exp_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(relu, ops::ActivationWithMKLDNNOp, ops::ReluOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(relu_grad, ops::ActivationWithMKLDNNOpGrad)

REGISTER_OPERATOR(tanh, ops::ActivationWithMKLDNNOp, ops::TanhOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(tanh_grad, ops::ActivationWithMKLDNNOpGrad)

REGISTER_OPERATOR(tanh_shrink, ops::ActivationOp, ops::TanhShrinkOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(tanh_shrink_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(softshrink, ops::ActivationOp, ops::SoftShrinkOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(softshrink_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(sqrt, ops::ActivationWithMKLDNNOp, ops::SqrtOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(sqrt_grad, ops::ActivationWithMKLDNNOpGrad)

REGISTER_OPERATOR(abs, ops::ActivationWithMKLDNNOp, ops::AbsOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(abs_grad, ops::ActivationWithMKLDNNOpGrad)

REGISTER_OPERATOR(ceil, ops::ActivationOp, ops::CeilOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(ceil_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(floor, ops::ActivationOp, ops::FloorOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(floor_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(cos, ops::ActivationOp, ops::CosOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(cos_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(sin, ops::ActivationOp, ops::SinOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(sin_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(round, ops::ActivationOp, ops::RoundOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(round_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(reciprocal, ops::ActivationOp, ops::ReciprocalOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(reciprocal_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(log, ops::ActivationOp, ops::LogOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(log_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(square, ops::ActivationOp, ops::SquareOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(square_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(softplus, ops::ActivationOp, ops::SoftplusOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(softplus_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(softsign, ops::ActivationOp, ops::SoftsignOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(softsign_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(brelu, ops::ActivationOp, ops::BReluOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(brelu_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(leaky_relu_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(soft_relu, ops::ActivationOp, ops::SoftReluOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(soft_relu_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(elu, ops::ActivationOp, ops::ELUOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(elu_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(relu6, ops::ActivationOp, ops::Relu6OpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(relu6_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(pow, ops::ActivationOp, ops::PowOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(pow_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(stanh, ops::ActivationOp, ops::STanhOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(stanh_grad, ops::ActivationOpGrad)
Q
qijun 已提交
664

Y
Yang Yang 已提交
665 666 667
REGISTER_OPERATOR(hard_shrink, ops::ActivationOp, ops::HardShrinkOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(hard_shrink_grad, ops::ActivationOpGrad)
668

Y
Yang Yang 已提交
669 670 671 672 673 674 675 676 677 678 679 680
REGISTER_OPERATOR(thresholded_relu, ops::ActivationOp,
                  ops::ThresholdedReluOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(thresholded_relu_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(hard_sigmoid, ops::ActivationOp, ops::HardSigmoidOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(hard_sigmoid_grad, ops::ActivationOpGrad)

REGISTER_OPERATOR(swish, ops::ActivationOp, ops::SwishOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>)
REGISTER_OPERATOR(swish_grad, ops::ActivationOpGrad)
A
Abhinav Arora 已提交
681

Q
QI JUN 已提交
682 683 684 685 686 687 688 689 690 691 692
#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 已提交
693
                                ops::grad_functor<double>>);
694 695

FOR_EACH_KERNEL_FUNCTOR(REGISTER_ACTIVATION_CPU_KERNEL);