activation_op.cc 19.9 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 267 268 269 270 271 272 273 274 275 276 277
class CosOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  CosOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "Input of Floor operator");
    AddOutput("Out", "Output of Floor operator");
    AddComment(R"DOC(
Floor Activation Operator.

$out = cos(x)$

)DOC");
  }
};

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

F
fengjiayi 已提交
287
$out = [x]$
D
dzhwinter 已提交
288 289 290 291 292

)DOC");
  }
};

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

F
fengjiayi 已提交
302
$$out = \frac{1}{x}$$
K
Kexin Zhao 已提交
303 304

)DOC");
305 306 307 308 309
  }
};

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

F
fengjiayi 已提交
317
$out = \ln(x)$
K
Kexin Zhao 已提交
318 319 320 321

Natural logarithm of x.

)DOC");
322 323 324 325 326
  }
};

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

F
fengjiayi 已提交
334
$out = x^2$
K
Kexin Zhao 已提交
335 336

)DOC");
337 338 339
  }
};

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

F
fengjiayi 已提交
349
$out = \ln(1 + e^{x})$
K
Kexin Zhao 已提交
350 351

)DOC");
K
kexinzhao 已提交
352 353 354
  }
};

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

F
fengjiayi 已提交
364
$$out = \frac{x}{1 + |x|}$$
K
Kexin Zhao 已提交
365 366

)DOC");
367 368 369
  }
};

370 371
class BReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
372 373
  BReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
374
    AddInput("X", "Input of BRelu operator");
F
fengjiayi 已提交
375
    AddOutput("Out", "Output of BRelu operator");
376 377 378 379
    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 已提交
380
    AddComment(R"DOC(
K
kexinzhao 已提交
381
BRelu Activation Operator.
K
Kexin Zhao 已提交
382

F
fengjiayi 已提交
383
$out = \max(\min(x, t_{min}), t_{max})$
K
Kexin Zhao 已提交
384 385

)DOC");
386 387 388 389 390
  }
};

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

F
fengjiayi 已提交
400
$out = \ln(1 + \exp(\max(\min(x, threshold), threshold))$
K
Kexin Zhao 已提交
401 402

)DOC");
403 404 405
  }
};

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

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

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

)DOC");
422 423 424
  }
};

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

F
fengjiayi 已提交
436
$out = \min(\max(0, x), 6)$
K
Kexin Zhao 已提交
437 438

)DOC");
439 440 441
  }
};

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

F
fengjiayi 已提交
452
$out = x^{factor}$
K
Kexin Zhao 已提交
453 454

)DOC");
455 456 457 458 459
  }
};

class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
460 461
  STanhOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
462
    AddInput("X", "Input of STanh operator");
F
fengjiayi 已提交
463
    AddOutput("Out", "Output of STanh operator");
464 465 466 467
    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 已提交
468
    AddComment(R"DOC(
K
kexinzhao 已提交
469
STanh Activation Operator.
K
Kexin Zhao 已提交
470

F
fengjiayi 已提交
471
$$out = b * \frac{e^{a * x} - e^{-a * x}}{e^{a * x} + e^{-a * x}}$$
K
Kexin Zhao 已提交
472 473

)DOC");
Q
qijun 已提交
474 475 476
  }
};

477 478
class ThresholdedReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
479 480
  ThresholdedReluOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
481
    AddInput("X", "Input of ThresholdedRelu operator");
F
fengjiayi 已提交
482
    AddOutput("Out", "Output of ThresholdedRelu operator");
483 484
    AddAttr<float>("threshold", "The threshold location of activation")
        .SetDefault(1.0f);
K
Kexin Zhao 已提交
485
    AddComment(R"DOC(
K
kexinzhao 已提交
486
ThresholdedRelu Activation Operator.
K
Kexin Zhao 已提交
487 488

$$
F
fengjiayi 已提交
489
out = \begin{cases} 
K
Kexin Zhao 已提交
490 491 492 493 494 495
    x, \text{if } x > threshold \\
    0,  \text{otherwise}
    \end{cases}
$$

)DOC");
496 497 498
  }
};

499 500
class HardSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
501 502
  HardSigmoidOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
503
    AddInput("X", "Input of HardSigmoid operator");
F
fengjiayi 已提交
504
    AddOutput("Out", "Output of HardSigmoid operator");
505 506 507 508
    AddAttr<float>("slope", "Slope for linear approximation of sigmoid")
        .SetDefault(0.2f);
    AddAttr<float>("offset", "Offset for linear approximation of sigmoid")
        .SetDefault(0.5f);
509
    AddComment(R"DOC(
K
kexinzhao 已提交
510
HardSigmoid Activation Operator.
511

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

F
fengjiayi 已提交
515
$out = \max(0, \min(1, slope * x + shift))$
516 517

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

K
Kexin Zhao 已提交
521
)DOC");
522 523 524
  }
};

A
Abhinav Arora 已提交
525 526
class SwishOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
527 528
  SwishOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
A
Abhinav Arora 已提交
529
    AddInput("X", "Input of Swish operator");
F
fengjiayi 已提交
530
    AddOutput("Out", "Output of Swish operator");
A
Abhinav Arora 已提交
531 532 533 534
    AddAttr<float>("beta", "Constant beta of swish operator").SetDefault(1.0f);
    AddComment(R"DOC(
Swish Activation Operator.

F
fengjiayi 已提交
535
$$out = \frac{x}{1 + e^{- \beta x}}$$
A
Abhinav Arora 已提交
536 537 538 539 540

)DOC");
  }
};

