1 #include "caffe2/operators/elu_op.h" 3 #include "caffe2/utils/math.h" 8 bool EluOp<float, CPUContext>::RunOnDevice() {
12 CAFFE_ENFORCE_GE(alpha_, 0);
14 const auto* Xdata = X.template data<float>();
15 auto* Ydata = Y->template mutable_data<float>();
16 ConstEigenVectorArrayMap<float> Xvec(Xdata, X.size());
17 EigenVectorArrayMap<float> Yvec(Ydata, Y->size());
18 Yvec = Xvec.cwiseMax(0.f) + (alpha_ * (Xvec.exp() - 1.0f)).cwiseMin(0.f);
23 bool EluGradientOp<float, CPUContext>::RunOnDevice() {
27 DCHECK_GT(Y.size(), 0);
28 DCHECK_EQ(dY.size(), Y.size());
31 const float* Ydata = Y.data<
float>();
32 const float* dYdata = dY.data<
float>();
33 float* dXdata = dX->mutable_data<
float>();
34 ConstEigenVectorArrayMap<float> Yvec(Ydata, Y.size());
35 ConstEigenVectorArrayMap<float> dYvec(dYdata, dY.size());
36 EigenVectorArrayMap<float> dXvec(dXdata, dX->size());
37 dXvec = (Yvec > 0).select(dYvec, dYvec * (Yvec + alpha_));
41 REGISTER_CPU_OPERATOR(Elu, EluOp<float, CPUContext>);
42 REGISTER_CPU_OPERATOR(EluGradient, EluGradientOp<float, CPUContext>);
48 .AllowInplace({{0, 0}})
49 .IdenticalTypeAndShape()
52 Elu takes one input data (Tensor<T>) and produces one output data 53 (Tensor<T>) where the function `f(x) = alpha * (exp(x) - 1.) for x < 54 0`, `f(x) = x for x >= 0`., is applied to the tensor elementwise. 57 .Input(0, "X",
"1D input tensor")
58 .Output(0,
"Y",
"1D input tensor")
59 .InheritOnnxSchema(
"Elu");
62 OPERATOR_SCHEMA(EluGradient)
65 .AllowInplace({{1, 0}})
67 EluGradient takes both Y and dY and uses this to update dX according to the 68 chain rule and derivatives of the rectified linear function. 72 using GradientMakerBase::GradientMakerBase;
73 vector<OperatorDef> GetGradientDefs()
override {
75 def_.type() +
"Gradient",
77 vector<string>{O(0), GO(0)},
78 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 ...