This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.py
158 lines (116 loc) · 4.91 KB
/
callbacks.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
# Copyright (c) 2021 Kemal Kurniawan
from typing import Callable, Dict, Optional, Sequence
from rnnr.callbacks import save
from sacred.run import Run
from text2array import Vocab
import torch
from crf import DepTreeCRF
from evaluation import count_correct
def set_train_mode(model: torch.nn.Module, training: bool = True) -> Callable[[dict], None]:
def callback(state):
model.train(training)
return callback
def batch2tensors(device: str, vocab: Vocab) -> Callable[[dict], None]:
def callback(state):
batch = state["batch"].to_array()
for k in batch:
batch[k] = torch.from_numpy(batch[k]).long().to(device)
batch["proj?"] = batch["proj?"].bool()
batch["punct?"] = batch["punct?"].bool()
batch["mask"] = batch["words"] != vocab["words"].index(Vocab.PAD_TOKEN)
state["batch"] = batch
return callback
def update_params(opt: torch.optim.Optimizer) -> Callable[[dict], None]:
def callback(state):
opt.zero_grad()
state["loss"].backward()
opt.step()
return callback
def log_grads(run: Run, model: torch.nn.Module, every: int = 10) -> Callable[[dict], None]:
def callback(state):
if state["n_iters"] % every != 0:
return
for name, p in model.named_parameters():
if p.requires_grad:
run.log_scalar(f"grad_{name}", p.grad.norm().item(), state["n_iters"])
return callback
def log_stats(run: Run, every: int = 10) -> Callable[[dict], None]:
def callback(state):
if state["n_iters"] % every != 0:
return
for name, value in state["stats"].items():
run.log_scalar(f"batch_{name}", value, state["n_iters"])
for name, value in state.get("extra_stats", {}).items():
run.log_scalar(f"batch_{name}", value, state["n_iters"])
return callback
def save_state_dict(*args, **kwargs) -> Callable[[dict], None]:
kwargs.update({"using": lambda m, p: torch.save(m.state_dict(), p), "ext": "pth"})
return save(*args, **kwargs)
def compute_total_arc_type_scores(
model: torch.nn.Module, vocab: Vocab
) -> Callable[[dict], None]:
def callback(state):
bat = state["batch"]
words, tags, mask = bat["words"], bat["tags"], bat["mask"]
arc_scores, type_scores = model(words, tags, mask)
type_scores[..., vocab["types"].index(vocab.PAD_TOKEN)] = -1e9 # mask padding type
_, HEAD, DEPD, TYPE = range(4)
arc_scores = arc_scores.masked_fill(~mask.unsqueeze(DEPD), -1e9) # mask padding heads
arc_scores = arc_scores.log_softmax(dim=HEAD)
type_scores = type_scores.log_softmax(dim=TYPE)
state["total_arc_type_scores"] = arc_scores.unsqueeze(TYPE) + type_scores
return callback
def predict_batch(
projective=False, multiroot=False, scores="total_arc_type_scores"
) -> Callable[[dict], None]:
def callback(state):
assert state["batch"]["mask"].all()
crf = DepTreeCRF(state[scores], projective=projective, multiroot=multiroot)
pred_heads, pred_types = crf.argmax()
state["pred_heads"] = pred_heads
state["pred_types"] = pred_types
return callback
def get_n_items() -> Callable[[dict], None]:
def callback(state):
state["n_items"] = state["batch"]["mask"].long().sum().item()
return callback
def evaluate_batch(type_vocab: Optional[Sequence[str]] = None) -> Callable[[dict], None]:
def callback(state):
bat = state["batch"]
words, tags, heads, types = bat["words"], bat["tags"], bat["heads"], bat["types"]
pred_heads, pred_types = state["pred_heads"], state["pred_types"]
mask, proj_mask, punct_mask = bat["mask"], bat["proj?"], bat["punct?"]
# remove root
words, tags, heads, types = words[:, 1:], tags[:, 1:], heads[:, 1:], types[:, 1:]
pred_heads, pred_types = pred_heads[:, 1:], pred_types[:, 1:]
mask, proj_mask, punct_mask = mask[:, 1:], proj_mask[:, 1:], punct_mask[:, 1:]
counts = count_correct(
heads, types, pred_heads, pred_types, mask, ~punct_mask, proj_mask
)
state["output"] = counts
if type_vocab is not None:
state["tw_output"] = {
y: count_correct(
heads,
types,
pred_heads,
pred_types,
mask,
~punct_mask,
proj_mask,
type_idx=type_vocab.index(y),
)
for y in type_vocab
}
return callback
def compute_l2_loss(
model: torch.nn.Module, origin: Optional[Dict[str, torch.Tensor]] = None
) -> Callable[[dict], None]:
if origin is None:
origin = {}
def callback(state):
loss = 0
for name, p in model.named_parameters():
loss += (p - origin.get(name, 0)).pow(2).sum()
state["l2_loss"] = loss
return callback