Q
qijun 已提交
541 542 543 544
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
545

Q
qijun 已提交
546 547 548
REGISTER_OP(sigmoid, ops::ActivationOp, ops::SigmoidOpMaker, sigmoid_grad,
            ops::ActivationOpGrad);

549 550 551
REGISTER_OP(logsigmoid, ops::ActivationOp, ops::LogSigmoidOpMaker,
            logsigmoid_grad, ops::ActivationOpGrad);

Q
qijun 已提交
552 553
REGISTER_OP(exp, ops::ActivationOp, ops::ExpOpMaker, exp_grad,
            ops::ActivationOpGrad);
Q
qijun 已提交
554

K
Krzysztof Binias 已提交
555 556
REGISTER_OP(relu, ops::ActivationWithMKLDNNOp, ops::ReluOpMaker, relu_grad,
            ops::ActivationWithMKLDNNOpGrad);
557

K
Krzysztof Binias 已提交
558 559
REGISTER_OP(tanh, ops::ActivationWithMKLDNNOp, ops::TanhOpMaker, tanh_grad,
            ops::ActivationWithMKLDNNOpGrad);
560

K
Kavya Srinet 已提交
561 562
REGISTER_OP(tanh_shrink, ops::ActivationOp, ops::TanhShrinkOpMaker,
            tanh_shrink_grad, ops::ActivationOpGrad);
563

564
REGISTER_OP(softshrink, ops::ActivationOp, ops::SoftShrinkOpMaker,
565 566
            softshrink_grad, ops::ActivationOpGrad);

K
Krzysztof Binias 已提交
567 568
REGISTER_OP(sqrt, ops::ActivationWithMKLDNNOp, ops::SqrtOpMaker, sqrt_grad,
            ops::ActivationWithMKLDNNOpGrad);
569

K
Krzysztof Binias 已提交
570 571
REGISTER_OP(abs, ops::ActivationWithMKLDNNOp, ops::AbsOpMaker, abs_grad,
            ops::ActivationWithMKLDNNOpGrad);
572

D
dzhwinter 已提交
573 574 575 576 577 578
REGISTER_OP(ceil, ops::ActivationOp, ops::CeilOpMaker, ceil_grad,
            ops::ActivationOpGrad);

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

C
add cos  
chengduoZH 已提交
579 580 581
REGISTER_OP(cos, ops::ActivationOp, ops::CosOpMaker, cos_grad,
            ops::ActivationOpGrad);

D
dzhwinter 已提交
582 583 584
REGISTER_OP(round, ops::ActivationOp, ops::RoundOpMaker, round_grad,
            ops::ActivationOpGrad);

585 586 587 588 589 590 591 592 593
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 已提交
594 595 596
REGISTER_OP(softplus, ops::ActivationOp, ops::SoftplusOpMaker, softplus_grad,
            ops::ActivationOpGrad);

597 598 599
REGISTER_OP(softsign, ops::ActivationOp, ops::SoftsignOpMaker, softsign_grad,
            ops::ActivationOpGrad);

600
REGISTER_OP(brelu, ops::ActivationOp, ops::BReluOpMaker, brelu_grad,
601 602
            ops::ActivationOpGrad);

603
REGISTER_OP(leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker,
K
Kavya Srinet 已提交
604
            leaky_relu_grad, ops::ActivationOpGrad);
605

606 607
REGISTER_OP(soft_relu, ops::ActivationOp, ops::SoftReluOpMaker, soft_relu_grad,
            ops::ActivationOpGrad);
608

609
REGISTER_OP(elu, ops::ActivationOp, ops::ELUOpMaker, elu_grad,
610 611
            ops::ActivationOpGrad);

612
REGISTER_OP(relu6, ops::ActivationOp, ops::Relu6OpMaker, relu6_grad,
613 614
            ops::ActivationOpGrad);

615
REGISTER_OP(pow, ops::ActivationOp, ops::PowOpMaker, pow_grad,
616 617
            ops::ActivationOpGrad);

618
REGISTER_OP(stanh, ops::ActivationOp, ops::STanhOpMaker, stanh_grad,
619
            ops::ActivationOpGrad);
620

621
REGISTER_OP(hard_shrink, ops::ActivationOp, ops::HardShrinkOpMaker,
622 623
            hard_shrink_grad, ops::ActivationOpGrad);

624 625
REGISTER_OP(thresholded_relu, ops::ActivationOp, ops::ThresholdedReluOpMaker,
            thresholded_relu_grad, ops::ActivationOpGrad);
626

627
REGISTER_OP(hard_sigmoid, ops::ActivationOp, ops::HardSigmoidOpMaker,
628 629
            hard_sigmoid_grad, ops::ActivationOpGrad);

A
Abhinav Arora 已提交
630 631 632
REGISTER_OP(swish, ops::ActivationOp, ops::SwishOpMaker, swish_grad,
            ops::ActivationOpGrad);

Q
QI JUN 已提交
633 634 635 636 637 638 639 640 641 642 643
#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 已提交
644
                                ops::grad_functor<double>>);
645 646

FOR_EACH_KERNEL_FUNCTOR(REGISTER_ACTIVATION_CPU_KERNEL);
K
Kexin Zhao 已提交
647 648 649 650 651 652 653 654 655 656 657

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>>);