Caffe2 - C++ API
A deep learning, cross platform ML framework
negative_op.cc
1 #include "caffe2/operators/negative_op.h"
2 
3 #include <string>
4 #include <vector>
5 
6 namespace caffe2 {
7 
8 REGISTER_CPU_OPERATOR(
9  Negative,
10  UnaryElementwiseOp<NumericTypes, CPUContext, NegativeFunctor<CPUContext>>);
11 
12 // Input: X, output: Y
13 OPERATOR_SCHEMA(Negative)
14  .NumInputs(1)
15  .NumOutputs(1)
16  .AllowInplace({{0, 0}})
17  .IdenticalTypeAndShape()
18  .SetDoc(R"DOC(
19 Computes the element-wise negative of the input.
20 
21 Github Links:
22 
23 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/negative_op.cc
24 
25 <details>
26 
27 <summary> <b>Example</b> </summary>
28 
29 **Code**
30 
31 ```
32 workspace.ResetWorkspace()
33 
34 op = core.CreateOperator(
35  "Negative",
36  ["X"],
37  ["Y"]
38 )
39 
40 workspace.FeedBlob("X", (np.random.rand(3,3).astype(np.float32)))
41 print("X:", workspace.FetchBlob("X"))
42 workspace.RunOperatorOnce(op)
43 print("Y:", workspace.FetchBlob("Y"))
44 ```
45 
46 **Result**
47 
48 ```
49 X: [[0.83296907 0.61407167 0.32562155]
50  [0.59304523 0.03111175 0.29365504]
51  [0.09478621 0.5424558 0.73940724]]
52 Y: [[-0.83296907 -0.61407167 -0.32562155]
53  [-0.59304523 -0.03111175 -0.29365504]
54  [-0.09478621 -0.5424558 -0.73940724]]
55 ```
56 
57 </details>
58 
59 )DOC")
60  .Input(0, "X", "*(type: Tensor`<float>`)* 1D input tensor.")
61  .Output(0, "Y", "*(type: Tensor`<float>`)* 1D output tensor.")
62  .InheritOnnxSchema("Neg");
63 
64 namespace {
65 
66 class GetNegativeGradient : public GradientMakerBase {
67  using GradientMakerBase::GradientMakerBase;
68  std::vector<OperatorDef> GetGradientDefs() override {
69  return SingleGradientDef(
70  "Negative",
71  "",
72  std::vector<std::string>{GO(0)},
73  std::vector<std::string>{GI(0)});
74  }
75 };
76 
77 } // namespace
78 
79 REGISTER_GRADIENT(Negative, GetNegativeGradient);
80 
81 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13