1 #include "caffe2/operators/cos_op.h" 2 #include "caffe2/utils/eigen_utils.h" 11 bool CosGradientFunctor<CPUContext>::Forward(
12 const std::vector<int>& X_dims,
13 const std::vector<int>& ,
18 const int size = std::accumulate(
19 X_dims.cbegin(), X_dims.cend(), 1, std::multiplies<int>());
20 ConstEigenVectorArrayMap<T> dY_arr(dY, size);
21 ConstEigenVectorArrayMap<T> X_arr(X, size);
22 EigenVectorMap<T>(dX, size) = -dY_arr * X_arr.sin();
26 REGISTER_CPU_OPERATOR(
28 UnaryElementwiseOp<TensorTypes<float>, CPUContext, CosFunctor<CPUContext>>);
29 REGISTER_CPU_OPERATOR(
34 CosGradientFunctor<CPUContext>>);
39 .IdenticalTypeAndShape()
41 Calculates the cosine of the given input tensor, element-wise. 45 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/cos_op.cc 50 <summary> <b>Example</b> </summary> 56 workspace.ResetWorkspace() 58 op = core.CreateOperator( 64 workspace.FeedBlob("X", np.random.rand(5).astype(np.float32)) 65 print("X:", workspace.FetchBlob("X")) 66 workspace.RunOperatorOnce(op) 67 print("Y:", workspace.FetchBlob("Y")) 75 X: [0.6816719 0.76771533 0.933932 0.01404487 0.11862425] 76 Y: [0.7765203 0.71949923 0.5946774 0.99990135 0.9929724 ] 84 .Input(0, "X",
"*(type: Tensor`<float>`)* Input tensor.")
88 "*(type: Tensor`<float>`)* Output tensor calculated as the cosine of the input tensor, element-wise.");
90 OPERATOR_SCHEMA(CosGradient).NumInputs(2).NumOutputs(1).IdenticalTypeAndShape();
94 class GetCosGradient :
public GradientMakerBase {
95 using GradientMakerBase::GradientMakerBase;
96 std::vector<OperatorDef> GetGradientDefs()
override {
97 return SingleGradientDef(
100 std::vector<std::string>{I(0), GO(0)},
101 std::vector<std::string>{GI(0)});
107 REGISTER_GRADIENT(Cos, GetCosGradient);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...