Caffe2 - C++ API
A deep learning, cross platform ML framework
mean_op.cc
1 #include "caffe2/operators/mean_op.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(Mean, MeanOp<CPUContext>);
6 REGISTER_CPU_OPERATOR(MeanGradient, MeanGradientOp<CPUContext>);
7 
8 OPERATOR_SCHEMA(Mean)
9  .NumInputs(1, INT_MAX)
10  .NumOutputs(1)
11  .IdenticalTypeAndShapeOfInput(0)
12  .AllowInplace({{0, 0}})
13  .SetDoc(R"DOC(
14 Element-wise mean of an arbitrary number of input tensors. This operation can be
15 performed in-place, by using the first input blob as the output blob. All inputs
16 must have the same shape and data type, and the output will have the same shape
17 as the inputs.
18 
19 Github Link:
20 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/mean_op.cc
21 
22 <details>
23 
24 <summary> <b>Example</b> </summary>
25 
26 **Code**
27 
28 ```
29 
30 workspace.ResetWorkspace()
31 
32 op = core.CreateOperator(
33  "Mean",
34  ["X", "Y", "Z"],
35  ["X"],
36 )
37 
38 workspace.FeedBlob("X", (np.random.rand(3,3)).astype(np.float32))
39 workspace.FeedBlob("Y", (np.random.rand(3,3)).astype(np.float32))
40 workspace.FeedBlob("Z", (np.random.rand(3,3)).astype(np.float32))
41 print("X:", workspace.FetchBlob("X"))
42 print("Y:", workspace.FetchBlob("Y"))
43 print("Z:", workspace.FetchBlob("Z"))
44 workspace.RunOperatorOnce(op)
45 print("Mean:", workspace.FetchBlob("X"))
46 
47 ```
48 
49 **Result**
50 
51 ```
52 
53 X:
54 [[0.6035237 0.5305746 0.6298913 ]
55  [0.9169737 0.01280353 0.16286302]
56  [0.6017664 0.9946255 0.05128575]]
57 Y:
58 [[0.07544111 0.45371833 0.08460239]
59  [0.9708728 0.7422064 0.7933344 ]
60  [0.97671497 0.3411384 0.73818344]]
61 Z:
62 [[0.08837954 0.90187573 0.46734726]
63  [0.6308827 0.8719029 0.39888734]
64  [0.90059936 0.92883426 0.5695987 ]]
65 Mean:
66 [[0.25578147 0.6287229 0.39394698]
67  [0.8395764 0.5423043 0.45169494]
68  [0.8263602 0.75486606 0.45302266]]
69 
70 ```
71 
72 </details>
73 
74 )DOC")
75  .Input(0, "X, Y, ...", "*(type: Tensor`<Ord>`)* List of input tensors with the same shape.")
76  .Output(0, "M", "*(type: Tensor`<Ord>`)* Output tensor with the same dimensions as inputs. Contains "
77  "the mean values of the input tensors calculated element-wise.");
78 
79 class GetMeanGradient : public GradientMakerBase {
80  using GradientMakerBase::GradientMakerBase;
81  vector<OperatorDef> GetGradientDefs() override {
82  auto outputs = std::vector<string>();
83  for (int i = 0; i < def_.input_size(); i++) {
84  outputs.push_back(GI(i));
85  }
86  return SingleGradientDef(
87  "MeanGradient", "", std::vector<string>{GO(0)}, outputs);
88  }
89 };
90 
91 REGISTER_GRADIENT(Mean, GetMeanGradient);
92 
93 OPERATOR_SCHEMA(MeanGradient)
94  .NumInputs(1)
95  .NumOutputs(1, INT_MAX)
96  .AllowInplace({{0, 0}});
97 
98 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
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 ...