1 #include "caffe2/operators/leaky_relu_op.h" 3 #include "caffe2/utils/math.h" 8 bool LeakyReluOp<float, CPUContext>::RunOnDevice() {
9 const auto& X = Input(0);
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_;
19 bool LeakyReluGradientOp<float, CPUContext>::RunOnDevice() {
20 const auto& Y = Input(0);
21 const auto& dY = Input(1);
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_;
34 REGISTER_CPU_OPERATOR(LeakyRelu, LeakyReluOp<float, CPUContext>);
35 REGISTER_CPU_OPERATOR(
37 LeakyReluGradientOp<float, CPUContext>);
39 OPERATOR_SCHEMA(LeakyRelu)
42 .Arg(
"alpha",
"Coefficient of leakage, default value is 0.01")
43 .AllowInplace({{0, 0}})
44 .CostInferenceFunction(PointwiseCostInference<2>)
45 .IdenticalTypeAndShape()
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. 51 .Input(0, "X",
"1D input tensor")
52 .Output(0,
"Y",
"1D input tensor");
53 OPERATOR_SCHEMA(LeakyReluGradient)
56 .AllowInplace({{1, 0}})
57 .Arg(
"alpha",
"Coefficient of leakage")
58 .InheritOnnxSchema(
"LeakyRelu");
61 using GradientMakerBase::GradientMakerBase;
62 vector<OperatorDef> GetGradientDefs()
override {
66 vector<string>{O(0), GO(0)},
67 vector<string>{GI(0)});
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 ...