Caffe2 - C++ API
A deep learning, cross platform ML framework
sigmoid_op.cc
1 
17 #include "caffe2/operators/elementwise_op.h"
18 #include "caffe2/utils/math.h"
19 
20 namespace caffe2 {
21 
23  template <typename T>
24  inline void
25  operator()(const int n, const T* x, T* y, CPUContext* /*device_context*/) {
26  ConstEigenVectorArrayMap<T> xM(x, n);
27  EigenVectorArrayMap<T>(y, n) = 1. / (1. + (-xM).exp());
28  }
29 };
30 
32  template <typename T>
33  inline void Run(
34  const int n,
35  const T* y,
36  const T* dy,
37  T* dx,
38  CPUContext* /*device_context*/) {
39  ConstEigenVectorArrayMap<T> yM(y, n), dyM(dy, n);
40  EigenVectorArrayMap<T>(dx, n) = dyM * yM * (1. - yM);
41  }
42 };
43 
44 REGISTER_CPU_OPERATOR(
45  Sigmoid, UnaryElementwiseOp<
47 REGISTER_CPU_OPERATOR(
48  SigmoidGradient,
51  CPUContext,
53 
54 // Input: X, output: Y
55 OPERATOR_SCHEMA(Sigmoid)
56  .NumInputs(1)
57  .NumOutputs(1)
58  .AllowInplace({{0, 0}})
59  .IdenticalTypeAndShape()
60  .SetDoc(R"DOC(
61 Sigmoid takes one input data (Tensor<T>) and produces one output data
62 (Tensor<T>) where the sigmoid function, y = 1 / (1 + exp(-x)), is applied to the
63 tensor elementwise.
64 )DOC")
65  .Input(0, "X", "1D input tensor")
66  .Output(0, "Y", "1D output tensor");
67 // Input: Y, dY, output: dX
68 OPERATOR_SCHEMA(SigmoidGradient)
69  .NumInputs(2)
70  .NumOutputs(1)
71  .AllowInplace({{1, 0}})
72  .SetDoc(R"DOC(
73 SigmoidGradient takes both Y and dY and uses this to update dX according to the
74 chain rule and derivatives of the sigmoid function.
75 )DOC");
76 
78  using GradientMakerBase::GradientMakerBase;
79  vector<OperatorDef> GetGradientDefs() override {
80  return SingleGradientDef(
81  "SigmoidGradient", "",
82  vector<string>{O(0), GO(0)},
83  vector<string>{GI(0)});
84  }
85 };
86 REGISTER_GRADIENT(Sigmoid, GetSigmoidGradient);
87 } // namespace caffe2
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:82
Copyright (c) 2016-present, Facebook, Inc.
Performs a binary operation (e.g.