1 #include "caffe2/operators/perplexity_op.h" 6 bool PerplexityOp<float, CPUContext>::RunOnDevice() {
10 DCHECK_EQ(X.ndim(), 1);
13 Y->Resize(vector<TIndex>());
14 const auto* Xdata = X.data<
float>();
16 float perplexity = 1.0;
17 for (
int i = 0; i < N; ++i) {
18 perplexity *= pow(Xdata[i], -1.0/N);
20 *(Y->mutable_data<
float>()) = perplexity;
24 REGISTER_CPU_OPERATOR(Perplexity, PerplexityOp<float, CPUContext>);
26 OPERATOR_SCHEMA(Perplexity).NumInputs(1).NumOutputs(1)
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. 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 " 39 SHOULD_NOT_DO_GRADIENT(Perplexity);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...