Caffe2 - C++ API
A deep learning, cross platform ML framework
selu_op.cc
1 #include "caffe2/operators/selu_op.h"
2 
3 #include "caffe2/utils/math.h"
4 
5 namespace caffe2 {
6 
7 template <>
8 bool SeluOp<float, CPUContext>::RunOnDevice() {
9  auto& X = Input(0);
10  auto* Y = Output(0);
11  Y->ResizeLike(X);
12 
13  ConstEigenVectorArrayMap<float> Xvec(X.data<float>(), X.size());
14  EigenVectorArrayMap<float> Yvec(Y->mutable_data<float>(), Y->size());
15  Yvec = lambda_ * (Xvec > 0).select(Xvec, (alpha_ * Xvec.exp() - alpha_));
16  return true;
17 }
18 
19 template <>
20 bool SeluGradientOp<float, CPUContext>::RunOnDevice() {
21  auto& Y = Input(0);
22  auto& dY = Input(1);
23  auto* dX = Output(0);
24  CAFFE_ENFORCE_EQ(dY.size(), Y.size());
25  dX->ResizeLike(Y);
26 
27  ConstEigenVectorArrayMap<float> Yvec(Y.data<float>(), Y.size());
28  ConstEigenVectorArrayMap<float> dYvec(dY.data<float>(), dY.size());
29  EigenVectorArrayMap<float> dXvec(dX->mutable_data<float>(), dX->size());
30 
31  const float la = lambda_ * alpha_;
32  dXvec = (Yvec > 0).select(lambda_ * dYvec, dYvec * (Yvec + la));
33  return true;
34 }
35 
36 REGISTER_CPU_OPERATOR(Selu, SeluOp<float, CPUContext>);
37 REGISTER_CPU_OPERATOR(SeluGradient, SeluGradientOp<float, CPUContext>);
38 
39 // Input: X; output: Y
40 OPERATOR_SCHEMA(Selu)
41  .NumInputs(1)
42  .NumOutputs(1)
43  .AllowInplace({{0, 0}})
44  .IdenticalTypeAndShape()
45  .SetDoc(R"DOC(
46 Selu takes one input data (Tensor<T>) and produces one output data
47 (Tensor<T>) where the function, y = scale*(alpha_*e^x-alpha_ if x < 0 else x),
48 is applied to the tensor elementwise.
49 )DOC")
50  .Arg(
51  "alpha",
52  "(float) default to 1.6732~; affects the activation function itself. "
53  "This should go with the weight initialization in the paper. "
54  " See https://arxiv.org/abs/1706.02515 ")
55  .Arg(
56  "scale",
57  "(float) default to 1.0507~; affects the activation function itself.")
58  .Input(0, "X", "input tensor")
59  .Output(0, "Y", "input tensor")
60  .InheritOnnxSchema("Selu");
61 
62 // Input: Y, dY; output: dX
63 OPERATOR_SCHEMA(SeluGradient)
64  .NumInputs(2)
65  .NumOutputs(1)
66  .AllowInplace({{1, 0}})
67  .SetDoc(R"DOC(
68 SeluGradient takes both Y and dY and uses this to update dX according to the
69 chain rule and derivatives of the selu function.
70 )DOC")
71  .Arg(
72  "alpha",
73  "(float) default to 1.6732~; affects the activation function itself."
74  "This should go with the weight initialization in the paper. "
75  " See https://arxiv.org/abs/1706.02515 ")
76  .Arg(
77  "scale",
78  "(float) default to 1.0507~; affects the activation function itself.")
79  .Input(0, "Y", "input tensor")
80  .Input(1, "dY", "input tensor");
81 
82 class GetSeluGradient : public GradientMakerBase {
83  using GradientMakerBase::GradientMakerBase;
84  vector<OperatorDef> GetGradientDefs() override {
85  return SingleGradientDef(
86  def_.type() + "Gradient",
87  "",
88  vector<string>{O(0), GO(0)},
89  vector<string>{GI(0)});
90  }
91 };
92 REGISTER_GRADIENT(Selu, GetSeluGradient);
93 
94 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...