Caffe2 - C++ API
A deep learning, cross platform ML framework
minmax_ops.cc
1 #include "caffe2/operators/minmax_ops.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(Min, MinOp<float, CPUContext>);
6 REGISTER_CPU_OPERATOR(Max, MaxOp<float, CPUContext>);
7 
8 OPERATOR_SCHEMA(Max)
9  .NumInputs(1, INT_MAX)
10  .NumOutputs(1)
11  .IdenticalTypeAndShapeOfInput(0)
12  .AllowInplace({{0, 0}})
13  .SetDoc(R"DOC(
14 Element-wise max 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/minmax_ops.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  "Max",
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("Max:", workspace.FetchBlob("X"))
46 
47 ```
48 
49 **Result**
50 
51 ```
52 
53 X:
54 [[0.4496477 0.07061381 0.7139333 ]
55  [0.83203 0.05970785 0.72786295]
56  [0.75988126 0.04601283 0.32820013]]
57 Y:
58 [[0.05683139 0.16872478 0.671098 ]
59  [0.70739156 0.09878621 0.03416285]
60  [0.34087983 0.94986707 0.67263436]]
61 Z:
62 [[0.48051122 0.07141234 0.85264146]
63  [0.77086854 0.22082241 0.13154659]
64  [0.42401117 0.995431 0.4263775 ]]
65 Max:
66 [[0.48051122 0.16872478 0.85264146]
67  [0.83203 0.22082241 0.72786295]
68  [0.75988126 0.995431 0.67263436]]
69 
70 ```
71 
72 </details>
73 
74 )DOC")
75  .Input(
76  0,
77  "X, Y, ...",
78  "*(type: Tensor`<Ord>`)* List of input tensors with the same shape.")
79  .Output(
80  0,
81  "M",
82  "*(type: Tensor`<Ord>`)* Output tensor with same dimensions as input(s)."
83  "Contains the maximum valued element at each location.")
84  .InheritOnnxSchema();
85 
86 OPERATOR_SCHEMA(Min)
87  .NumInputs(1, INT_MAX)
88  .NumOutputs(1)
89  .IdenticalTypeAndShapeOfInput(0)
90  .AllowInplace({{0, 0}})
91  .SetDoc(R"DOC(
92 Element-wise min of an arbitrary number of input tensors. This operation can be performed in-place, by using the first input blob as the output blob. All inputs must have the same shape and data type, and the output will have the same shape as the inputs.
93 
94 Github Link:
95 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/minmax_ops.cc
96 
97 <details>
98 
99 <summary> <b>Example</b> </summary>
100 
101 **Code**
102 
103 ```
104 
105 workspace.ResetWorkspace()
106 
107 op = core.CreateOperator(
108  "Min",
109  ["X", "Y", "Z"],
110  ["X"],
111 )
112 
113 workspace.FeedBlob("X", (np.random.rand(2,2)).astype(np.float32))
114 workspace.FeedBlob("Y", (np.random.rand(2,2)).astype(np.float32))
115 workspace.FeedBlob("Z", (np.random.rand(2,2)).astype(np.float32))
116 print("X:", workspace.FetchBlob("X"))
117 print("Y:", workspace.FetchBlob("Y"))
118 print("Z:", workspace.FetchBlob("Z"))
119 workspace.RunOperatorOnce(op)
120 print("Min:", workspace.FetchBlob("X"))
121 
122 ```
123 
124 **Result**
125 
126 ```
127 
128 X:
129 [[0.32731926 0.4939747 ]
130  [0.29242373 0.43460014]]
131 Y:
132 [[0.40928316 0.916115 ]
133  [0.77526504 0.29339448]]
134 Z:
135 [[0.7899794 0.90335774]
136  [0.82599413 0.2843068 ]]
137 Min:
138 [[0.32731926 0.4939747 ]
139  [0.29242373 0.2843068 ]]
140 
141 ```
142 
143 </details>
144 
145 )DOC")
146  .Input(
147  0,
148  "X, Y, ...",
149  "*(type: Tensor`<Ord>`)* List of input tensors with the same shape.")
150  .Output(
151  0,
152  "M",
153  "*(type: Tensor`<Ord>`)* Output tensor with same dimensions as input(s)."
154  "Contains the minimum valued element at each location.")
155  .InheritOnnxSchema();
156 
157 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13