Caffe2 - C++ API
A deep learning, cross platform ML framework
percentile_op.cc
1 #include "caffe2/operators/percentile_op.h"
2 
3 namespace caffe2 {
4 
5 template <>
6 bool PercentileOp<CPUContext>::RunOnDevice() {
7  const auto& original_values = Input(X);
8  CAFFE_ENFORCE_EQ(original_values.dim(), 2);
9  const auto num_examples = original_values.size(0);
10  const float* original_values_data = original_values.template data<float>();
11  const auto num_features = original_values.size(1);
12 
13  const auto& value_pct_pairs = Input(VAL_PCT_PAIRS);
14  CAFFE_ENFORCE_EQ(value_pct_pairs.dim(), 2);
15  CAFFE_ENFORCE_EQ(value_pct_pairs.size(1), 2);
16  const int num_values = value_pct_pairs.size(0);
17  const float* value_pct_data = value_pct_pairs.template data<float>();
18 
19  const auto& lengths = Input(LENS);
20  const int* lengths_data = lengths.template data<int>();
21  CAFFE_ENFORCE_EQ(lengths.numel(), num_features);
22 
23  CAFFE_ENFORCE_EQ(
24  std::accumulate(lengths_data, lengths_data + lengths.numel(), 0),
25  num_values,
26  "Sum of lengths should be equal to the total number of samples");
27 
29  &values_tensor,
30  {num_values},
31  at::dtype<float>().device(CPU));
33  &percentiles_tensor,
34  {num_values},
35  at::dtype<float>().device(CPU));
36  float* values_tensor_data = values_tensor.template mutable_data<float>();
37  float* percentiles_tensor_data =
38  percentiles_tensor.template mutable_data<float>();
39  for (int ind = 0; ind < num_values; ind++) {
40  values_tensor_data[ind] = value_pct_data[2 * ind];
41  percentiles_tensor_data[ind] = value_pct_data[2 * ind + 1];
42  }
43 
44  auto* percentile_values =
45  Output(PCT, original_values.sizes(), at::dtype<float>());
46  float* percentile_values_data =
47  percentile_values->template mutable_data<float>();
48 
49  int current_ind = 0;
50  int current_dist_start = 0;
51  int current_length;
52  for (int i = 0; i < num_examples; i++) {
53  current_dist_start = 0;
54 
55  for (int j = 0; j < num_features; j++) {
56  current_length = lengths_data[j];
57  const auto lower_bound =
58  std::lower_bound(
59  values_tensor_data + current_dist_start,
60  values_tensor_data + current_dist_start + current_length,
61  original_values_data[current_ind]) -
62  values_tensor_data;
63  if (lower_bound == current_dist_start + current_length) {
64  percentile_values_data[current_ind] = 1.0;
65  } else if (
66  original_values_data[current_ind] ==
67  values_tensor_data[lower_bound]) {
68  percentile_values_data[current_ind] =
69  percentiles_tensor_data[lower_bound];
70  } else if (lower_bound == current_dist_start) {
71  percentile_values_data[current_ind] = 0.0;
72  } else {
73  float lower_pct = percentiles_tensor_data[lower_bound - 1];
74  float upper_pct = percentiles_tensor_data[lower_bound];
75  float interval_length = values_tensor_data[lower_bound] -
76  values_tensor_data[lower_bound - 1];
77  float normalized_dist_to_lower = (original_values_data[current_ind] -
78  values_tensor_data[lower_bound - 1]) /
79  interval_length;
80  percentile_values_data[current_ind] =
81  lower_pct + normalized_dist_to_lower * (upper_pct - lower_pct);
82  }
83  current_dist_start += current_length;
84  current_ind++;
85  }
86  }
87  return true;
88 }
89 
90 REGISTER_CPU_OPERATOR(Percentile, PercentileOp<CPUContext>);
91 OPERATOR_SCHEMA(Percentile)
92  .NumInputs(3)
93  .NumOutputs(1)
94  .IdenticalTypeAndShapeOfInput(0)
95  .SetDoc(R"DOC(
96  This operator is used to find percentile representations for raw values, given a sample
97  set of raw values, labeled with their corresponding percentiles from the same distribution.
98  In particular, this operator takes as input a tensor of floats to find the percentile values
99  for, a 2D tensor of floats, where the first column of the tensor represents sampled values,
100  and the second column represents the percentile labels, and a tensor of integers lengths.
101 
102  This lengths tensor is used because the operator works on multiple sets of raw values at the same time. For
103  example, for an input:
104  original_values=[[3, 5, 3],[5, 1, 6]], lengths = [2, 1, 1], value_to_pct = [[3, 0.2], [5, 0.5], [1, 0.3], [3. 0.6]]
105 
106  Our operator expects that each column i of the input tensor is sampled from distribution i. Lengths tells
107  us that the first two elements in value_to_pct are sampled from distribution 1, the next is from distribution two,
108  and the last is from distribution 3. We expect the output of our operator to give us [[0.2, 1.0, 0.6], [0.5, 0.3, 1.0]].
109 
110  To calculate the percentile of an element, we check to see if its value is already mapped to
111  a percentile in value_to_pct. If so, we return that value. If not, we linearly interpolate between
112  the two closest values in value_to_pct. If the value is larger than all values in value_to_pct, we
113  return 1. If it's smaller than all the values, we return 0.
114 
115 )DOC")
116  .Input(
117  0,
118  "original_values",
119  "Input 2D tensor of floats, representing the original, raw data to calculate percentiles for.")
120  .Input(
121  1,
122  "value_to_pct",
123  "Sorted 2D tensor, with 2 columns. Each element in the first column is a float representing the"
124  " raw value of a sample. Its corresponding element in the next column represents the percentile it maps to.")
125  .Input(
126  2,
127  "lengths",
128  "1D tensor, representing the length of each distribution. We expect that the sum of elements of this tensor"
129  " is equal to the total length of value_to_pct.")
130  .Output(
131  0,
132  "percentile_values",
133  "1D tensor of floats, with the same dimensions as the flattened input tensor. Each element "
134  "of this tensor, percentile_values[i], corresponds to the percentile calculated "
135  "for original_values[i].");
136 
137 NO_GRADIENT(Percentile);
138 
139 } // namespace caffe2
void ReinitializeTensor(Tensor *tensor, at::IntArrayRef dims, at::TensorOptions options)
Reinitialize a Tensor to given dims and options if necessary, note that this will not do anything if ...
Definition: tensor.cc:127
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13