-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunker.py
266 lines (217 loc) · 11.6 KB
/
chunker.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
# Code adapted from original code by Robert Guthrie
import os, sys, optparse, gzip, re, logging
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import tqdm
import numpy as np
import string
def read_conll(handle, input_idx=0, label_idx=2):
conll_data = []
contents = re.sub(r'\n\s*\n', r'\n\n', handle.read())
contents = contents.rstrip()
for sent_string in contents.split('\n\n'):
annotations = list(zip(*[ word_string.split() for word_string in sent_string.split('\n') ]))
assert(input_idx < len(annotations))
if label_idx < 0:
conll_data.append( annotations[input_idx] )
logging.info("CoNLL: {}".format( " ".join(annotations[input_idx])))
else:
assert(label_idx < len(annotations))
conll_data.append(( annotations[input_idx], annotations[label_idx] ))
logging.info("CoNLL: {} ||| {}".format( " ".join(annotations[input_idx]), " ".join(annotations[label_idx])))
return conll_data
def prepare_sequence(seq, to_ix, unk):
idxs = []
if unk not in to_ix:
idxs = [to_ix[w] for w in seq]
else:
idxs = [to_ix[w] for w in map(lambda w: unk if w not in to_ix else w, seq)]
return torch.tensor(idxs, dtype=torch.long)
# ========== OUR FUNCTIONS HERE ==========
def character_level_representation(sentence):
first_characters = torch.zeros((len(sentence), len(string.printable)))
last_characters = torch.zeros((len(sentence), len(string.printable)))
other_characters = torch.zeros((len(sentence), len(string.printable)))
for i, word in enumerate(sentence):
if word == '[UNK]':
word = 'a'
# First and last characters
first_characters[i][string.printable.find(word[0])] = 1
last_characters[i][string.printable.find(word[-1])] = 1
# Non Edge Characters
for j in range(1, len(word)-1):
other_characters[i][string.printable.find(word[j])] += 1
# Normalize Internal Character Vector
tmax = torch.max(other_characters[i])
tmin = torch.min(other_characters[i])
other_characters[i] = (other_characters[i]-tmin) / (tmax - tmin)
return torch.cat([first_characters, other_characters, last_characters], dim=1)
def character_level_representation_v2(sentence):
first_characters = torch.zeros((len(sentence), len(string.printable)))
second_characters = torch.zeros((len(sentence), len(string.printable)))
last_characters = torch.zeros((len(sentence), len(string.printable)))
second_last_characters = torch.zeros((len(sentence), len(string.printable)))
other_characters = torch.zeros((len(sentence), len(string.printable)))
for i, word in enumerate(sentence):
if word == '[UNK]':
word = 'a'
# First and last characters
first_characters[i][string.printable.find(word[0])] = 1
last_characters[i][string.printable.find(word[-1])] = 1
if len(word) > 3:
second_characters[i][string.printable.find(word[1])] = 1
second_last_characters[i][string.printable.find(word[-2])] = 1
# Non Edge Characters
for j in range(2, len(word)-2):
other_characters[i][string.printable.find(word[j])] += 1
# Normalize Internal Character Vector
tmax = torch.max(other_characters[i])
tmin = torch.min(other_characters[i])
other_characters[i] = (other_characters[i]-tmin) / (tmax - tmin)
return torch.cat([first_characters, second_characters, other_characters, second_last_characters, last_characters], dim=1)
# ========== OUR FUNCTIONS END HERE ==========
class LSTMTaggerModel(nn.Module):
def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size):
torch.manual_seed(2)
super(LSTMTaggerModel, self).__init__()
self.hidden_dim = hidden_dim
self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)
# The LSTM takes word embeddings as inputs, and outputs hidden states
# with dimensionality hidden_dim.
self.lstm = nn.LSTM(embedding_dim + 300, hidden_dim, bidirectional=True)
# The linear layer that maps from hidden state space to tag space
self.hidden2tag = nn.Linear(hidden_dim*2, tagset_size)
def forward(self, sentence, encoding_tensor):
embeds = torch.cat([self.word_embeddings(sentence), encoding_tensor], dim=1)
lstm_out, _ = self.lstm(embeds.view(len(sentence), 1, -1))
tag_space = self.hidden2tag(lstm_out.view(len(sentence), -1))
tag_scores = F.log_softmax(tag_space, dim=1)
return tag_scores
class LSTMTagger:
def __init__(self, trainfile, modelfile, modelsuffix, unk="[UNK]", epochs=10, embedding_dim=128, hidden_dim=64):
self.unk = unk
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.epochs = epochs
self.modelfile = modelfile
self.modelsuffix = modelsuffix
self.training_data = []
if trainfile[-3:] == '.gz':
with gzip.open(trainfile, 'rt') as f:
self.training_data = read_conll(f)
else:
with open(trainfile, 'r') as f:
self.training_data = read_conll(f)
self.word_to_ix = {} # replaces words with an index (one-hot vector)
self.tag_to_ix = {} # replace output labels / tags with an index
self.ix_to_tag = [] # during inference we produce tag indices so we have to map it back to a tag
for sent, tags in self.training_data:
for word in sent:
if word not in self.word_to_ix:
self.word_to_ix[word] = len(self.word_to_ix)
for tag in tags:
if tag not in self.tag_to_ix:
self.tag_to_ix[tag] = len(self.tag_to_ix)
self.ix_to_tag.append(tag)
logging.info("word_to_ix:", self.word_to_ix)
logging.info("tag_to_ix:", self.tag_to_ix)
logging.info("ix_to_tag:", self.ix_to_tag)
self.model = LSTMTaggerModel(self.embedding_dim, self.hidden_dim, len(self.word_to_ix), len(self.tag_to_ix))
self.optimizer = optim.SGD(self.model.parameters(), lr=0.01)
def argmax(self, seq):
output = []
with torch.no_grad():
inputs = prepare_sequence(seq, self.word_to_ix, self.unk)
encoding_tensor = character_level_representation(seq)
tag_scores = self.model(inputs, encoding_tensor)
for i in range(len(inputs)):
output.append(self.ix_to_tag[int(tag_scores[i].argmax(dim=0))])
return output
def train(self):
loss_function = nn.NLLLoss()
self.model.train()
loss = float("inf")
for epoch in range(self.epochs):
for sentence, tags in tqdm.tqdm(self.training_data):
# Step 1. Remember that Pytorch accumulates gradients.
# We need to clear them out before each instance
self.model.zero_grad()
# Step 2. Get our inputs ready for the network, that is, turn them into
# Tensors of word indices.
sentence_in = prepare_sequence(sentence, self.word_to_ix, self.unk)
targets = prepare_sequence(tags, self.tag_to_ix, self.unk)
# Step 2a. Encode character level representation
encoding_tensor = character_level_representation(sentence)
# Step 3. Run our forward pass.
tag_scores = self.model(sentence_in, encoding_tensor)
# Step 4. Compute the loss, gradients, and update the parameters by
# calling optimizer.step()
loss = loss_function(tag_scores, targets)
loss.backward()
self.optimizer.step()
if epoch == self.epochs-1:
epoch_str = '' # last epoch so do not use epoch number in model filename
else:
epoch_str = str(epoch)
savefile = self.modelfile + epoch_str + self.modelsuffix
print("saving model file: {}".format(savefile), file=sys.stderr)
torch.save({
'epoch': epoch,
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': self.optimizer.state_dict(),
'loss': loss,
'unk': self.unk,
'word_to_ix': self.word_to_ix,
'tag_to_ix': self.tag_to_ix,
'ix_to_tag': self.ix_to_tag,
}, savefile)
def decode(self, inputfile):
if inputfile[-3:] == '.gz':
with gzip.open(inputfile, 'rt') as f:
input_data = read_conll(f, input_idx=0, label_idx=-1)
else:
with open(inputfile, 'r') as f:
input_data = read_conll(f, input_idx=0, label_idx=-1)
if not os.path.isfile(self.modelfile + self.modelsuffix):
raise IOError("Error: missing model file {}".format(self.modelfile + self.modelsuffix))
saved_model = torch.load(self.modelfile + self.modelsuffix)
self.model.load_state_dict(saved_model['model_state_dict'])
self.optimizer.load_state_dict(saved_model['optimizer_state_dict'])
epoch = saved_model['epoch']
loss = saved_model['loss']
self.unk = saved_model['unk']
self.word_to_ix = saved_model['word_to_ix']
self.tag_to_ix = saved_model['tag_to_ix']
self.ix_to_tag = saved_model['ix_to_tag']
self.model.eval()
decoder_output = []
for sent in tqdm.tqdm(input_data):
decoder_output.append(self.argmax(sent))
return decoder_output
if __name__ == '__main__':
optparser = optparse.OptionParser()
optparser.add_option("-i", "--inputfile", dest="inputfile", default=os.path.join('data', 'input', 'dev.txt'), help="produce chunking output for this input file")
optparser.add_option("-t", "--trainfile", dest="trainfile", default=os.path.join('data', 'train.txt.gz'), help="training data for chunker")
optparser.add_option("-m", "--modelfile", dest="modelfile", default=os.path.join('data', 'chunker'), help="filename without suffix for model files")
optparser.add_option("-s", "--modelsuffix", dest="modelsuffix", default='.tar', help="filename suffix for model files")
optparser.add_option("-e", "--epochs", dest="epochs", default=5, help="number of epochs [fix at 5]")
optparser.add_option("-u", "--unknowntoken", dest="unk", default='[UNK]', help="unknown word token")
optparser.add_option("-f", "--force", dest="force", action="store_true", default=False, help="force training phase (warning: can be slow)")
optparser.add_option("-l", "--logfile", dest="logfile", default=None, help="log file for debugging")
(opts, _) = optparser.parse_args()
if opts.logfile is not None:
logging.basicConfig(filename=opts.logfile, filemode='w', level=logging.DEBUG)
modelfile = opts.modelfile
if opts.modelfile[-4:] == '.tar':
modelfile = opts.modelfile[:-4]
chunker = LSTMTagger(opts.trainfile, modelfile, opts.modelsuffix, opts.unk)
# use the model file if available and opts.force is False
if os.path.isfile(opts.modelfile + opts.modelsuffix) and not opts.force:
decoder_output = chunker.decode(opts.inputfile)
else:
print("Warning: could not find modelfile {}. Starting training.".format(modelfile + opts.modelsuffix), file=sys.stderr)
chunker.train()
decoder_output = chunker.decode(opts.inputfile)
print("\n\n".join([ "\n".join(output) for output in decoder_output ]))