Caffe2 - C++ API
A deep learning, cross platform ML framework
tan_op.cc
1 #include "caffe2/operators/tan_op.h"
2 
3 #include "caffe2/utils/eigen_utils.h"
4 
5 #include <algorithm>
6 #include <functional>
7 
8 namespace caffe2 {
9 
10 template <>
11 template <typename T>
12 bool TanGradientFunctor<CPUContext>::Forward(
13  const std::vector<int>& X_dims,
14  const std::vector<int>& /* dY_dims */,
15  const T* X,
16  const T* dY,
17  T* dX,
18  CPUContext* /* context */) const {
19  const int size = std::accumulate(
20  X_dims.cbegin(), X_dims.cend(), 1, std::multiplies<int>());
21  ConstEigenVectorArrayMap<T> dY_arr(dY, size);
22  ConstEigenVectorArrayMap<T> X_arr(X, size);
23  EigenVectorMap<T>(dX, size) = dY_arr / X_arr.cos().square();
24  return true;
25 }
26 
27 REGISTER_CPU_OPERATOR(
28  Tan,
29  UnaryElementwiseOp<TensorTypes<float>, CPUContext, TanFunctor<CPUContext>>);
30 REGISTER_CPU_OPERATOR(
31  TanGradient,
32  BinaryElementwiseOp<
33  TensorTypes<float>,
34  CPUContext,
35  TanGradientFunctor<CPUContext>>);
36 
37 OPERATOR_SCHEMA(Tan)
38  .NumInputs(1)
39  .NumOutputs(1)
40  .IdenticalTypeAndShape()
41  .SetDoc(R"DOC(
42 Calculates the tangent of the given input tensor, element-wise.
43 )DOC")
44  .Input(0, "input", "Input tensor")
45  .Output(
46  0,
47  "output",
48  "The tangent of the input tensor computed element-wise");
49 
50 OPERATOR_SCHEMA(TanGradient).NumInputs(2).NumOutputs(1).IdenticalTypeAndShape();
51 
52 namespace {
53 
54 class GetTanGradient : public GradientMakerBase {
55  using GradientMakerBase::GradientMakerBase;
56  std::vector<OperatorDef> GetGradientDefs() override {
57  return SingleGradientDef(
58  "TanGradient",
59  "",
60  std::vector<std::string>{I(0), GO(0)},
61  std::vector<std::string>{GI(0)});
62  }
63 };
64 
65 } // namespace
66 
67 REGISTER_GRADIENT(Tan, GetTanGradient);
68 
69 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13