/
my_maxent_backup.py
71 lines (59 loc) · 1.78 KB
/
my_maxent_backup.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
from scipy.optimize import minimize as mymin
class MyMaxEnt(object):
'''
Python Class for the Maximum Entropy Classifier
'''
def __init__(self,hist_list,feature_fn_list,tags):
'''
Initialises the Max Ent model by producing the feature vectors for the training data
'''
self.hist_list = hist_list
self.tags = tags
self.fvectors = self.create_dataset(feature_fn_list)
s = np.array([0]*10)
for(i in self.fvectors.values()):
np.add(s,i)
self.cum_f = s
def init_model(self):
'''
Initialises the model parameter
'''
self.model = np.array([0]*10)
def cost(self,model):
'''
Given the model, compute the cost
'''
# return L(v)
L_of_v = sum([math.log(self.p_y_given_x(i,tag)) for i in self.fvectors.keys() for tag in tags])
return L_of_v
def train(self):
'''
Train the classifier
'''
params = mymin(self.cost, self.model, method = 'L-BFGS-B', jac = gradient)
self.model = params.x
def p_y_given_x(self,h,tag):
'''
Take the history tuple and the required tag as the input and return the probability
'''
def classify(self,h):
'''
Performs the classification by determining the tag that maximizes the probability
'''
def gradient(self):
'''
Maximizes the log-likelihood(Minimizes the negative)
'''
temp = np.array([0]*10)
for h in self.fvectors.keys():
for tag in fv.keys():
np.add(temp, (self.fvectors[h][tag] * self.p_y_given_x(h, tag)))
derivative = temp - self.cum_f
return derivative
def create_dataset(self, feature_fn_list):
dataset = {k:v for k in self.hist_list for v in [{}]}
for i in self.hist_list:
for tag in self.tags:
dataset[i][tag] = [fun() for fun in feature_fn_list]
return dataset