-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSModules.py
191 lines (152 loc) · 8.32 KB
/
CSModules.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
import torch
import argparse
import configparser
import Helpers
from torch.autograd import Variable
from Modules import ShorterBiaffine, LongerBiaffine
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true')
parser.add_argument('--cuda', action='store_true')
parser.add_argument('--code_switch', action='store_true')
parser.add_argument('--config', default='./config.ini')
parser.add_argument('--train', default='./data/sv-ud-train.conllu')
parser.add_argument('--dev', default='./data/sv-ud-dev.conllu')
parser.add_argument('--test', default='./data/sv-ud-test.conllu')
args = parser.parse_args()
config = configparser.ConfigParser()
config.read(args.config)
BATCH_SIZE = int(config['parser']['BATCH_SIZE'])
EMBED_DIM = int(config['parser']['EMBED_DIM'])
LSTM_DIM = int(config['parser']['LSTM_DIM'])
LSTM_LAYERS = int(config['parser']['LSTM_LAYERS'])
REDUCE_DIM_ARC = int(config['parser']['REDUCE_DIM_ARC'])
REDUCE_DIM_LABEL = int(config['parser']['REDUCE_DIM_LABEL'])
LEARNING_RATE = float(config['parser']['LEARNING_RATE'])
EPOCHS = int(config['parser']['EPOCHS'])
class CSParser(torch.nn.Module):
def __init__(self, sizes, args):
super().__init__()
self.use_cuda = args.cuda
self.debug = args.debug
# self.embeddings_chars = CharEmbedding(sizes, EMBED_DIM)
self.embeddings_forms = torch.nn.Embedding(sizes['vocab'], EMBED_DIM)
self.embeddings_tags = torch.nn.Embedding(sizes['postags'], EMBED_DIM)
self.embeddings_langs = torch.nn.Embedding(sizes['langs'], EMBED_DIM)
self.lstm = torch.nn.LSTM(2 * EMBED_DIM, LSTM_DIM, LSTM_LAYERS,
batch_first=True, bidirectional=True, dropout=0.33)
self.mlp_head = torch.nn.Linear(2 * LSTM_DIM, REDUCE_DIM_ARC)
self.mlp_dep = torch.nn.Linear(2 * LSTM_DIM, REDUCE_DIM_ARC)
self.mlp_deprel_head = torch.nn.Linear(2 * LSTM_DIM, REDUCE_DIM_LABEL)
self.mlp_deprel_dep = torch.nn.Linear(2 * LSTM_DIM, REDUCE_DIM_LABEL)
self.relu = torch.nn.ReLU()
self.dropout = torch.nn.Dropout(p=0.33)
# self.biaffine = Biaffine(REDUCE_DIM_ARC + 1, REDUCE_DIM_ARC, BATCH_SIZE)
self.biaffine = ShorterBiaffine(REDUCE_DIM_ARC)
self.label_biaffine = LongerBiaffine(REDUCE_DIM_LABEL, REDUCE_DIM_LABEL, sizes['deprels'])
self.criterion = torch.nn.CrossEntropyLoss(ignore_index=-1)
self.optimiser = torch.optim.Adam(self.parameters(), lr=LEARNING_RATE, betas=(0.9, 0.9))
# langid stuffs
self.langid_mlp = torch.nn.Linear(2 * LSTM_DIM, REDUCE_DIM_ARC)
self.langid_out = torch.nn.Linear(REDUCE_DIM_ARC, sizes['langs'])
if self.use_cuda:
self.biaffine.cuda()
self.label_biaffine.cuda()
def langid_fwd(self, forms, tags, pack):
form_embeds = self.dropout(self.embeddings_forms(forms))
tag_embeds = self.dropout(self.embeddings_tags(tags))
embeds = torch.cat([form_embeds, tag_embeds], dim=2)
embeds = torch.nn.utils.rnn.pack_padded_sequence(embeds, pack.tolist(), batch_first=True)
lstm_out, _ = self.lstm(embeds)
lstm_out, _ = torch.nn.utils.rnn.pad_packed_sequence(lstm_out, batch_first=True)
mlp_out = self.dropout(self.relu(self.langid_mlp(lstm_out)))
return self.langid_out(mlp_out)
def forward(self, forms, tags, pack):
# embed and dropout forms and tags; concat
# TODO: same mask embedding
# char_embeds = self.embeddings_chars(chars, pack)
form_embeds = self.dropout(self.embeddings_forms(forms))
tag_embeds = self.dropout(self.embeddings_tags(tags))
# lang_embeds = self.dropout(self.embeddings_tags(tags))
embeds = torch.cat([form_embeds, tag_embeds], dim=2)
# pack/unpack for LSTM
embeds = torch.nn.utils.rnn.pack_padded_sequence(embeds, pack.tolist(), batch_first=True)
output, _ = self.lstm(embeds)
output, _ = torch.nn.utils.rnn.pad_packed_sequence(output, batch_first=True)
# predict heads
reduced_head_head = self.dropout(self.relu(self.mlp_head(output)))
reduced_head_dep = self.dropout(self.relu(self.mlp_dep(output)))
y_pred_head = self.biaffine(reduced_head_head, reduced_head_dep)
if self.debug:
return y_pred_head, Variable(torch.rand(y_pred_head.size()))
# predict deprels using heads
reduced_deprel_head = self.dropout(self.relu(self.mlp_deprel_head(output)))
reduced_deprel_dep = self.dropout(self.relu(self.mlp_deprel_dep(output)))
predicted_labels = y_pred_head.max(2)[1]
selected_heads = torch.stack([torch.index_select(reduced_deprel_head[n], 0, predicted_labels[n])
for n, _ in enumerate(predicted_labels)])
y_pred_label = self.label_biaffine(selected_heads, reduced_deprel_dep)
y_pred_label = Helpers.extract_best_label_logits(predicted_labels, y_pred_label, pack)
if self.use_cuda:
y_pred_label = y_pred_label.cuda()
return y_pred_head, y_pred_label
def train_(self, epoch, train_loader):
self.train()
train_loader.init_epoch()
for i, batch in enumerate(train_loader):
(x_forms, pack), x_tags, y_heads, y_deprels, y_langs = \
batch.form, batch.upos, batch.head, batch.deprel, batch.misc
mask = torch.zeros(pack.size()[0], max(pack)).type(torch.LongTensor)
for n, size in enumerate(pack):
mask[n, 0:size] = 1
y_pred_head, y_pred_deprel = self(x_forms, x_tags, pack)
y_pred_langs = self.langid_fwd(x_forms, x_tags, pack)
# reshape for cross-entropy
batch_size, longest_sentence_in_batch = y_heads.size()
# predictions: (B x S x S) => (B * S x S)
# heads: (B x S) => (B * S)
y_pred_head = y_pred_head.view(batch_size * longest_sentence_in_batch, -1)
y_heads = y_heads.contiguous().view(batch_size * longest_sentence_in_batch)
# predictions: (B x S x D) => (B * S x D)
# heads: (B x S) => (B * S)
y_pred_deprel = y_pred_deprel.view(batch_size * longest_sentence_in_batch, -1)
y_deprels = y_deprels.contiguous().view(batch_size * longest_sentence_in_batch)
# langid
y_pred_langs = y_pred_langs.view(batch_size * longest_sentence_in_batch, -1)
y_langs = y_langs.contiguous().view(batch_size * longest_sentence_in_batch)
train_loss = self.criterion(y_pred_head, y_heads)
if not self.debug:
train_loss += self.criterion(y_pred_deprel, y_deprels)
train_loss -= self.criterion(y_pred_langs, y_langs)
self.zero_grad()
train_loss.backward()
self.optimiser.step()
print("Epoch: {}\t{}/{}\tloss: {}".format(epoch, (i + 1) * len(x_forms), len(train_loader.dataset), train_loss.data[0]))
def evaluate_(self, test_loader):
las_correct, uas_correct, total = 0, 0, 0
self.eval()
for i, batch in enumerate(test_loader):
(x_forms, pack), x_tags, y_heads, y_deprels = batch.form, batch.upos, batch.head, batch.deprel
mask = torch.zeros(pack.size()[0], max(pack)).type(torch.LongTensor)
for n, size in enumerate(pack):
mask[n, 0:size] = 1
# get labels
# TODO: ensure well-formed tree
y_pred_head, y_pred_deprel = [i.max(2)[1] for i in self(x_forms, x_tags, pack)]
mask = mask.type(torch.ByteTensor)
if self.use_cuda:
mask = mask.cuda()
mask = Variable(mask)
heads_correct = ((y_heads == y_pred_head) * mask)
deprels_correct = ((y_deprels == y_pred_deprel) * mask)
# excepts should never trigger; leave them in just in case
try:
uas_correct += heads_correct.nonzero().size(0)
except RuntimeError:
pass
try:
las_correct += (heads_correct * deprels_correct) .nonzero().size(0)
except RuntimeError:
pass
total += mask.nonzero().size(0)
print("UAS = {}/{} = {}\nLAS = {}/{} = {}".format(uas_correct, total, uas_correct / total,
las_correct, total, las_correct / total))