1 #include "caffe2/operators/abs_op.h" 2 #include "caffe2/utils/eigen_utils.h" 11 bool AbsGradientFunctor<CPUContext>::Forward(
12 const std::vector<int>& X_dims,
13 const std::vector<int>& ,
18 const int size = std::accumulate(
19 X_dims.cbegin(), X_dims.cend(), 1, std::multiplies<int>());
20 ConstEigenVectorArrayMap<T> dY_arr(dY, size);
21 ConstEigenVectorArrayMap<T> X_arr(X, size);
22 EigenVectorMap<T>(dX, size) =
23 (X_arr ==
T(0)).select(
T(0), (X_arr >
T(0)).select(dY_arr, -dY_arr));
27 REGISTER_CPU_OPERATOR(
29 UnaryElementwiseOp<TensorTypes<float>, CPUContext, AbsFunctor<CPUContext>>);
30 REGISTER_CPU_OPERATOR(
35 AbsGradientFunctor<CPUContext>>);
40 .IdenticalTypeAndShape()
42 Calculates the absolute value of the given input tensor, element-wise. 46 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/abs_op.cc 50 <summary> <b>Example</b> </summary> 55 workspace.ResetWorkspace() 57 op = core.CreateOperator( 63 workspace.FeedBlob("X", np.random.randn(5).astype(np.float32)) 64 print("X:", workspace.FetchBlob("X")) 65 workspace.RunOperatorOnce(op) 66 print("Y:", workspace.FetchBlob("Y")) 74 X: [ 0.3005476 1.551666 -1.3591481 0.39191285 -0.21866608] 75 Y: [0.3005476 1.551666 1.3591481 0.39191285 0.21866608] 82 .Input(0, "X",
"*(type: Tensor<float>)* Input tensor.")
86 "*(type: Tensor`<float>`)* Absolute value of input element-wise.")
89 OPERATOR_SCHEMA(AbsGradient).NumInputs(2).NumOutputs(1).IdenticalTypeAndShape();
93 class GetAbsGradient :
public GradientMakerBase {
94 using GradientMakerBase::GradientMakerBase;
95 std::vector<OperatorDef> GetGradientDefs()
override {
96 return SingleGradientDef(
99 std::vector<std::string>{I(0), GO(0)},
100 std::vector<std::string>{GI(0)});
106 REGISTER_GRADIENT(Abs, GetAbsGradient);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...