Caffe2 - C++ API
A deep learning, cross platform ML framework
tanh_gradient_op.cc
1 #include "caffe2/operators/tanh_op.h"
2 
3 #include "caffe2/utils/eigen_utils.h"
4 
5 #include <algorithm>
6 #include <functional>
7 #include <string>
8 #include <vector>
9 
10 namespace caffe2 {
11 
12 template <>
13 template <>
14 bool TanhGradientFunctor<CPUContext>::Forward<float>(
15  const std::vector<int>& Y_dims,
16  const std::vector<int>& /* dY_dims */,
17  const float* Y,
18  const float* dY,
19  float* dX,
20  CPUContext* /* context */) const {
21  const int size = std::accumulate(
22  Y_dims.cbegin(), Y_dims.cend(), 1, std::multiplies<int>());
23  ConstEigenVectorArrayMap<float> dY_arr(dY, size);
24  ConstEigenVectorArrayMap<float> Y_arr(Y, size);
25  EigenVectorMap<float>(dX, size) = dY_arr * (1 - Y_arr * Y_arr);
26  return true;
27 }
28 
29 REGISTER_CPU_OPERATOR(
30  TanhGradient,
31  BinaryElementwiseOp<
32  TensorTypes<float>,
33  CPUContext,
34  TanhGradientFunctor<CPUContext>>);
35 
36 namespace {
37 
38 class GetTanhGradient : public GradientMakerBase {
39  using GradientMakerBase::GradientMakerBase;
40  std::vector<OperatorDef> GetGradientDefs() override {
41  return SingleGradientDef(
42  "TanhGradient",
43  "",
44  std::vector<std::string>{O(0), GO(0)},
45  std::vector<std::string>{GI(0)});
46  }
47 };
48 
49 } // namespace
50 
51 REGISTER_GRADIENT(Tanh, GetTanhGradient);
52 
53 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13