Caffe2 - C++ API
A deep learning, cross platform ML framework
elu_op.cc
1 
17 #include "caffe2/operators/elu_op.h"
18 
19 #include "caffe2/utils/math.h"
20 
21 namespace caffe2 {
22 
23 template <>
24 bool EluOp<float, CPUContext>::RunOnDevice() {
25  auto& X = Input(0);
26  auto* Y = Output(0);
27  // Otherwise inplace gradient and Elu dosen't make sense.
28  CAFFE_ENFORCE_GE(alpha_, 0);
29  Y->ResizeLike(X);
30  const auto* Xdata = X.template data<float>();
31  auto* Ydata = Y->template mutable_data<float>();
32  ConstEigenVectorArrayMap<float> Xvec(Xdata, X.size());
33  EigenVectorArrayMap<float> Yvec(Ydata, Y->size());
34  Yvec = Xvec.cwiseMax(0.f) + (alpha_ * (Xvec.exp() - 1.0f)).cwiseMin(0.f);
35  return true;
36 }
37 
38 template <>
39 bool EluGradientOp<float, CPUContext>::RunOnDevice() {
40  auto& Y = Input(0);
41  auto& dY = Input(1);
42  auto* dX = Output(0);
43  DCHECK_GT(Y.size(), 0);
44  DCHECK_EQ(dY.size(), Y.size());
45  dX->ResizeLike(Y);
46 
47  const float* Ydata = Y.data<float>();
48  const float* dYdata = dY.data<float>();
49  float* dXdata = dX->mutable_data<float>();
50  ConstEigenVectorArrayMap<float> Yvec(Ydata, Y.size());
51  ConstEigenVectorArrayMap<float> dYvec(dYdata, dY.size());
52  EigenVectorArrayMap<float> dXvec(dXdata, dX->size());
53  dXvec = (Yvec > 0).select(dYvec, dYvec * (Yvec + alpha_));
54  return true;
55 }
56 
57 REGISTER_CPU_OPERATOR(Elu, EluOp<float, CPUContext>);
58 REGISTER_CPU_OPERATOR(EluGradient, EluGradientOp<float, CPUContext>);
59 
60 // Input: X, output: Y
61 OPERATOR_SCHEMA(Elu)
62  .NumInputs(1)
63  .NumOutputs(1)
64  .AllowInplace({{0, 0}})
65  .IdenticalTypeAndShape()
66  .SetDoc(R"DOC(
67 
68 Elu takes one input data (Tensor<T>) and produces one output data
69 (Tensor<T>) where the function `f(x) = alpha * (exp(x) - 1.) for x <
70 0`, `f(x) = x for x >= 0`., is applied to the tensor elementwise.
71 
72 )DOC")
73  .Input(0, "X", "1D input tensor")
74  .Output(0, "Y", "1D input tensor");
75 
76 // Input: Y, dY, output: dX
77 OPERATOR_SCHEMA(EluGradient)
78  .NumInputs(2)
79  .NumOutputs(1)
80  .AllowInplace({{1, 0}})
81  .SetDoc(R"DOC(
82 EluGradient takes both Y and dY and uses this to update dX according to the
83 chain rule and derivatives of the rectified linear function.
84 )DOC");
85 
86 class GetEluGradient : public GradientMakerBase {
87  using GradientMakerBase::GradientMakerBase;
88  vector<OperatorDef> GetGradientDefs() override {
89  return SingleGradientDef(
90  def_.type() + "Gradient",
91  "",
92  vector<string>{O(0), GO(0)},
93  vector<string>{GI(0)});
94  }
95 };
96 REGISTER_GRADIENT(Elu, GetEluGradient);
97 
98 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.
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 ...