Caffe2 - C++ API
A deep learning, cross platform ML framework
cos_op.cc
1 
17 #include "caffe2/operators/elementwise_op.h"
18 #include "caffe2/utils/math.h"
19 
20 namespace caffe2 {
21 
22 struct CosCPUFunctor {
23  template <typename T>
24  inline void
25  operator()(const int n, const T* x, T* y, CPUContext* device_context) {
26  math::Cos<T, CPUContext>(n, x, y, device_context);
27  }
28 };
29 
31  template <typename T>
32  inline void
33  Run(const int n, const T* x, const T* dy, T* dx, CPUContext* /* unused */) {
34  ConstEigenVectorArrayMap<T> dyM(dy, n);
35  ConstEigenVectorArrayMap<T> xM(x, n);
36  EigenVectorMap<T>(dx, n) = -dyM * sin(xM);
37  }
38 };
39 
40 REGISTER_CPU_OPERATOR(
41  Cos,
43 REGISTER_CPU_OPERATOR(
44  CosGradient,
47  CPUContext,
49 
50 OPERATOR_SCHEMA(Cos)
51  .NumInputs(1)
52  .NumOutputs(1)
53  .IdenticalTypeAndShape()
54  .SetDoc(R"DOC(
55 Calculates the cosine of the given input tensor, element-wise.
56 )DOC")
57  .Input(0, "input", "Input tensor")
58  .Output(
59  0,
60  "output",
61  "The cosine of the input tensor computed element-wise");
62 
63 OPERATOR_SCHEMA(CosGradient).NumInputs(2).NumOutputs(1).IdenticalTypeAndShape();
64 
66  using GradientMakerBase::GradientMakerBase;
67  vector<OperatorDef> GetGradientDefs() override {
68  return SingleGradientDef(
69  "CosGradient",
70  "",
71  std::vector<string>{I(0), GO(0)},
72  std::vector<string>{GI(0)});
73  }
74 };
75 REGISTER_GRADIENT(Cos, GetCosGradient);
76 } // 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.