Caffe2 - C++ API
A deep learning, cross platform ML framework
sigmoid_op.cc
1 #include "caffe2/operators/sigmoid_op.h"
2 
3 #include "caffe2/utils/eigen_utils.h"
4 
5 namespace caffe2 {
6 
7 template <>
8 template <typename T>
9 bool SigmoidFunctor<CPUContext>::
10 operator()(const int N, const T* X, T* Y, CPUContext* /* context */) const {
11  EigenVectorArrayMap<T>(Y, N) =
12  T(1) / (T(1) + (-ConstEigenVectorArrayMap<T>(X, N)).exp());
13  return true;
14 }
15 
16 REGISTER_CPU_OPERATOR(
17  Sigmoid,
18  UnaryElementwiseOp<
19  TensorTypes<float>,
20  CPUContext,
21  SigmoidFunctor<CPUContext>>);
22 
23 // Input: X, output: Y
24 OPERATOR_SCHEMA(Sigmoid)
25  .NumInputs(1)
26  .NumOutputs(1)
27  .AllowInplace({{0, 0}})
28  .IdenticalTypeAndShape()
29  .SetDoc(R"DOC(
30 Apply the Sigmoid function element-wise to the input tensor. This is often used
31 as a non-linear activation function in a neural network. The sigmoid function is
32 defined as:
33 
34 $$Sigmoid(x) = \frac{1}{1+\exp(-x)}$$
35 
36 Github Links:
37 
38 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/sigmoid_op.cc
39 
40 
41 <details>
42 
43 <summary> <b>Example</b> </summary>
44 
45 **Code**
46 
47 ```
48 
49 workspace.ResetWorkspace()
50 
51 op = core.CreateOperator(
52  "Sigmoid",
53  ["X"],
54  ["Y"]
55 )
56 
57 workspace.FeedBlob("X", np.random.randn(5).astype(np.float32))
58 print("input:", workspace.FetchBlob("X"))
59 workspace.RunOperatorOnce(op)
60 print("sigmoid:", workspace.FetchBlob("Y"))
61 
62 ```
63 
64 **Result**
65 
66 ```
67 
68 input: [ 1.5744036 0.31632107 1.7842269 1.4450722 -2.1726978 ]
69 sigmoid: [0.8284105 0.57842743 0.85621804 0.80923885 0.10222916]
70 
71 ```
72 
73 </details>
74 
75 
76 )DOC")
77  .Input(0, "X", "*(type: Tensor`<float>`)* Input tensor.")
78  .Output(0, "Y", "*(type: Tensor`<float>`)* Output tensor.")
79  .InheritOnnxSchema();
80 // Input: Y, dY, output: dX
81 OPERATOR_SCHEMA(SigmoidGradient)
82  .NumInputs(2)
83  .NumOutputs(1)
84  .AllowInplace({{1, 0}})
85  .SetDoc(R"DOC(
86 SigmoidGradient takes both Y and dY and uses this to update dX according to the
87 chain rule and derivatives of the sigmoid function.
88 )DOC");
89 
90 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13