-
Notifications
You must be signed in to change notification settings - Fork 88
/
experiment.py
304 lines (265 loc) · 11.4 KB
/
experiment.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
Copyright (c) 2016 Duolingo Inc. MIT Licence.
Python script that implements spaced repetition models from Settles & Meeder (2016).
Recommended to run with pypy for efficiency. See README.
"""
import argparse
import csv
import gzip
import math
import os
import random
import sys
from collections import defaultdict, namedtuple
# various constraints on parameters and outputs
MIN_HALF_LIFE = 15.0 / (24 * 60) # 15 minutes
MAX_HALF_LIFE = 274. # 9 months
LN2 = math.log(2.)
# data instance object
Instance = namedtuple('Instance', 'p t fv h a lang right wrong ts uid lexeme'.split())
class SpacedRepetitionModel(object):
"""
Spaced repetition model. Implements the following approaches:
- 'hlr' (half-life regression; trainable)
- 'lr' (logistic regression; trainable)
- 'leitner' (fixed)
- 'pimsleur' (fixed)
"""
def __init__(self, method='hlr', omit_h_term=False, initial_weights=None, lrate=.001, hlwt=.01, l2wt=.1, sigma=1.):
self.method = method
self.omit_h_term = omit_h_term
self.weights = defaultdict(float)
if initial_weights is not None:
self.weights.update(initial_weights)
self.fcounts = defaultdict(int)
self.lrate = lrate
self.hlwt = hlwt
self.l2wt = l2wt
self.sigma = sigma
def halflife(self, inst, base):
try:
dp = sum([self.weights[k]*x_k for (k, x_k) in inst.fv])
return hclip(base ** dp)
except:
return MAX_HALF_LIFE
def predict(self, inst, base=2.):
if self.method == 'hlr':
h = self.halflife(inst, base)
p = 2. ** (-inst.t/h)
return pclip(p), h
elif self.method == 'leitner':
try:
h = hclip(2. ** inst.fv[0][1])
except OverflowError:
h = MAX_HALF_LIFE
p = 2. ** (-inst.t/h)
return pclip(p), h
elif self.method == 'pimsleur':
try:
h = hclip(2. ** (2.35*inst.fv[0][1] - 16.46))
except OverflowError:
h = MAX_HALF_LIFE
p = 2. ** (-inst.t/h)
return pclip(p), h
elif self.method == 'lr':
dp = sum([self.weights[k]*x_k for (k, x_k) in inst.fv])
p = 1./(1+math.exp(-dp))
return pclip(p), random.random()
else:
raise Exception
def train_update(self, inst):
if self.method == 'hlr':
base = 2.
p, h = self.predict(inst, base)
dlp_dw = 2.*(p-inst.p)*(LN2**2)*p*(inst.t/h)
dlh_dw = 2.*(h-inst.h)*LN2*h
for (k, x_k) in inst.fv:
rate = (1./(1+inst.p)) * self.lrate / math.sqrt(1 + self.fcounts[k])
# rate = self.lrate / math.sqrt(1 + self.fcounts[k])
# sl(p) update
self.weights[k] -= rate * dlp_dw * x_k
# sl(h) update
if not self.omit_h_term:
self.weights[k] -= rate * self.hlwt * dlh_dw * x_k
# L2 regularization update
self.weights[k] -= rate * self.l2wt * self.weights[k] / self.sigma**2
# increment feature count for learning rate
self.fcounts[k] += 1
elif self.method == 'leitner' or self.method == 'pimsleur':
pass
elif self.method == 'lr':
p, _ = self.predict(inst)
err = p - inst.p
for (k, x_k) in inst.fv:
# rate = (1./(1+inst.p)) * self.lrate / math.sqrt(1 + self.fcounts[k])
rate = self.lrate / math.sqrt(1 + self.fcounts[k])
# error update
self.weights[k] -= rate * err * x_k
# L2 regularization update
self.weights[k] -= rate * self.l2wt * self.weights[k] / self.sigma**2
# increment feature count for learning rate
self.fcounts[k] += 1
def train(self, trainset):
if self.method == 'leitner' or self.method == 'pimsleur':
return
random.shuffle(trainset)
for inst in trainset:
self.train_update(inst)
def losses(self, inst):
p, h = self.predict(inst)
slp = (inst.p - p)**2
slh = (inst.h - h)**2
return slp, slh, p, h
def eval(self, testset, prefix=''):
results = {'p': [], 'h': [], 'pp': [], 'hh': [], 'slp': [], 'slh': []}
for inst in testset:
slp, slh, p, h = self.losses(inst)
results['p'].append(inst.p) # ground truth
results['h'].append(inst.h)
results['pp'].append(p) # predictions
results['hh'].append(h)
results['slp'].append(slp) # loss function values
results['slh'].append(slh)
mae_p = mae(results['p'], results['pp'])
mae_h = mae(results['h'], results['hh'])
cor_p = spearmanr(results['p'], results['pp'])
cor_h = spearmanr(results['h'], results['hh'])
total_slp = sum(results['slp'])
total_slh = sum(results['slh'])
total_l2 = sum([x**2 for x in self.weights.values()])
total_loss = total_slp + self.hlwt*total_slh + self.l2wt*total_l2
if prefix:
sys.stderr.write('%s\t' % prefix)
sys.stderr.write('%.1f (p=%.1f, h=%.1f, l2=%.1f)\tmae(p)=%.3f\tcor(p)=%.3f\tmae(h)=%.3f\tcor(h)=%.3f\n' % \
(total_loss, total_slp, self.hlwt*total_slh, self.l2wt*total_l2, \
mae_p, cor_p, mae_h, cor_h))
def dump_weights(self, fname):
with open(fname, 'wb') as f:
for (k, v) in self.weights.iteritems():
f.write('%s\t%.4f\n' % (k, v))
def dump_predictions(self, fname, testset):
with open(fname, 'wb') as f:
f.write('p\tpp\th\thh\tlang\tuser_id\ttimestamp\n')
for inst in testset:
pp, hh = self.predict(inst)
f.write('%.4f\t%.4f\t%.4f\t%.4f\t%s\t%s\t%d\n' % (inst.p, pp, inst.h, hh, inst.lang, inst.uid, inst.ts))
def dump_detailed_predictions(self, fname, testset):
with open(fname, 'wb') as f:
f.write('p\tpp\th\thh\tlang\tuser_id\ttimestamp\tlexeme_tag\n')
for inst in testset:
pp, hh = self.predict(inst)
for i in range(inst.right):
f.write('1.0\t%.4f\t%.4f\t%.4f\t%s\t%s\t%d\t%s\n' % (pp, inst.h, hh, inst.lang, inst.uid, inst.ts, inst.lexeme))
for i in range(inst.wrong):
f.write('0.0\t%.4f\t%.4f\t%.4f\t%s\t%s\t%d\t%s\n' % (pp, inst.h, hh, inst.lang, inst.uid, inst.ts, inst.lexeme))
def pclip(p):
# bound min/max model predictions (helps with loss optimization)
return min(max(p, 0.0001), .9999)
def hclip(h):
# bound min/max half-life
return min(max(h, MIN_HALF_LIFE), MAX_HALF_LIFE)
def mae(l1, l2):
# mean average error
return mean([abs(l1[i] - l2[i]) for i in range(len(l1))])
def mean(lst):
# the average of a list
return float(sum(lst))/len(lst)
def spearmanr(l1, l2):
# spearman rank correlation
m1 = mean(l1)
m2 = mean(l2)
num = 0.
d1 = 0.
d2 = 0.
for i in range(len(l1)):
num += (l1[i]-m1)*(l2[i]-m2)
d1 += (l1[i]-m1)**2
d2 += (l2[i]-m2)**2
return num/math.sqrt(d1*d2)
def read_data(input_file, method, omit_bias=False, omit_lexemes=False, max_lines=None):
# read learning trace data in specified format, see README for details
sys.stderr.write('reading data...')
instances = list()
if input_file.endswith('gz'):
f = gzip.open(input_file, 'rb')
else:
f = open(input_file, 'rb')
reader = csv.DictReader(f)
for i, row in enumerate(reader):
if max_lines is not None and i >= max_lines:
break
p = pclip(float(row['p_recall']))
t = float(row['delta'])/(60*60*24) # convert time delta to days
h = hclip(-t/(math.log(p, 2)))
lang = '%s->%s' % (row['ui_language'], row['learning_language'])
lexeme_id = row['lexeme_id']
lexeme_string = row['lexeme_string']
timestamp = int(row['timestamp'])
user_id = row['user_id']
seen = int(row['history_seen'])
right = int(row['history_correct'])
wrong = seen - right
right_this = int(row['session_correct'])
wrong_this = int(row['session_seen']) - right_this
# feature vector is a list of (feature, value) tuples
fv = []
# core features based on method
if method == 'leitner':
fv.append((intern('diff'), right-wrong))
elif method == 'pimsleur':
fv.append((intern('total'), right+wrong))
else:
# fv.append((intern('right'), right))
# fv.append((intern('wrong'), wrong))
fv.append((intern('right'), math.sqrt(1+right)))
fv.append((intern('wrong'), math.sqrt(1+wrong)))
# optional flag features
if method == 'lr':
fv.append((intern('time'), t))
if not omit_bias:
fv.append((intern('bias'), 1.))
if not omit_lexemes:
fv.append((intern('%s:%s' % (row['learning_language'], lexeme_string)), 1.))
instances.append(Instance(p, t, fv, h, (right+2.)/(seen+4.), lang, right_this, wrong_this, timestamp, user_id, lexeme_string))
if i % 1000000 == 0:
sys.stderr.write('%d...' % i)
sys.stderr.write('done!\n')
splitpoint = int(0.9 * len(instances))
return instances[:splitpoint], instances[splitpoint:]
argparser = argparse.ArgumentParser(description='Fit a SpacedRepetitionModel to data.')
argparser.add_argument('-b', action="store_true", default=False, help='omit bias feature')
argparser.add_argument('-l', action="store_true", default=False, help='omit lexeme features')
argparser.add_argument('-t', action="store_true", default=False, help='omit half-life term')
argparser.add_argument('-m', action="store", dest="method", default='hlr', help="hlr, lr, leitner, pimsleur")
argparser.add_argument('-x', action="store", dest="max_lines", type=int, default=None, help="maximum number of lines to read (for dev)")
argparser.add_argument('input_file', action="store", help='log file for training')
if __name__ == "__main__":
args = argparser.parse_args()
# model diagnostics
sys.stderr.write('method = "%s"\n' % args.method)
if args.b:
sys.stderr.write('--> omit_bias\n')
if args.l:
sys.stderr.write('--> omit_lexemes\n')
if args.t:
sys.stderr.write('--> omit_h_term\n')
# read data set
trainset, testset = read_data(args.input_file, args.method, args.b, args.l, args.max_lines)
sys.stderr.write('|train| = %d\n' % len(trainset))
sys.stderr.write('|test| = %d\n' % len(testset))
# train model & print preliminary evaluation info
model = SpacedRepetitionModel(method=args.method, omit_h_term=args.t)
model.train(trainset)
model.eval(testset, 'test')
# write out model weights and predictions
filebits = [args.method] + \
[k for k, v in sorted(vars(args).iteritems()) if v is True] + \
[os.path.splitext(os.path.basename(args.input_file).replace('.gz', ''))[0]]
if args.max_lines is not None:
filebits.append(str(args.max_lines))
filebase = '.'.join(filebits)
if not os.path.exists('results/'):
os.makedirs('results/')
model.dump_weights('results/'+filebase+'.weights')
model.dump_predictions('results/'+filebase+'.preds', testset)
# model.dump_detailed_predictions('results/'+filebase+'.detailed', testset)