-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrain.py
More file actions
577 lines (470 loc) · 22.1 KB
/
train.py
File metadata and controls
577 lines (470 loc) · 22.1 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import torch
import json
import os
import pandas as pd
import numpy as np
import tqdm.auto as tqdm
from model import TriangleModel,clipped_sigmoid
from typing import List,Optional,Dict,Tuple,Callable,Union
BOUND = int(os.environ.get('BOUND',15))
### Clamp value for output
def invert_binary(tensor : torch.Tensor) -> torch.Tensor:
'''
Replace values of a binary tensor w/ +- BOUND
Args:
tensor (torch.Tensor) : binary values
Returns:
torch.Tensor with the same size as [tensor], but with
all 1's set to BOUND and all 0's set to -BOUND.
'''
new_tensor = BOUND * torch.ones_like(tensor,device=tensor.device)
new_tensor[tensor == 0] = -BOUND
return new_tensor
def forward_euler(f : torch.nn.Module, x_0 : Dict[str,torch.Tensor],
t_0 : float, T : float, delta_t : float, detach = False) -> Dict[str,torch.Tensor]:
'''
Implementation of Forward Euler solver.
Args:
f (torch.nn.Module) : differential operator
x_0 (Dict[str,torch.Tensor]) : Initial conditions
t_0 (float) : Time at which to begin updating
phonology and semantics
T (float) : Maxmimum time
delta_t (float) : timestep
Returns:
Dictionary containing the values of all states / hidden units
at each timestep.
'''
outputs,x = [x_0],x_0
for t in torch.arange(0,T,delta_t):
derivatives = f(x,detach=detach)
nx = {}
for key in x:
if t<t_0 and key in ['phonology','semantics']:
nx[key] = x_0[key]
continue;
else:
nx[key] = x[key] + delta_t * derivatives[key]
nx[key] = torch.clamp(nx[key],-BOUND,BOUND)
outputs.append(nx)
x = nx
return outputs
class Metrics:
def __init__(self,phoneme_embedding_matrix : torch.Tensor,
k : Optional[List[int]] = [1],
tau : Optional[List[float]] = [.5]):
'''
Utility class for phonological and semantic accuracies
Args:
phoneme_embedding_matrix (torch.Tensor) : feature vectors corresponding
to each phoneme
k (List[int]) : list of top-k phonological accuracies to compute
tau (List[float]) : list of thresholds for computing semantic accuracy
'''
self.phoneme_embedding_matrix = phoneme_embedding_matrix
self.k = k
self.tau = tau
def to(self, device : torch.device) -> 'Metrics':
'''
Copy [phoneme_embedding_matrix] to the provided device.
Args:
device (torch.device)
Returns:
self
'''
assert isinstance(device,torch.device)
self.phoneme_embedding_matrix = self.phoneme_embedding_matrix.to(device)
return self
def compute_phon_accuracy(self,preds : torch.Tensor, targets : torch.Tensor,
k : int) -> float:
'''
Compute phonological accuracy
Args:
preds (torch.Tensor) : predicted phonology vectors
targets (torch.Tensor) : ground-truth phonology vectors
k (int) : setting for top-k accuracy
Returns:
Accuracy
'''
preds = preds.view(preds.shape[0],-1,1,self.phoneme_embedding_matrix.shape[-1])
targets = targets.view(targets.shape[0],-1,1,self.phoneme_embedding_matrix.shape[-1])
pred_distances = (preds - self.phoneme_embedding_matrix[None,None]).norm(dim=-1)
target_distances = (targets - self.phoneme_embedding_matrix[None,None]).norm(dim=-1)
vals = (target_distances.argmin(dim=-1,keepdim=True) == pred_distances.argsort(dim=-1)[:,:,:k]).any(dim=-1)
return vals.all(dim=-1).float().mean().item()
def compute_sem_accuracy(self,preds : torch.Tensor, targets : torch.Tensor,
tau : float) -> float:
'''
Compute semantic accuracy
Args:
preds (torch.Tensor) : predicted semantic vectors
targets (torch.Tensor) : ground-truth semantic vectors
tau (float) : threshold for binarizing [preds]
Return:
Accuracy
'''
acc = ((preds>=tau) == targets.bool()).all(dim=-1).float()
return acc.mean().item()
def __call__(self,preds : Dict[str,torch.Tensor], targets : Dict[str,torch.Tensor]) -> Tuple[List[float]]:
'''
Compute phonological and semantic accuracies for all
values of [k]/[tau]
Args:
preds (Dict[str,torch.Tensor]) : predicted phonology and semantics
targets (Dict[str,torch.Tensor]) : ground-truth phonology and semantics
Returns:
Accuracies
'''
phonology_preds = preds['phonology']
phonology_targets = targets['phonology']
semantics_preds = preds['semantics']
semantics_targets = targets['semantics']
phon_accuracy = [[self.compute_phon_accuracy(phonology_pred,phonology_targets,k)
for k in self.k] for phonology_pred in phonology_preds]
sem_accuracy = [[self.compute_sem_accuracy(semantics_pred,semantics_targets,tau)
for tau in self.tau] for semantics_pred in semantics_preds]
return phon_accuracy,sem_accuracy
class TrainerConfig:
def __init__(self,**kwargs):
'''
Configuration class to store training options / hyperparameters.
Somewhat limited ATM.
Args:
**kwargs (dict)
'''
self.params = kwargs
@classmethod
def from_json(cls,json_path : str) -> 'TrainerConfig':
'''
Read config parameters from .json file
Args:
json_path (str) : path to config file
Return:
TrainerConfig
'''
config_params = json.load(open(json_path,'r'))
return cls(**config_params)
def create_trainer(self,phoneme_embedding_matrix : torch.Tensor) -> 'Trainer':
'''
Instantiate Trainer w/ desired parameters
Args:
phoneme_embedding_matrix (torch.Tensor) : feature vectors corresponding
to each phoneme
Returns:
Trainer
'''
### TODO: Add support for additional sovlers besides Forward Euler
if self.params.get('solver','forward_euler') == 'forward_euler':
solver = forward_euler
else:
raise ValueError('Supported solvers include: forward_euler')
### Set Zero Error Radius
zer = self.params.get('zer',.1)
### Set initial conditions
init_val = self.params.get('init_val',-15)
return Trainer(solver,phoneme_embedding_matrix,zer,init_val)
class Trainer:
def __init__(self,solver : Callable[[torch.nn.Module,Dict[str,torch.Tensor],float,float,float],
Dict[str,torch.Tensor]], phoneme_embedding_matrix : torch.Tensor,
zer : float, init_val : float):
'''
Utility class for running/updating the Triangle Model
under various lesioning configurations.
Args:
solver (Callable[[torch.nn.Module,Dict[str,torch.Tensor],float,float,float],
Dict[str,torch.Tensor]]) : ODE solver routine
phoneme_embedding_matrix (torch.Tensor) : feature vectors corresponding
to each phoneme
zer (float) : Zero Error Radiues for Cross-Entropy loss.
bound (float) : bound on the inverse sigmoid
init_val (float) : all unspecified states / hidden units will be initialized
to this value.
'''
self.solver = solver
self.metrics = Metrics(phoneme_embedding_matrix,[1,2,3],[.4,.5,.6])
self.zer = zer
self.init_val = init_val
self.device = torch.device('cpu')
def to(self,device : torch.device) -> 'Trainer':
'''
Move stored tensors to the provided device.
Args:
device (torch.device)
Returns:
self
'''
assert isinstance(device,torch.device)
self.device = device
self.metrics.to(device)
return self
def cross_entropy(self,preds : torch.Tensor, targets : torch.Tensor,
zer : float, eps : Optional[float] = 1e-4) -> torch.Tensor:
'''
Compute Cross-Entropy loss w/ ZER
Args:
preds (torch.Tensor) : predicted vectors
targets (torch.Tensor) : ground-truth vectors
zer (float) : Zero Error Radius
eps (Optional[float]) : Added for numerical stability
Returns:
Value of loss
'''
mask = ((targets-preds).abs()>=zer).float()
cross_entropy = -targets * (eps + preds).log()
cross_entropy = cross_entropy - (1-targets) * (1 + eps - preds).log()
return (mask * cross_entropy).sum(dim=(-1,-2))/(eps + mask.sum(dim=(-1,-2)))
def collate_outputs(self,outputs : Dict[str,torch.Tensor]) -> Dict[str,torch.Tensor]:
'''
Compute the "output" of the Triangle Model.
Args:
outputs (Dict[str,torch.Tensor]) : Values of all states / hidden units
at each timestep.
Returns:
Phonology and semantics (all timesteps) normalized via a logistic sigmoid.
'''
for idx,output in enumerate(outputs):
S = clipped_sigmoid(output['semantics'],BOUND)[None]
P = clipped_sigmoid(output['phonology'],BOUND)[None]
if idx == 0:
semantics = S
phonology = P
else:
semantics = torch.cat((semantics,S),dim=0)
phonology = torch.cat((phonology,P),dim=0)
return phonology,semantics
def create_inputs(self,model : TriangleModel, data : Dict[str,torch.Tensor]) -> Dict[str,torch.Tensor]:
'''
Define initial conditions
Args:
model (TriangleModel) : Only used to retrieve dimensionality of
states / hidden units
data (Dict[str,torch.Tensor]) : For all states / hidden units in [data],
we set the corresponding intial conditions
to the dictionary values.
Return:
Initial conditions for all states / hidden units
'''
### Determine batch size
temp = torch.zeros((0,))
batch_size = max([
data.get('orthography',temp).shape[0],
data.get('phonology',temp).shape[0],
data.get('semantics',temp).shape[0],
])
### Define orthography (constant)
if data.get('orthography',None) is not None:
inputs = {'orthography':invert_binary(data['orthography'])}
else:
inputs = {'orthography': self.init_val * torch.ones((batch_size,model.orth_dim),device=self.device)}
### Define initial phonology
if data.get('phonology',None) is not None:
inputs['phonology'] = invert_binary(data['phonology'])
else:
inputs['phonology'] = self.init_val * torch.ones((batch_size,model.phon_dim),device=self.device)
### Define initial semantics
if data.get('semantics',None) is not None:
inputs['semantics'] = invert_binary(data['semantics'])
else:
inputs['semantics'] = self.init_val * torch.ones((batch_size,model.sem_dim),device=self.device)
### Define initial conditions for cleanup units
inputs['cleanup_phon'] = self.init_val * torch.ones((batch_size,model.phon_cleanup_dim),device=self.device)
inputs['cleanup_sem'] = self.init_val * torch.ones((batch_size,model.sem_cleanup_dim),device=self.device)
### Define initial conditions for oral units
inputs['sem_2_phon'] = self.init_val * torch.ones((batch_size,model.sem_2_phon_dim),device=self.device)
inputs['phon_2_sem'] = self.init_val * torch.ones((batch_size,model.phon_2_sem_dim),device=self.device)
### Define initial conditions for reading units
inputs['orth_2_phon'] = self.init_val * torch.ones((batch_size,model.orth_2_phon_dim),device=self.device)
inputs['orth_2_sem'] = self.init_val * torch.ones((batch_size,model.orth_2_sem_dim),device=self.device)
return inputs
def step(self,model : TriangleModel, inputs : Dict[str,torch.Tensor],
targets : Optional[Dict[str,torch.Tensor]] = None,
opt : Optional[torch.optim.Optimizer] = None,
**kwargs) -> Union[
Tuple[torch.Tensor,torch.Tensor],
Tuple[List[float],List[float]],
Tuple[torch.Tensor,torch.Tensor,List[float],List[float]],
]:
'''
Perform a single forward pass through the Triangle Model w/
the specified training parameters.
Args:
model (TriangleModel)
inputs (Dict[str,torch.Tensor]) : specified initial conditions
targets (Optional[Dict[str,torch.Tensor]]) : ground-truth phonology and
semantics.
opt (Optional[torch.optim.Optimizer]) : optimizer for gradient descent
Returns:
If [targets] are not given, returns the output of [model]
If [targets] are given:
- If [opt] is given:
Return losses and accuracies
- If [opt] is not given:
Return accuracies
'''
### Get timestep at which to begin computing the error
start_error = kwargs.get('start_error',2)
### Get solver paramters
delta_t = kwargs.get('delta_t',1/3)
t_0 = kwargs.get('t_0',0)
T = kwargs.get('T',4)
detach = kwargs.get('detach',False)
### Define initial conditions
inputs = self.create_inputs(model,inputs)
### Call solver
outputs = self.solver(model,inputs,t_0,T,delta_t,detach)
### Extract outputs
predicted_phonology,predicted_semantics = self.collate_outputs(outputs)
### If no targets are provided, we just return the output
### of the model
if targets is None:
if kwargs.get('return_outputs',False):
return outputs
else:
return predicted_phonology,predicted_semantics
else:
phonology = targets['phonology']
semantics = targets['semantics']
### Compute accuracies for the final timestep
p_acc,s_acc = self.metrics(
{'phonology':predicted_phonology[start_error::],
'semantics':predicted_semantics[start_error::]},
targets)
if opt is None:
return None,None,p_acc,s_acc
### If an optimizer is provided, perform a training step.
else:
### Compute losses
phonology_loss = self.cross_entropy(predicted_phonology[start_error::],phonology[None],self.zer)
semantics_loss = self.cross_entropy(predicted_semantics[start_error::],semantics[None],self.zer)
### Set timestep weighting.
### TODO: allow user to change weighting; must be monotonic
weighting = torch.arange(1,phonology_loss.shape[0]+1,device=self.device)
weighting = torch.clamp(weighting/weighting[-1],.5,1)
summed_phonology_loss = (weighting * phonology_loss).sum()
summed_semantics_loss = (weighting * semantics_loss).sum()
### BPTT
loss = summed_phonology_loss + summed_semantics_loss
loss.backward()
### Update parameters
opt.step()
opt.zero_grad()
return phonology_loss.tolist(),semantics_loss.tolist(),p_acc,s_acc
def train_p2p(self,model : TriangleModel, opt : torch.optim.Optimizer,
data : Dict[str,torch.Tensor]) -> Tuple[float,List[float]]:
'''
Run + update the phonological cleanup units.
Args:
model (TriangleModel)
data (Dict[str,torch.Tensor]) : phonological and semantic representations
for a set of words
opt (torch.optim.Optimizer) : optimizer for gradient descent
'''
model.set_lesions(['o2s','o2p','p2s','s2p','s2s'])
start_error = -4
t_0 = 2 + 2/3
inputs = {
'phonology':data['phonology'].to(self.device),
'semantics':data['semantics'].to(self.device),
}
targets = {
'phonology':data['phonology'].to(self.device),
'semantics':data['semantics'].to(self.device),
}
phon_loss,sem_loss,phon_acc,sem_acc = self.step(model,inputs,opt=opt,targets=targets,
start_error = start_error, t_0 = t_0)
model.set_lesions()
return phon_loss,phon_acc
def train_s2s(self,model : TriangleModel, opt : torch.optim.Optimizer,
data : Dict[str,torch.Tensor]) -> Tuple[float,List[float]]:
'''
Run + update the semantic cleanup units
Args:
model (TriangleModel)
data (Dict[str,torch.Tensor]) : phonological and semantic representations
for a set of words
opt (torch.optim.Optimizer) : optimizer for gradient descent
'''
model.set_lesions(['o2s','o2p','p2s','s2p','p2p'])
start_error = -4
t_0 = 2 + 2/3
inputs = {
'phonology':data['phonology'].to(self.device),
'semantics':data['semantics'].to(self.device),
}
targets = {
'phonology':data['phonology'].to(self.device),
'semantics':data['semantics'].to(self.device),
}
phon_loss,sem_loss,phon_acc,sem_acc = self.step(model,inputs,opt=opt,targets=targets,
start_error = start_error, t_0 = t_0)
model.set_lesions()
return sem_loss,sem_acc
def train_s2p(self,model : TriangleModel, opt : torch.optim.Optimizer,
data : Dict[str,torch.Tensor]) -> Tuple[float,List[float]]:
'''
Run + update the S2P oral pathway and phonological cleanup units
Args:
model (TriangleModel)
data (Dict[str,torch.Tensor]) : phonological and semantic representations
for a set of words
opt (torch.optim.Optimizer) : optimizer for gradient descent
'''
model.set_lesions(['o2s','o2p','p2s','s2s'])
start_error = -3
t_0 = 0
inputs = {'semantics':data['semantics'].to(self.device)}
targets = {
'phonology':data['phonology'].to(self.device),
'semantics':data['semantics'].to(self.device),
}
loss,_,acc,_ = self.step(model,inputs,opt=opt,targets=targets,
start_error = start_error, t_0 = t_0)
model.set_lesions()
return loss,acc
def train_p2s(self,model : TriangleModel, opt : torch.optim.Optimizer,
data : Dict[str,torch.Tensor]) -> Tuple[float,List[float]]:
'''
Run + update the P2S oral pathway and semantic cleanup units
Args:
model (TriangleModel)
data (Dict[str,torch.Tensor]) : phonological and semantic representations
for a set of words
opt (torch.optim.Optimizer) : optimizer for gradient descent
'''
model.set_lesions(['o2s','o2p','s2p','p2p'])
start_error = -3
t_0 = 0
inputs = {'phonology':data['phonology'].to(self.device)}
targets = {
'phonology':data['phonology'].to(self.device),
'semantics':data['semantics'].to(self.device),
}
_,loss,_,acc = self.step(model,inputs,opt=opt,targets=targets,
start_error = start_error, t_0 = t_0)
model.set_lesions()
return loss,acc
def train_full(self,model : TriangleModel, opt : torch.optim.Optimizer,
data : Dict[str,torch.Tensor], lesions = [],
detach : Optional[bool] = False) -> Tuple[Tuple[float],Tuple[List[float]]]:
'''
Run + update the "full" (i.e: w/ orthography, no lesions
by default) Triangle Model.
Args:
model (TriangleModel)
data (Dict[str,torch.Tensor]) : phonological and semantic representations
for a set of words
opt (torch.optim.Optimizer) : optimizer for gradient descent
'''
model.set_lesions(lesions)
start_error = 1
t_0 = 0
inputs = {'orthography':data['orthography'].to(self.device)}
targets = {
'phonology':data['phonology'].to(self.device),
'semantics':data['semantics'].to(self.device),
}
phon_loss,sem_loss,phon_acc,sem_acc = self.step(model,inputs,opt=opt,targets=targets,
start_error = start_error,
t_0 = t_0,detach=detach)
model.set_lesions()
return (phon_loss,sem_loss),(phon_acc,sem_acc)