Caffe2 - C++ API
A deep learning, cross platform ML framework
perplexity_op.cc
1 #include "caffe2/operators/perplexity_op.h"
2 
3 namespace caffe2 {
4 
5 template <>
6 bool PerplexityOp<float, CPUContext>::RunOnDevice() {
7  auto& X = Input(0);
8  auto* Y = Output(0);
9 
10  DCHECK_EQ(X.ndim(), 1);
11  int N = X.dim32(0);
12 
13  Y->Resize(vector<TIndex>());
14  const auto* Xdata = X.data<float>();
15 
16  float perplexity = 1.0;
17  for (int i = 0; i < N; ++i) {
18  perplexity *= pow(Xdata[i], -1.0/N);
19  }
20  *(Y->mutable_data<float>()) = perplexity;
21  return true;
22 }
23 
24 REGISTER_CPU_OPERATOR(Perplexity, PerplexityOp<float, CPUContext>);
25 
26 OPERATOR_SCHEMA(Perplexity).NumInputs(1).NumOutputs(1)
27 .SetDoc(R"DOC(
28 Perplexity calculates how well a probability distribution predicts a sample.
29 Perplexity takes a 1-D tensor containing a batch of probabilities. Each value
30 in the tensor belongs to a different sample and represents the probability of
31 the model predicting the true label for that sample. The operator returns a
32 single (float) perplexity value for the batch.
33 )DOC")
34 .Input(0, "probabilities", "The input data as Tensor. It contains a batch of"
35  "true label or target probabilities")
36 .Output(0, "output", "The output- a single (float) perplexity value for the "
37  "batch");
38 
39 SHOULD_NOT_DO_GRADIENT(Perplexity);
40 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...