Caffe2 - C++ API
A deep learning, cross platform ML framework
sample_as_op.cc
1 
17 #include "sample_as_op.h"
18 
19 namespace caffe2 {
20 
21 REGISTER_CPU_OPERATOR(SampleAs, SampleAsOp<float, CPUContext>);
22 REGISTER_CPU_OPERATOR(SampleAsGradient, SampleAsGradientOp<float, CPUContext>);
23 
24 OPERATOR_SCHEMA(SampleAs)
25  .NumInputs(2)
26  .NumOutputs(1)
27  .SetDoc(R"DOC(
28 Select the batch elements from input tensor X where the corresponding input
29 label value is > 0.
30 )DOC")
31  .Input(
32  0,
33  "X",
34  "Tensor of at least 1D shape (N, ...).")
35  .Input(
36  1,
37  "labels",
38  "Tensor of type int with 1D shape (N, ).")
39  .Output(
40  0,
41  "Y",
42  "Tensor with number of dims matching X, but with the length of dim 0 "
43  "equal to the number of non-zero elements in labels. The batch items "
44  "from X corresponding to the non-zero elements in labels are copied "
45  "into Y.");
46 
47 OPERATOR_SCHEMA(SampleAsGradient)
48  .NumInputs(3)
49  .NumOutputs(1)
50  .Input(
51  0,
52  "X",
53  "See SampleAs.")
54  .Input(
55  1,
56  "labels",
57  "See SampleAs."
58  )
59  .Input(
60  2,
61  "dY",
62  "Gradient of forward output 0 (Y).")
63  .Output(
64  0,
65  "dX",
66  "Gradient of forward input 0 (X).");
67 
69  using GradientMakerBase::GradientMakerBase;
70  vector<OperatorDef> GetGradientDefs() override {
71  return SingleGradientDef(
72  "SampleAsGradient",
73  "",
74  vector<string>{I(0), I(1), GO(0)},
75  vector<string>{GI(0)});
76  }
77 };
78 
79 REGISTER_GRADIENT(SampleAs, GetSampleAsGradient);
80 
81 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...