Caffe2 - C++ API
A deep learning, cross platform ML framework
sqr_op.cc
1 #include "caffe2/operators/sqr_op.h"
2 
3 #include <string>
4 #include <vector>
5 
6 namespace caffe2 {
7 
8 REGISTER_CPU_OPERATOR(
9  Sqr,
10  UnaryElementwiseOp<TensorTypes<float>, CPUContext, SqrFunctor<CPUContext>>);
11 
12 OPERATOR_SCHEMA(Sqr)
13  .NumInputs(1)
14  .NumOutputs(1)
15  .AllowInplace({{0, 0}})
16  .IdenticalTypeAndShape()
17  .SetDoc(R"DOC(
18 Performs element-wise squaring ($x^2$) of input tensor.
19 
20 Github Link:
21 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/sqr_op.cc
22 
23 <details>
24 
25 <summary> <b>Example</b> </summary>
26 
27 **Code**
28 
29 ```
30 
31 workspace.ResetWorkspace()
32 
33 op = core.CreateOperator(
34  "Sqr",
35  ["X"],
36  ["Y"],
37 )
38 
39 workspace.FeedBlob("X", (np.random.randint(10, size=(3,3))).astype(np.float32))
40 print("X:", workspace.FetchBlob("X"))
41 workspace.RunOperatorOnce(op)
42 print("Y:", workspace.FetchBlob("Y"))
43 
44 ```
45 
46 **Result**
47 
48 ```
49 
50 X:
51 [[4. 6. 2.]
52  [0. 1. 6.]
53  [9. 2. 7.]]
54 Y:
55 [[16. 36. 4.]
56  [ 0. 1. 36.]
57  [81. 4. 49.]]
58 
59 ```
60 
61 </details>
62 
63  )DOC")
64  .Input(0, "X", "*(type: Tensor`<float>`)* Input data tensor.")
65  .Output(0, "Y", "*(type: Tensor`<float>`)* Output tensor.");
66 
67 namespace {
68 
69 class GetSqrGradient : public GradientMakerBase {
70  using GradientMakerBase::GradientMakerBase;
71  std::vector<OperatorDef> GetGradientDefs() override {
72  Argument scale_arg;
73  scale_arg.set_name("scale");
74  scale_arg.set_f(2.0);
75  return std::vector<OperatorDef>{CreateOperatorDef(
76  "Scale",
77  "",
78  std::vector<std::string>{GO(0)},
79  std::vector<std::string>{GO(0)},
80  std::vector<Argument>{scale_arg}),
81  CreateOperatorDef(
82  "Mul",
83  "",
84  std::vector<std::string>{GO(0), I(0)},
85  std::vector<std::string>{GI(0)})};
86  }
87 };
88 
89 } // namespace
90 
91 REGISTER_GRADIENT(Sqr, GetSqrGradient);
92 
93 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13