Caffe2 - C++ API
A deep learning, cross platform ML framework
tanh_op.cc
1 #include "caffe2/operators/tanh_op.h"
2 
3 #include "caffe2/utils/eigen_utils.h"
4 
5 namespace caffe2 {
6 
7 #ifdef CAFFE2_USE_ACCELERATE
8 template <>
9 template <>
10 bool TanhFunctor<CPUContext>::operator()<float>(
11  const int N,
12  const float* X,
13  float* Y,
14  CPUContext* /* context */) const {
15  vvtanhf(Y, X, &N);
16  return true;
17 }
18 #endif // CAFFE2_USE_ACCELERATE
19 
20 REGISTER_CPU_OPERATOR(
21  Tanh,
22  UnaryElementwiseOp<
23  TensorTypes<float>,
24  CPUContext,
25  TanhFunctor<CPUContext>>);
26 
27 OPERATOR_SCHEMA(Tanh)
28  .NumInputs(1)
29  .NumOutputs(1)
30  .AllowInplace({{0, 0}})
31  .IdenticalTypeAndShape()
32  .SetDoc(R"DOC(
33 Calculates the hyperbolic tangent of the given input tensor element-wise. This
34 operation can be done in an in-place fashion too, by providing the same input
35 and output blobs.
36 
37 Github Links:
38 
39 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/tanh_op.cc
40 
41 
42 <details>
43 
44 <summary> <b>Example</b> </summary>
45 
46 **Code**
47 
48 ```
49 
50 workspace.ResetWorkspace()
51 
52 op = core.CreateOperator(
53  "Tanh",
54  ["X"],
55  ["X"],
56 )
57 
58 workspace.FeedBlob("X", np.random.randn(3, 3).astype(np.float32))
59 print("X:\n", workspace.FetchBlob("X"), "\n")
60 
61 workspace.RunOperatorOnce(op)
62 print("X:\n", workspace.FetchBlob("X"))
63 
64 ```
65 
66 **Result**
67 
68 ```
69 
70 X:
71  [[ 2.032603 -2.3556721 -0.14955314]
72  [ 0.39309832 -1.1020128 -0.92951244]
73  [-0.62815386 0.21342885 1.4002231 ]]
74 
75 X:
76  [[ 0.9662601 -0.982175 -0.14844811]
77  [ 0.3740282 -0.8012209 -0.73036647]
78  [-0.55677974 0.21024609 0.8853999 ]]
79 
80 ```
81 
82 </details>
83 
84 )DOC")
85  .Input(0, "input", "1-D input tensor")
86  .Output(
87  0,
88  "output",
89  "The hyperbolic tangent values of the input tensor, computed "
90  "element-wise")
91  .InheritOnnxSchema();
92 
93 OPERATOR_SCHEMA(TanhGradient).NumInputs(2).NumOutputs(1).AllowInplace({{1, 0}});
94 
95 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13