Caffe2 - C++ API
A deep learning, cross platform ML framework
sparse_funhash_op.cc
1 #include "caffe2/experiments/operators/sparse_funhash_op.h"
2 
3 namespace caffe2 {
4 namespace {
5 
6 REGISTER_CPU_OPERATOR(SparseFunHash, SparseFunHashOp<float, CPUContext>);
7 REGISTER_CPU_OPERATOR(
8  SparseFunHashGradient,
9  SparseFunHashGradientOp<float, CPUContext>);
10 
11 OPERATOR_SCHEMA(SparseFunHash)
12  .NumInputs(4, 5)
13  .NumOutputs(1)
14  .SetDoc(R"DOC(
15 This layer compresses a fully-connected layer for sparse inputs
16 via hashing.
17 It takes four required inputs and an option fifth input.
18 The first three inputs `scalars`, `indices`, and `segment_ids` are
19 the sparse segmented representation of sparse data, which are the
20 same as the last three inputs of the `SparseSortedSegmentWeightedSum`
21 operator. If the argument `num_segments` is specified, it would be used
22 as the first dimension for the output; otherwise it would be derived
23 from the maximum segment ID.
24 
25 The fourth input is a 1D weight vector. Each entry of the fully-connected
26 layer would be randomly mapped from one of the entries in this vector.
27 
28 When the optional fifth input vector is present, each weight of the
29 fully-connected layer would be the linear combination of K entries
30 randomly mapped from the weight vector, provided the input
31 (length-K vector) serves as the coefficients.
32 )DOC")
33  .Input(0, "scalars", "Values of the non-zero entries of the sparse data.")
34  .Input(1, "indices", "Indices to the non-zero valued features.")
35  .Input(
36  2,
37  "segment_ids",
38  "Segment IDs corresponding to the non-zero entries.")
39  .Input(3, "weight", "Weight vector")
40  .Input(
41  4,
42  "alpha",
43  "Optional coefficients for linear combination of hashed weights.")
44  .Output(
45  0,
46  "output",
47  "Output tensor with the first dimension equal to the number "
48  "of segments.")
49  .Arg("num_outputs", "Number of outputs")
50  .Arg("num_segments", "Number of segments");
51 
52 OPERATOR_SCHEMA(SparseFunHashGradient).NumInputs(5, 6).NumOutputs(2, 3);
53 
54 class GetSparseFunHashGradient : public GradientMakerBase {
55  using GradientMakerBase::GradientMakerBase;
56  vector<OperatorDef> GetGradientDefs() override {
57  if (def_.input_size() == 4) {
58  return SingleGradientDef(
59  "SparseFunHashGradient",
60  "",
61  vector<string>{GO(0), I(0), I(1), I(2), I(3)},
62  vector<string>{GI_V(3), GI_I(3)});
63  }
64  // def_.input_size() == 5
65  return SingleGradientDef(
66  "SparseFunHashGradient",
67  "",
68  vector<string>{GO(0), I(0), I(1), I(2), I(3), I(4)},
69  vector<string>{GI_V(3), GI_I(3), GI(4)});
70  }
71 };
72 
73 REGISTER_GRADIENT(SparseFunHash, GetSparseFunHashGradient);
74 
75 } // namespace
76 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...