-
Notifications
You must be signed in to change notification settings - Fork 6
/
perceptron.py
407 lines (375 loc) · 15.7 KB
/
perceptron.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
__author__ = 'Daan van Stigt'
import json
import multiprocessing as mp
from ctypes import c_float
import numpy as np
from tqdm import tqdm
from features import get_features
from decode import Decoder
from parallel import worker, make_features_parallel, parse_parallel
from tokens import Token
from utils import ceil_div, softmax
class Perceptron:
"""Base methods for a perceptron."""
def __init__(self):
pass
def initialize_weights(self, feature_set):
"""Initialize weights with zero."""
self.weights = dict((f, 0) for f in feature_set)
self._totals = dict((f, 0) for f in feature_set)
self._timestamps = dict((f, 0) for f in feature_set)
def average_weights(self):
"""Average weights over all updates."""
assert self.i > 0, 'cannot average weight'
self.weights = dict((f, val/self.i) for f, val in self._totals.items())
del self._totals, self._timestamps
def prune(self, eps=1e-1):
"""Prune all features with weight smaller than eps."""
before = len(self.weights)
zeros = sum(1 for val in self.weights.values() if val == 0.0)
self.weights = dict((f, val) for f, val in self.weights.items() if abs(val) > eps)
return before, zeros
def sort(self):
"""Sort features by weight."""
self.weights = dict(sorted(self.weights.items(), reverse=True, key=lambda x: x[1]))
def top_features(self, n):
"""Return the n features with the largest weight."""
self.sort()
return list(self.weights.items())[:n]
def save(self, path, data=None, epochs=None, accuracy=None):
"""Save model features and weights in json format."""
path = path + '.json' if not path.endswith('.json') else path
self.sort()
model = {
'data': data,
'epochs': epochs,
'accuracy': accuracy,
'feature-opts': self.feature_opts,
'weights': self.weights}
with open(path, 'w') as f:
json.dump(model, f, indent=4)
def load(self, path, training=False):
"""Load model features and weights from json format."""
path = path + '.json' if not path.endswith('.json') else path
with open(path, 'r') as f:
model = json.load(f)
weights, feature_opts, accuracy = model['weights'], model['feature-opts'], model['accuracy']
self.weights = weights
self.feature_opts = feature_opts
if training:
# If we wish to continue training we will need these.
self._totals = dict((f, 0) for f in weights.keys())
self._timestamps = dict((f, 0) for f in weights.keys())
return accuracy, feature_opts
class ArcPerceptron(Perceptron):
"""Perceptron to score dependency arcs."""
def __init__(self, feature_opts=dict()):
self.feature_opts = feature_opts
self.i = 0
def make_features(self, lines, parallel=False):
"""Create the feature-set from gold head-dep pairs in the lines."""
assert isinstance(lines, list)
assert all(isinstance(line, list) for line in lines)
assert all(isinstance(token, Token) for line in lines for token in line)
if not parallel:
features = set()
for tokens in tqdm(lines):
for dep in tokens:
head = tokens[dep.head]
features.update(get_features(head, dep, tokens, **self.feature_opts))
else:
features = make_features_parallel(lines, self.feature_opts)
self.initialize_weights(features)
del features
def make_all_features(self, lines, parallel=False):
"""Create the feature-set from *all* head-dep pairs in the lines.
We need the features of _all_ possible head-dep combinations
in order to produce full score matrices at prediction time.
"""
assert isinstance(lines, list)
assert all(isinstance(line, list) for line in lines)
assert all(isinstance(token, Token) for line in lines for token in line)
if not parallel:
features = set()
for tokens in tqdm(lines):
for head in tokens:
for dep in tokens:
features.update(get_features(head, dep, tokens, **self.feature_opts))
else:
features = make_features_parallel(lines, self.feature_opts)
self.initialize_weights(features)
del features
def score(self, features):
score = 0.0
for f in features:
if f not in self.weights:
continue
score += self.weights[f]
return score
def predict(self, token, tokens, weights=None):
"""Gready head prediction used for training."""
scores = []
features = []
for head in tokens:
feats = get_features(head, token, tokens, **self.feature_opts)
score = self.score(feats)
features.append(feats)
scores.append(score)
guess = np.argmax(scores)
return guess, features
def update(self, guess_features, true_features):
def upd_feat(f, v):
if f not in self.weights:
pass
else:
# For the number of parameter updates in which we skipped this feature.
nr_iters_at_this_weight = self.i - self._timestamps[f]
self._totals[f] += nr_iters_at_this_weight * self.weights[f]
self.weights[f] += v
self._timestamps[f] = self.i
self.i += 1
for f in true_features:
upd_feat(f, 1.0)
for f in guess_features:
upd_feat(f, -1.0)
def train(self, niters, train_set):
c = 0; n = 0
for j, line in enumerate(tqdm(train_set)):
for token in line:
guess, features = self.predict(token, line)
self.update(features[guess], features[token.head])
c += guess == token.head; n += 1
return c, n
def prepare_for_parallel(self):
"""Prepare shared memory for parallel training."""
self.feature_dict = dict((f, i) for i, f in enumerate(self.weights.keys()))
self.weights = mp.Array(
c_float,
np.zeros(len(self.feature_dict)),
lock=False) # Hogwild!
def restore_from_parallel(self):
"""Restore weights dictionary from shared memory after parallel training."""
self.weights = dict((f, self.weights[i]) for f, i in self.feature_dict.items())
del self.feature_dict
def train_parallel(self, niters, lines, nprocs):
chunk_size = ceil_div(len(lines), nprocs)
partitioned = [lines[i:i+chunk_size] for i in range(0, len(lines), chunk_size)]
processes = []
for rank in range(nprocs):
p = mp.Process(
target=worker,
args=(partitioned[rank], rank, self.weights, self.feature_dict, self.feature_opts))
p.start()
processes.append(p)
for p in processes:
p.join()
class LabPerceptron(Perceptron):
"""Perceptron to score labels for dependency arcs."""
pass
# class Perceptron:
# def __init__(self, decoding='mst', feature_opts={}):
# self.decoder = Decoder(decoding)
# self.feature_opts = feature_opts
# self.i = 0
#
# def initialize_weights(self, feature_set):
# self.weights = dict((f, 0) for f in feature_set)
# self._totals = dict((f, 0) for f in feature_set)
# self._timestamps = dict((f, 0) for f in feature_set)
#
# def make_features(self, lines, parallel=False):
# """Create the feature-set from all head-dep pairs in the lines.
#
# We need the features of _all_ possible head-dep combinations
# in order to produce full score matrices at prediction time.
# Note: this can take some time if `lines` is long...
# """
# assert isinstance(lines, list)
# assert all(isinstance(line, list) for line in lines)
# assert all(isinstance(token, Token) for line in lines for token in line)
# if not parallel:
# features = set()
# for tokens in tqdm(lines):
# for head in tokens:
# for dep in tokens:
# features.update(get_features(head, dep, tokens, **self.feature_opts))
# else:
# features = make_features_parallel(lines, self.feature_opts)
# self.initialize_weights(features)
# del features
#
# def score(self, features):
# score = 0.0
# for f in features:
# if f not in self.weights:
# continue
# score += self.weights[f]
# return score
#
# def predict(self, token, tokens, weights=None):
# """Gready head prediction used for training."""
# scores = []
# features = []
# for head in tokens:
# feats = get_features(head, token, tokens, **self.feature_opts)
# score = self.score(feats)
# features.append(feats)
# scores.append(score)
# guess = np.argmax(scores)
# return guess, features
#
# def update(self, guess_features, true_features):
# def upd_feat(f, v):
# if f not in self.weights:
# pass
# else:
# # For the number of parameter updates in which we skipped this feature.
# nr_iters_at_this_weight = self.i - self._timestamps[f]
# self._totals[f] += nr_iters_at_this_weight * self.weights[f]
# self.weights[f] += v
# self._timestamps[f] = self.i
#
# self.i += 1
# for f in true_features:
# upd_feat(f, 1.0)
# for f in guess_features:
# upd_feat(f, -1.0)
#
# def train_step(self, line, structured=False):
# def structured_update(line):
# """Heads are predicted with tree algorithm."""
# c = 0
# heads, _, all_features = self.parse(line)
# for i in range(len(line)):
# token = line[i]
# guess = heads[i]
# self.update(all_features[i][guess], all_features[i][token.head])
# c += guess == token.head
# return c
#
# def greedy_update(line):
# """Heads are predicted greedily per dependant."""
# c = 0
# for token in line:
# guess, features = self.predict(token, line)
# self.update(features[guess], features[token.head])
# c += guess == token.head
# return c
#
# if structured:
# c = structured_update(line)
# else:
# c = greedy_update(line)
# return c
#
# def train(self, niters, lines, dev_set, structured=False, approx=100):
# for i in range(1, niters+1):
# c = 0; n = 0
# for j, line in enumerate(tqdm(lines)):
# correct = self.train_step(line, structured=structured)
# c += correct; n += len(line)
# train_acc, dev_acc = self.evaluate(lines[:approx]), self.evaluate(dev_set[:approx])
# print(f'| Iter {i} | Correct guess {c:,}/{n:,} ~ {c/n:.2f} | Train UAS {train_acc:.2f} | Dev UAS {dev_acc:.2f} |')
# np.random.shuffle(lines)
#
# def train_parallel(self, niters, lines, dev_set, nprocs=-1, approx=100):
# """Asynchronous lock-free (`Hogwild`) training of perceptron."""
# size = mp.cpu_count() if nprocs == -1 else nprocs
# print(f'Hogwild training with {size} processes...')
# chunk_size = ceil_div(len(lines), size)
# # Make a shared array of the weights (cannot make a shared dict),
# # but will restore this to dict after training.
# self.feature_dict = dict((f, i) for i, f in enumerate(self.weights.keys()))
# self.weights = mp.Array(
# c_float,
# np.zeros(len(self.feature_dict)),
# lock=False # Hogwild!
# )
# for i in range(1, niters+1):
# partitioned = [lines[i:i+chunk_size] for i in range(0, len(lines), chunk_size)]
# processes = []
# for rank in range(size):
# p = mp.Process(
# target=worker,
# args=(partitioned[rank], rank, self.weights, self.feature_dict, self.feature_opts))
# p.start()
# processes.append(p)
# for p in processes:
# p.join()
# # TODO: make this less hacky:
# train_acc = self.evaluate_parallel(
# lines[:approx], self.weights, self.feature_dict, self.feature_opts, self.decoder)
# dev_acc = self.evaluate_parallel(
# dev_set[:approx], self.weights, self.feature_dict, self.feature_opts, self.decoder)
# print(f'| Iter {i} | Train UAS {train_acc:.2f} | Dev UAS {dev_acc:.2f} |')
# np.random.shuffle(lines)
# # Restore weights.
# self.weights = dict((f, self.weights[i]) for f, i in self.feature_dict.items())
# del self.feature_dict
#
# def average_weights(self):
# self.weights = dict((f, val/self.i) for f, val in self._totals.items())
# del self._totals, self._timestamps
#
# def accuracy(self, pred, gold):
# return 100 * sum((p == g for p, g in zip(pred, gold))) / len(pred)
#
# def evaluate(self, lines):
# acc = 0.0
# for line in lines:
# pred, _, _ = self.parse(line)
# gold = [token.head for token in line]
# acc += self.accuracy(pred, gold)
# return acc / len(lines)
#
# def evaluate_parallel(self, lines, weights, feature_dict, feature_opts, decoder):
# acc = 0.0
# for line in lines:
# pred, _, _ = parse_parallel(line, weights, feature_dict, feature_opts, decoder)
# gold = [token.head for token in line]
# acc += self.accuracy(pred, gold)
# return acc / len(lines)
#
# def parse(self, tokens):
# score_matrix = np.zeros((len(tokens), len(tokens)))
# all_features = dict()
# for i, dep in enumerate(tokens):
# all_features[i] = dict()
# for j, head in enumerate(tokens):
# features = get_features(head, dep, tokens, **self.feature_opts)
# score = self.score(features)
# score_matrix[i][j] = score
# all_features[i][j] = features
# probs = softmax(score_matrix)
# heads = self.decoder(probs)
# return heads, probs, all_features
#
# def prune(self, eps=1e-1):
# before = len(self.weights)
# zeros = sum(1 for val in self.weights.values() if val == 0.0)
# self.weights = dict((f, val) for f, val in self.weights.items() if abs(val) > eps)
# return before, zeros
#
# def sort(self):
# """Sort features by weight."""
# self.weights = dict(sorted(self.weights.items(), reverse=True, key=lambda x: x[1]))
#
# def save(self, path):
# path = path + '.json' if not path.endswith('.json') else path
# self.sort()
# with open(path, 'w') as f:
# json.dump(self.weights, f, indent=4)
#
# def load(self, path, training=False):
# path = path + '.json' if not path.endswith('.json') else path
# with open(path, 'r') as f:
# weights = json.load(f)
# self.weights = weights
# if training:
# # If we wish to continue training we will need these.
# self._totals = dict((f, 0) for f in weights.keys())
# self._timestamps = dict((f, 0) for f in weights.keys())
#
# def top_features(self, n):
# self.sort()
# return list(self.weights.items())[:n]