Caffe2 - C++ API
A deep learning, cross platform ML framework
leaky_relu_op.cc
1 #include "caffe2/operators/leaky_relu_op.h"
2 
3 #include "caffe2/utils/math.h"
4 
5 namespace caffe2 {
6 
7 template <>
8 bool LeakyReluOp<float, CPUContext>::RunOnDevice() {
9  const auto& X = Input(0);
10  auto* Y = Output(0);
11  Y->ResizeLike(X);
12  ConstEigenVectorMap<float> Xvec(X.template data<float>(), X.size());
13  EigenVectorMap<float> Yvec(Y->template mutable_data<float>(), Y->size());
14  Yvec = Xvec.cwiseMax(0.f) + Xvec.cwiseMin(0.f) * alpha_;
15  return true;
16 }
17 
18 template <>
19 bool LeakyReluGradientOp<float, CPUContext>::RunOnDevice() {
20  const auto& Y = Input(0);
21  const auto& dY = Input(1);
22  auto* dX = Output(0);
23  dX->ResizeLike(Y);
24  CAFFE_ENFORCE_EQ(Y.size(), dY.size());
25  ConstEigenVectorMap<float> Yvec(Y.template data<float>(), Y.size());
26  ConstEigenVectorMap<float> dYvec(dY.template data<float>(), dY.size());
27  EigenVectorMap<float> dXvec(dX->template mutable_data<float>(), dX->size());
28  Eigen::VectorXf gtZero = (Yvec.array() >= 0.0f).cast<float>();
29  dXvec = dYvec.array() * gtZero.array() -
30  dYvec.array() * (gtZero.array() - 1.0f) * alpha_;
31  return true;
32 }
33 
34 REGISTER_CPU_OPERATOR(LeakyRelu, LeakyReluOp<float, CPUContext>);
35 REGISTER_CPU_OPERATOR(
36  LeakyReluGradient,
37  LeakyReluGradientOp<float, CPUContext>);
38 
39 OPERATOR_SCHEMA(LeakyRelu)
40  .NumInputs(1)
41  .NumOutputs(1)
42  .Arg("alpha", "Coefficient of leakage, default value is 0.01")
43  .AllowInplace({{0, 0}})
44  .CostInferenceFunction(PointwiseCostInference<2>)
45  .IdenticalTypeAndShape()
46  .SetDoc(R"DOC(
47 LeakyRelu takes input data (Tensor<T>) and an argument alpha, and produces one
48 output data (Tensor<T>) where the function `f(x) = alpha * x for x < 0`,
49 `f(x) = x for x >= 0`, is applied to the data tensor elementwise.
50 )DOC")
51  .Input(0, "X", "1D input tensor")
52  .Output(0, "Y", "1D input tensor");
53 OPERATOR_SCHEMA(LeakyReluGradient)
54  .NumInputs(2)
55  .NumOutputs(1)
56  .AllowInplace({{1, 0}})
57  .Arg("alpha", "Coefficient of leakage")
58  .InheritOnnxSchema("LeakyRelu");
59 
61  using GradientMakerBase::GradientMakerBase;
62  vector<OperatorDef> GetGradientDefs() override {
63  return SingleGradientDef(
64  "LeakyReluGradient",
65  "",
66  vector<string>{O(0), GO(0)},
67  vector<string>{GI(0)});
68  }
69 };
70 
71 REGISTER_GRADIENT(LeakyRelu, GetLeakyReluGradient);
72 
73 } // 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 ...