-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPerplexityAnalyzer.hpp
More file actions
45 lines (43 loc) · 1.06 KB
/
PerplexityAnalyzer.hpp
File metadata and controls
45 lines (43 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* PerplexityAnalyzer.hpp
*
* Created on: 04/01/2013
* Author: polymorpher
*/
#pragma once
#include"util.hpp"
inline bool isnan(double x){return x!=x;}
class PerplexityAnalyzer {
int K;
int V;
const Vector2DInt& docs;
const Vector2DDouble& phi;
const Vector2DDouble& theta;
public:
PerplexityAnalyzer(const Vector2DInt &_docs, const Vector2DDouble &_phi,
const Vector2DDouble &_theta, int _K, int _V): docs(_docs),phi(_phi),theta(_theta),K(_K),V(_V){
}
double getPerplexity() {
float sum = 0;
int N = 0;
for (int d = 0; d < docs.size(); d++) {
N += docs[d].size();
double dsum = 0;
for (int l = 0; l < docs[d].size(); l++) {
double wsum = 0;
int w = docs[d][l];
for(int k=0;k<K;k++){
wsum+=theta[d][k]*phi[w][k];
if(isnan(theta[d][k]) || isnan(phi[w][k])
|| theta[d][k]<0 || phi[w][k]<0){
printf("Warning d=%d w=%d k=%d theta=%f phi=%f\n",d,w,k,theta[d][k],phi[w][k]);
}
}
dsum += log(wsum);
}
sum += dsum;
}
printf("%lf %d\n", sum, N);
return (double) exp(-sum / (double) N);
}
};