Caffe2 - C++ API
A deep learning, cross platform ML framework
reciprocal_gradient_op.cc
1 #include "caffe2/operators/reciprocal_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 <typename T>
14 bool ReciprocalGradientFunctor<CPUContext>::Forward(
15  const std::vector<int>& Y_dims,
16  const std::vector<int>& /* dY_dims */,
17  const T* Y,
18  const T* dY,
19  T* dX,
20  CPUContext* /* context */) const {
21  const int size = std::accumulate(
22  Y_dims.cbegin(), Y_dims.cend(), 1, std::multiplies<int>());
23  ConstEigenVectorArrayMap<T> dY_arr(dY, size);
24  ConstEigenVectorArrayMap<T> Y_arr(Y, size);
25  EigenVectorMap<T>(dX, size) = dY_arr * (-Y_arr.square());
26  return true;
27 }
28 
29 REGISTER_CPU_OPERATOR(
30  ReciprocalGradient,
31  BinaryElementwiseOp<
32  TensorTypes<float>,
33  CPUContext,
34  ReciprocalGradientFunctor<CPUContext>>);
35 
36 namespace {
37 
38 class GetReciprocalGradient : public GradientMakerBase {
39  using GradientMakerBase::GradientMakerBase;
40  std::vector<OperatorDef> GetGradientDefs() override {
41  return SingleGradientDef(
42  "ReciprocalGradient",
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(Reciprocal, GetReciprocalGradient);
52 
53 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13