1 #include "caffe2/operators/perplexity_op.h" 6 bool PerplexityOp<float, CPUContext>::RunOnDevice() {
12 auto* Y = Output(0, vector<int64_t>(), at::dtype<float>());
13 const auto* Xdata = X.data<
float>();
15 float perplexity = 1.0;
16 for (
int i = 0; i < N; ++i) {
17 perplexity *= pow(Xdata[i], -1.0/N);
19 *(Y->template mutable_data<float>()) = perplexity;
23 REGISTER_CPU_OPERATOR(Perplexity, PerplexityOp<float, CPUContext>);
25 OPERATOR_SCHEMA(Perplexity).NumInputs(1).NumOutputs(1)
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. 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 " 38 SHOULD_NOT_DO_GRADIENT(Perplexity);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...