1 #include "caffe2/operators/thresholded_relu_op.h" 3 #include "caffe2/utils/math.h" 8 bool ThresholdedReluOp<float, CPUContext>::RunOnDevice() {
13 ConstEigenVectorArrayMap<float> Xvec(X.data<
float>(), X.size());
14 EigenVectorArrayMap<float> Yvec(Y->mutable_data<
float>(), Y->size());
15 Yvec = (Xvec > alpha_).select(Xvec, 0.f);
28 bool ThresholdedReluGradientOp<float, CPUContext>::RunOnDevice() {
32 CAFFE_ENFORCE_EQ(dY.size(), Y.size());
35 const float* Ydata = Y.data<
float>();
36 const float* dYdata = dY.data<
float>();
37 float* dXdata = dX->mutable_data<
float>();
38 EigenVectorArrayMap<float> dXvec(dXdata, dX->size());
39 ConstEigenVectorArrayMap<float> Yvec(Ydata, Y.size());
40 ConstEigenVectorArrayMap<float> dYvec(dYdata, dY.size());
41 dXvec = dYvec * Yvec.cwiseSign();
50 REGISTER_CPU_OPERATOR(ThresholdedRelu, ThresholdedReluOp<float, CPUContext>);
51 REGISTER_CPU_OPERATOR(
52 ThresholdedReluGradient,
53 ThresholdedReluGradientOp<float, CPUContext>);
56 OPERATOR_SCHEMA(ThresholdedRelu)
59 .AllowInplace({{0, 0}})
60 .CostInferenceFunction(PointwiseCostInference<2>)
61 .IdenticalTypeAndShape()
63 ThresholdedRelu takes one input data (Tensor) and produces one output data 64 (Tensor) where the rectified linear function, y = x for x > alpha, y = 0 65 otherwise, is applied to the tensor elementwise. 67 .Arg("alpha",
"(float) defaults to 1.0.")
68 .Input(0,
"X",
"1D input tensor")
69 .Output(0,
"Y",
"1D input tensor");
72 OPERATOR_SCHEMA(ThresholdedReluGradient)
75 .AllowInplace({{1, 0}})
77 ThresholdedReluGradient takes both Y and dY and uses this to update dX 78 according to the chain rule and derivatives of the rectified linear function. 81 class GetThresholdedReluGradient :
public GradientMakerBase {
82 using GradientMakerBase::GradientMakerBase;
83 vector<OperatorDef> GetGradientDefs()
override {
84 return SingleGradientDef(
85 def_.type() +
"Gradient",
87 vector<string>{O(0), GO(0)},
88 vector<string>{GI(0)});
91 REGISTER_GRADIENT(ThresholdedRelu, GetThresholdedReluGradient);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...