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 
9  DCHECK_EQ(X.dim(), 1);
10  int N = X.dim32(0);
11 
12  auto* Y = Output(0, vector<int64_t>(), at::dtype<float>());
13  const auto* Xdata = X.data<float>();
14 
15  float perplexity = 1.0;
16  for (int i = 0; i < N; ++i) {
17  perplexity *= pow(Xdata[i], -1.0/N);
18  }
19  *(Y->template mutable_data<float>()) = perplexity;
20  return true;
21 }
22 
23 REGISTER_CPU_OPERATOR(Perplexity, PerplexityOp<float, CPUContext>);
24 
25 OPERATOR_SCHEMA(Perplexity).NumInputs(1).NumOutputs(1)
26 .SetDoc(R"DOC(
27 Perplexity calculates how well a probability distribution predicts a sample.
28 Perplexity takes a 1-D tensor containing a batch of probabilities. Each value
29 in the tensor belongs to a different sample and represents the probability of
30 the model predicting the true label for that sample. The operator returns a
31 single (float) perplexity value for the batch.
32 )DOC")
33 .Input(0, "probabilities", "The input data as Tensor. It contains a batch of"
34  "true label or target probabilities")
35 .Output(0, "output", "The output- a single (float) perplexity value for the "
36  "batch");
37 
38 SHOULD_NOT_DO_GRADIENT(Perplexity);
39 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13