Caffe2 - C++ API
A deep learning, cross platform ML framework
log_op.cc
1 #include "caffe2/operators/log_op.h"
2 
3 #include <string>
4 #include <vector>
5 
6 namespace caffe2 {
7 
8 REGISTER_CPU_OPERATOR(
9  Log,
10  UnaryElementwiseOp<TensorTypes<float>, CPUContext, LogFunctor<CPUContext>>);
11 
12 OPERATOR_SCHEMA(Log)
13  .NumInputs(1)
14  .NumOutputs(1)
15  .AllowInplace({{0, 0}})
16  .IdenticalTypeAndShape()
17  .SetDoc(R"DOC(
18 Calculates the natural log of the given input tensor ($ln(x)$), element-wise. This
19 operation can be done in an in-place fashion too, by providing the same input
20 and output blobs.
21 
22 Github Link:
23 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/log_op.cc
24 
25 <details>
26 
27 <summary> <b>Example</b> </summary>
28 
29 **Code**
30 
31 ```
32 
33 workspace.ResetWorkspace()
34 
35 op = core.CreateOperator(
36  "Log",
37  ["X"],
38  ["X"],
39 )
40 
41 workspace.FeedBlob("X", (np.random.rand(3,3)).astype(np.float32))
42 print("X before running op:", workspace.FetchBlob("X"))
43 workspace.RunOperatorOnce(op)
44 print("X after running op:", workspace.FetchBlob("X"))
45 
46 ```
47 
48 **Result**
49 
50 ```
51 
52 X before running op:
53 [[0.07341351 0.15404125 0.386613 ]
54  [0.34090295 0.99727786 0.24141751]
55  [0.32016268 0.8724168 0.93515724]]
56 X after running op:
57 [[-2.6116474 -1.8705349 -0.9503311 ]
58  [-1.0761575 -0.00272586 -1.4212275 ]
59  [-1.138926 -0.13648799 -0.06704059]]
60 
61 ```
62 
63 </details>
64 
65 )DOC")
66  .Input(0, "X", "*(type: Tensor`<float>`)* Input tensor.")
67  .Output(
68  0,
69  "Y",
70  "*(type: Tensor`<float>`)* Output tensor computed as the natural log of the input tensor computed, element-wise.")
71  .InheritOnnxSchema();
72 
73 namespace {
74 
76  using GradientMakerBase::GradientMakerBase;
77  std::vector<OperatorDef> GetGradientDefs() override {
78  return SingleGradientDef(
79  "Div",
80  "",
81  std::vector<std::string>{GO(0), I(0)},
82  std::vector<std::string>{GI(0)});
83  }
84 };
85 
86 } // namespace
87 
88 REGISTER_GRADIENT(Log, GetLogGradient);
89 
90 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13