Caffe2 - C++ API
A deep learning, cross platform ML framework
softsign_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> x_arr(x, n);
27  EigenVectorMap<T>(y, n) = (1 + x_arr.abs()).inverse() * x_arr;
28  }
29 };
30 
32  template <typename T>
33  inline void Run(
34  const int n,
35  const T* x,
36  const T* dy,
37  T* dx,
38  CPUContext* /*device_context*/) {
39  ConstEigenVectorArrayMap<T> dy_arr(dy, n);
40  ConstEigenVectorArrayMap<T> x_arr(x, n);
41  EigenVectorMap<T>(dx, n) = dy_arr * (1 + x_arr.abs()).pow(2).inverse();
42  }
43 };
44 
45 REGISTER_CPU_OPERATOR(
46  Softsign,
48 REGISTER_CPU_OPERATOR(
49  SoftsignGradient,
52  CPUContext,
54 
55 OPERATOR_SCHEMA(Softsign)
56  .NumInputs(1)
57  .NumOutputs(1)
58  .AllowInplace({{0, 0}})
59  .IdenticalTypeAndShape()
60  .SetDoc(R"DOC(
61 Calculates the softsign (x/1+|x|) of the given input tensor element-wise. This
62 operation can be done in an in-place fashion too, by providing the same input
63 and output blobs.
64 )DOC")
65  .Input(0, "input", "1-D input tensor")
66  .Output(
67  0,
68  "output",
69  "The softsign (x/1+|x|) values of the input tensor "
70  "computed element-wise");
71 
72 OPERATOR_SCHEMA(SoftsignGradient)
73  .NumInputs(2)
74  .NumOutputs(1)
75  .AllowInplace({{1, 0}})
76  .SetDoc(R"DOC(
77 Calculates the softsign gradient (sgn(x)/(1+|x|)^2) of the given input tensor
78 element-wise.
79 )DOC")
80  .Input(0, "input", "1-D input tensor")
81  .Input(1, "input", "1-D input tensor")
82  .Output(
83  0,
84  "output",
85  "The softsign gradient (sgn(x)/(1+|x|)^2) values of the input tensor "
86  "computed element-wise");
87 
89  using GradientMakerBase::GradientMakerBase;
90  vector<OperatorDef> GetGradientDefs() override {
91  CAFFE_ENFORCE(
92  I(0) != O(0),
93  "Cannot compute softsign gradient "
94  "if you choose to do an in-place calculation.");
95 
96  return SingleGradientDef(
97  "SoftsignGradient",
98  "",
99  vector<string>{I(0), GO(0)},
100  vector<string>{GI(0)});
101  }
102 };
103 
104 REGISTER_GRADIENT(Softsign, GetSoftsignGradient);
105 
106 } // 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.