-
Notifications
You must be signed in to change notification settings - Fork 32
/
sampler.py
229 lines (206 loc) · 7.5 KB
/
sampler.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from constants import *
import numpy as np
# Joint aspect distribution
def joint_aspect(u, m):
"""
Returns the joint aspect distribution
"""
u_a = theta_u[u].T
m_a = theta_m[m].T
um_a = np.exp(np.add(u_a, m_a))
theta_um = um_a / np.sum(um_a)
return theta_um
def predicted_rating(u, m):
"""
Computes the predicted rating for user u on movie m
"""
theta_um = joint_aspect(u, m)
temp = np.diag((np.dot(M_a.T, theta_um)).reshape(K))
r = v_u[u].dot(temp).dot(v_m[m].T) + b_o + b_u[u] + b_m[m]
return r.sum()
def predicted_aspect_rating(u, m, a):
"""
Computes the predicted rating for user u on movie m and aspect a
"""
temp = np.diag(M_a[a])
r = v_u[u].dot(temp).dot(v_m[m].T) + b_o + b_u[u] + b_m[m]
return r.sum()
def aspect_sentiment_probability(s, u, m, a):
"""
Computes the probability for a sentiment s on aspect a
for user u on movie m
"""
ruma = predicted_aspect_rating(u,m,a)
prob_suma = 1.0 / (1.0 + np.exp(-s*(c*ruma - b)))
return prob_suma
def aggregate_sentiment_probability(s, u, m):
"""
Computes the probability for aggregate sentiment s
for user u and movie m
"""
rum = predicted_rating(u,m)
prob_sum = 1.0 / (1.0 + np.exp(-s*(c*rum - b)))
return prob_sum
def sample_multinomial(w):
"""
Returns the index of a sample from a multinomial distribution
"""
x = np.random.uniform(0,1)
for i,v in enumerate(np.cumsum(w)):
if x < v: return i
return len(w)-1
def sample_multiple_indices(p):
"""
Samples indices from a joint probability distribution
"""
(Y, Z, S) = p.shape
dist = list()
for y in xrange(Y):
for z in xrange(Z):
for s in xrange(S):
dist.append(p[y,z,s])
index = sample_multinomial(dist)
y = index / (Z * S)
rem = index % (Z * S)
z = rem / S
s = rem % S
return (y, z, s)
def word_indices(vec):
"""
Returns non-zero entries of vec one at a time
"""
for idx in vec.nonzero()[0]:
for i in xrange(int(vec[idx])):
yield idx
class GibbsSampler:
"""
Class to handle Gibbs Sampling
"""
def __init__(self, Y, Z, S):
"""
Constructor
"""
self.Y = Y
self.Z = Z
self.S = S
self.M = M
def _initialize(self, matrix):
"""
Initialize all variables needed in the run step
"""
(self.n_reviews, self.vocab_size) = matrix.shape
# Number of times y occurs
self.cy = np.zeros(self.Y)
self.c = 0
# Number of times y occurs with w
self.cyw = np.zeros((self.Y, self.vocab_size))
# Number of times y occurs with s and w
self.cysw = np.zeros((self.Y, self.S, self.vocab_size))
# Number of times y occurs with s
self.cys = np.zeros((self.Y, self.S))
# Number of times y occurs with z and w
self.cyzw = np.zeros((self.Y, self.Z, self.vocab_size))
# Number of times y occurs with z
self.cyz = np.zeros((self.Y, self.Z))
# Number of times y occurs with m and w
self.cymw = np.zeros((self.Y, self.M, self.vocab_size))
# Number of times y occurs with m
self.cym = np.zeros((self.Y, self.M))
self.topics = {}
for r in xrange(self.n_reviews):
for i, w in enumerate(word_indices(matrix[r, :])):
# Choose a random assignment of y, z, w
(y, z, s) = (np.random.randint(self.Y), np.random.randint(self.Z), np.random.randint(self.S))
# Assign new values
self.cy[y] += 1
self.c += 1
self.cyw[y,w] += 1
self.cy[y] += 1
self.cysw[y,s,w] += 1
self.cys[y,s] += 1
self.cyzw[y,z,w] += 1
self.cyz[y,z] += 1
# TODO: Define m
m = np.random.randint(self.M)
self.cymw[y,m,w] += 1
self.cym[y,m] += 1
self.topics[(r, i)] = (y, z, s)
def _conditional_distribution(self, u, m, w):
"""
Returns the CPD for word w in the review by user u for movie m
"""
p_z = np.zeros((self.Y, self.Z, self.S))
# y = 0
for z in xrange(self.Z):
for s in xrange(self.S):
p_z[0,z,s] = (self.cy[0] + gamma) / (self.c + 5 * gamma)
p_z[0,z,s] = (p_z[0,z,s] * (self.cyw[0,w] + eta)) / (self.cy[0] + eta)
# y = 1
for z in xrange(self.Z):
for s in xrange(self.S):
p_z[1,z,s] = (self.cy[1] + gamma) / (self.c + 5 * gamma)
p_z[1,z,s] = (p_z[1,z,s] * (self.cysw[1,s,w] + eta)) / (self.cys[1,s] + eta)
p_z[1,z,s] = p_z[1,z,s] * aggregate_sentiment_probability(s,u,m)
# y = 2
for z in xrange(self.Z):
for s in xrange(self.S):
p_z[2,z,s] = (self.cy[2] + gamma) / (self.c + 5 * gamma)
p_z[2,z,s] = (p_z[2,z,s] * (self.cyzw[2,z,w] + eta)) / (self.cyz[2,z] + eta)
p_z[2,z,s] = p_z[2,z,s] * (joint_aspect(u, m)[z])
p_z[2,z,s] = p_z[2,z,s] * aspect_sentiment_probability(s,u,m,z)
# y = 3
for z in xrange(self.Z):
for s in xrange(self.S):
p_z[3,z,s] = (self.cy[3] + gamma) / (self.c + 5 * gamma)
p_z[3,z,s] = (p_z[3,z,s] * (self.cyzw[3,z,w] + eta)) / (self.cyz[3,z] + eta)
p_z[3,z,s] = p_z[3,z,s] * (joint_aspect(u,m)[z])
# y = 4
for z in xrange(self.Z):
for s in xrange(self.S):
p_z[4,z,s] = (self.cy[4] + gamma) / (self.c + 5 * gamma)
p_z[4,z,s] = (p_z[4,z,s] * (self.cymw[4,m,w] + eta)) / (self.cym[4,m] + eta)
# Normalize
p_z = p_z / p_z.sum()
return p_z
def run(self, matrix, max_iter=20):
"""
Perform sampling max_iter times
"""
self._initialize(matrix)
for it in xrange(max_iter):
print 'Gibbs Sampling Iteration: %d' % it
for r in xrange(self.n_reviews):
for i, w in enumerate(word_indices(matrix[r, :])):
(y, z, s) = self.topics[(r, i)]
# Exclude current assignment
self.cy[y] -= 1
self.c -= 1
self.cyw[y,w] -= 1
self.cy[y] -= 1
self.cysw[y,s,w] -= 1
self.cys[y,s] -= 1
self.cyzw[y,z,w] -= 1
self.cyz[y,z] -= 1
# TODO: Define m
m = np.random.randint(self.M)
self.cymw[y,m,w] -= 1
self.cym[y,m] -= 1
# Get next distribution
# TODO: Define u
u = np.random.randint(1000)
p_z = self._conditional_distribution(u, m, w)
(y, z, s) = sample_multiple_indices(p_z)
# Assign new values
self.cy[y] += 1
self.c += 1
self.cyw[y,w] += 1
self.cy[y] += 1
self.cysw[y,s,w] += 1
self.cys[y,s] += 1
self.cyzw[y,z,w] += 1
self.cyz[y,z] += 1
# TODO: Define m
m = np.random.randint(self.M)
self.cymw[y,m,w] += 1
self.cym[y,m] += 1
self.topics[(r, i)] = (y, z, s)