forked from henrishi/bm_model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyro_model.py
222 lines (179 loc) · 9.81 KB
/
pyro_model.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
"""
Pyro models for person-item response
Author: Zhaolei (Henry) Shi -- [email protected]
"""
from pdb import set_trace
import torch
from torch.distributions import constraints
import pyro
import pyro.distributions as dist
import pyro.optim as optim
def sigmoid_torch(z):
return (1. / (1. + torch.exp(- z)))
"""
The 2 parameter traditional IRT model
"""
def pred_fn_2param(theta, alpha, beta, stu_id, ques_id):
theta_i = theta[stu_id]
alpha_j = alpha[ques_id]
beta_j = beta[ques_id]
prob = sigmoid_torch(alpha_j * (theta_i - beta_j))
return prob
def model_2param(ques_id, stu_id, correct, n_ques = None, n_stu = None, subsample_size = None):
if not n_ques: n_ques = torch.unique(ques_id).size()[0]
if not n_stu: n_stu = torch.unique(stu_id).size()[0]
# link params to models defined in model function
theta = pyro.sample("theta", dist.Normal(torch.zeros(n_stu), torch.ones(n_stu)).independent(1))
alpha = pyro.sample("alpha", dist.Normal(torch.zeros(n_ques), torch.ones(n_ques)).independent(1))
beta = pyro.sample("beta", dist.Normal(torch.zeros(n_ques), torch.ones(n_ques)).independent(1))
# actual parameters corresponding to data
prob = pred_fn_2param(theta, alpha, beta, stu_id, ques_id)
if subsample_size:
with pyro.plate("data", size = len(correct), subsample_size = subsample_size) as ind:
pyro.sample("obs", dist.Bernoulli(prob[ind]), obs=correct[ind])
else:
with pyro.plate("data", len(correct)):
pyro.sample("obs", dist.Bernoulli(prob), obs=correct)
def guide_2param(ques_id, stu_id, correct, n_ques = None, n_stu = None, subsample_size = None):
if not n_ques: n_ques = torch.unique(ques_id).size()[0]
if not n_stu: n_stu = torch.unique(stu_id).size()[0]
theta_loc = pyro.param('theta_loc', torch.randn(n_stu, dtype = torch.float64) / 100.)
theta_scale = pyro.param('theta_scale', torch.ones(n_stu, dtype = torch.float64),
constraint=constraints.positive)
alpha_loc = pyro.param('alpha_loc', torch.ones(n_ques, dtype = torch.float64),
constraint=constraints.positive)
alpha_scale = pyro.param('alpha_scale', torch.ones(n_ques, dtype = torch.float64),
constraint=constraints.positive)
beta_loc = pyro.param('beta_loc', torch.randn(n_ques, dtype = torch.float64) / 100.)
beta_scale = pyro.param('beta_scale', torch.ones(n_ques, dtype = torch.float64),
constraint=constraints.positive)
# link params to models defined in model function
pyro.sample("theta", dist.Normal(theta_loc, theta_scale).independent(1))
pyro.sample("alpha", dist.Normal(alpha_loc, alpha_scale).independent(1))
pyro.sample("beta", dist.Normal(beta_loc, beta_scale).independent(1))
"""
Factorization model
"""
def pred_fn_factorization(theta, alpha, beta, stu_id, ques_id):
theta_i = theta[stu_id, :]
alpha_j = alpha[ques_id, :]
beta_j = beta[ques_id]
prob = sigmoid_torch(torch.sum(alpha_j * theta_i, dim = 1) - beta_j)
return prob
def model_factorization(ques_id, stu_id, correct, n_latent, n_ques = None, n_stu = None):
if not n_ques: n_ques = torch.unique(ques_id).size()[0]
if not n_stu: n_stu = torch.unique(stu_id).size()[0]
# link params to models defined in model function
theta = pyro.sample("theta", dist.Normal(torch.zeros(n_stu, n_latent), torch.ones(n_stu, n_latent)).independent(2))
alpha = pyro.sample("alpha", dist.Normal(torch.zeros(n_ques, n_latent), torch.ones(n_ques, n_latent)).independent(2))
beta = pyro.sample("beta", dist.Normal(torch.zeros(n_ques), torch.ones(n_ques)).independent(1))
# actual parameters corresponding to data
prob = pred_fn_factorization(theta, alpha, beta, stu_id, ques_id)
with pyro.plate("data", len(correct)):
pyro.sample("obs", dist.Bernoulli(prob), obs=correct)
def guide_factorization(ques_id, stu_id, correct, n_latent, n_ques = None, n_stu = None):
if not n_ques: n_ques = torch.unique(ques_id).size()[0]
if not n_stu: n_stu = torch.unique(stu_id).size()[0]
theta_loc = pyro.param('theta_loc', torch.randn(n_stu, n_latent, dtype = torch.float64) / 100.)
theta_scale = pyro.param('theta_scale', torch.ones(n_stu, n_latent, dtype = torch.float64),
constraint=constraints.positive)
alpha_loc = pyro.param('alpha_loc', torch.ones(n_ques, n_latent, dtype = torch.float64),
constraint=constraints.positive)
alpha_scale = pyro.param('alpha_scale', torch.ones(n_ques, n_latent, dtype = torch.float64),
constraint=constraints.positive)
beta_loc = pyro.param('beta_loc', torch.randn(n_ques, dtype = torch.float64) / 100.)
beta_scale = pyro.param('beta_scale', torch.ones(n_ques, dtype = torch.float64),
constraint=constraints.positive)
# link params to models defined in model function
pyro.sample("theta", dist.Normal(theta_loc, theta_scale).independent(2))
pyro.sample("alpha", dist.Normal(alpha_loc, alpha_scale).independent(2))
pyro.sample("beta", dist.Normal(beta_loc, beta_scale).independent(1))
"""
Hierarchical factorization model (using attributes)
"""
def pred_fn_hierarchical(theta, alpha, beta, ques_hmat_list, stu_id, ques_id, ques_attrib_list):
hier = []
for ques_hmat, ques_attrib in zip(ques_hmat_list, ques_attrib_list):
# ques_attrib are composed of n_obs * n_var matrices
# ques_hmat_list are composed of n_var * ques_n_latent matrices
hier.append(torch.matmul(ques_attrib, ques_hmat))
theta_i = theta[stu_id, :]
alpha_j = alpha[ques_id, :]
hier_j = [x[ques_id, :] for x in hier]
alphahier_j = torch.cat([alpha_j] + hier_j, dim = 1)
beta_j = beta[ques_id]
prob = sigmoid_torch(torch.sum(alphahier_j * theta_i, dim = 1) - beta_j)
return prob
def model_hierarchical(ques_id, stu_id, correct, n_latent, ques_attrib_list, ques_attrib_n_latent, n_ques = None, n_stu = None):
if not n_ques: n_ques = torch.unique(ques_id).size()[0]
if not n_stu: n_stu = torch.unique(stu_id).size()[0]
# attribute and trasformation matrices
ques_hmat_list = []
counter = 0
total_ques_n_latent = 0
for ques_attrib, ques_n_latent in zip(ques_attrib_list, ques_attrib_n_latent):
n_var = ques_attrib.shape[1]
ques_hmat_list.append(
pyro.sample(
"ques_hmat_{}".format(counter),
dist.Normal(torch.zeros(n_var, ques_n_latent), torch.ones(n_var, ques_n_latent)).independent(2)
))
counter += 1
total_ques_n_latent += ques_n_latent
# latent parameters
theta = pyro.sample("theta", dist.Normal(torch.zeros(n_stu, n_latent + total_ques_n_latent), torch.ones(n_stu, n_latent + total_ques_n_latent)).independent(2))
alpha = pyro.sample("alpha", dist.Normal(torch.zeros(n_ques, n_latent), torch.ones(n_ques, n_latent)).independent(2))
beta = pyro.sample("beta", dist.Normal(torch.zeros(n_ques), torch.ones(n_ques)).independent(1))
# actual parameters corresponding to data
prob = pred_fn_hierarchical(
theta = theta, alpha = alpha, beta = beta,
ques_hmat_list = ques_hmat_list, stu_id = stu_id,
ques_id = ques_id, ques_attrib_list = ques_attrib_list
)
with pyro.plate("data", len(correct)):
pyro.sample("obs", dist.Bernoulli(prob), obs=correct)
def guide_hierarchical(ques_id, stu_id, correct, n_latent, ques_attrib, ques_attrib_n_latent, n_ques = None, n_stu = None):
if not n_ques: n_ques = torch.unique(ques_id).size()[0]
if not n_stu: n_stu = torch.unique(stu_id).size()[0]
ques_hmat_guides = {}
counter = 0
total_ques_n_latent = 0
for ques_attrib, ques_n_latent in zip(ques_attrib, ques_attrib_n_latent):
n_var = ques_attrib.shape[1]
hmat_name = "ques_hmat_{}".format(counter)
ques_hmat_guides[hmat_name] = {
'loc' : pyro.param(
'{}_loc'.format(hmat_name),
torch.ones(n_var, ques_n_latent, dtype = torch.float64),
constraint=constraints.positive
),
'scale' : pyro.param(
'{}_scale'.format(hmat_name),
torch.ones(n_var, ques_n_latent, dtype = torch.float64),
constraint=constraints.positive
)
}
counter += 1
total_ques_n_latent += ques_n_latent
theta_loc = pyro.param('theta_loc', torch.randn(n_stu, n_latent + total_ques_n_latent, dtype = torch.float64) / 100.)
theta_scale = pyro.param('theta_scale', torch.ones(n_stu, n_latent + total_ques_n_latent, dtype = torch.float64),
constraint=constraints.positive)
alpha_loc = pyro.param('alpha_loc', torch.ones(n_ques, n_latent, dtype = torch.float64),
constraint=constraints.positive)
alpha_scale = pyro.param('alpha_scale', torch.ones(n_ques, n_latent, dtype = torch.float64),
constraint=constraints.positive)
beta_loc = pyro.param('beta_loc', torch.randn(n_ques, dtype = torch.float64) / 100.)
beta_scale = pyro.param('beta_scale', torch.ones(n_ques, dtype = torch.float64),
constraint=constraints.positive)
# link params to models defined in model function
for hmat_name in ques_hmat_guides:
pyro.sample(
hmat_name,
dist.Normal(
ques_hmat_guides[hmat_name]['loc'],
ques_hmat_guides[hmat_name]['scale']
).independent(2)
)
pyro.sample("theta", dist.Normal(theta_loc, theta_scale).independent(2))
pyro.sample("alpha", dist.Normal(alpha_loc, alpha_scale).independent(2))
pyro.sample("beta", dist.Normal(beta_loc, beta_scale).independent(1))