-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
279 lines (217 loc) · 8.09 KB
/
train.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
"""
Learn a model to morphologically generate surface forms from analyses
In this demo, a recurrent network equipped with an attention mechanism
learns to morphologically generate each word (on a symbol-by-symbol basis) in
its input text.
Mostly hacked from WordReverser
"""
import sys, math;
from theano import tensor
from blocks.bricks import Tanh, Initializable
from blocks.bricks.base import application
from blocks.bricks.lookup import LookupTable
from blocks.bricks.recurrent import SimpleRecurrent, Bidirectional
from blocks.bricks.attention import SequenceContentAttention
from blocks.bricks.parallel import Fork
from blocks.bricks.sequence_generators import (SequenceGenerator, Readout, SoftmaxEmitter, LookupFeedback)
from fuel.datasets import TextFile
from fuel.transformers import Mapping, Batch, Padding, Filter
from fuel.schemes import ConstantScheme
from blocks.initialization import Orthogonal, IsotropicGaussian, Constant
from blocks.utils import dict_union
from blocks.model import Model
from blocks.graph import ComputationGraph
from blocks.algorithms import (GradientDescent, Scale, StepClipping, CompositeRule)
from blocks.algorithms import RMSProp
from blocks.monitoring import aggregation
from blocks.extensions import FinishAfter, Printing, Timing
from blocks.extensions.saveload import Checkpoint
from blocks.extensions.monitoring import TrainingDataMonitoring
from blocks.main_loop import MainLoop
class Globals: #{
char2code = {};
code2char = {};
lookup = {};
def read_alphabet(path): #{
f = open(path);
for line in f.readlines(): #{
row = line.strip().split('\t');
code = int(row[0]);
sym = row[1];
Globals.char2code[sym] = code;
Globals.code2char[code] = sym;
#}
f.close();
#}
def read_lookup(path): #{
f = open(path);
for line in f.readlines(): #{
row = line.strip().replace('|||', '\t').split('\t');
out_string = '<S>';
out_symbols = []; out_symbols.append(Globals.char2code['<S>']);
for c in row[0]: #{
out_string = out_string + ' ' + c ;
out_symbols.append(Globals.char2code[c]);
#}
out_string = out_string + ' ' + '</S>';
out_symbols.append(Globals.char2code['</S>']);
in_string = '<S>';
in_symbols = []; in_symbols.append(Globals.char2code['<S>']);
for c in row[1]: #{
in_string = in_string + ' ' + c ;
in_symbols.append(Globals.char2code[c]);
#}
for tag in row[2].split('|'): #{
in_string = in_string + ' ' + tag ;
in_symbols.append(Globals.char2code[tag]);
#}
in_string = in_string + ' ' + '</S>';
in_symbols.append(Globals.char2code['</S>']);
# print(in_string,'→', in_symbols, file=sys.stderr);
# print(out_string,'→', out_symbols, file=sys.stderr);
Globals.lookup[tuple(in_symbols)] = out_symbols;
#}
#}
#}
class MorphGen(Initializable): #{
def __init__(self, dimen, vocab_size): #{
# No idea what this is doing, but otherwise "allocated" is not set
super(MorphGen, self).__init__(self)
# The encoder, would changing 'dim' here make the char embeddings bigger?
encoder = Bidirectional(SimpleRecurrent(dim=dimen, activation=Tanh()))
# What is this doing ?
fork = Fork([name for name in encoder.prototype.apply.sequences if name != 'mask'])
fork.input_dim = dimen
fork.output_dims = [encoder.prototype.get_dim(name) for name in fork.input_names]
lookup = LookupTable(vocab_size, dimen)
transition = SimpleRecurrent(activation=Tanh(),dim=dimen, name="transition")
attention = SequenceContentAttention(state_names=transition.apply.states,attended_dim=2*dimen, match_dim=dimen, name="attention")
readout = Readout(
readout_dim=vocab_size,
source_names=[transition.apply.states[0],
attention.take_glimpses.outputs[0]],
emitter=SoftmaxEmitter(name="emitter"),
feedback_brick=LookupFeedback(vocab_size, dimen),
name="readout");
generator = SequenceGenerator(readout=readout, transition=transition, attention=attention,name="generator")
self.lookup = lookup
self.fork = fork
self.encoder = encoder
self.generator = generator
self.children = [lookup, fork, encoder, generator]
#}
@application
def cost(self, chars, chars_mask, targets, targets_mask): #{
return self.generator.cost_matrix(targets, targets_mask,
attended=self.encoder.apply(**dict_union(
self.fork.apply(self.lookup.apply(chars), as_dict=True),mask=chars_mask)),
attended_mask=chars_mask);
#}
#}
# What does this do ?
def _transpose(data): #{
return tuple(array.T for array in data)
#
def _tokenise(s): #{
# print('@_tokenise()', s.strip(), file=sys.stderr);
row = s.strip().replace('|||', '\t').split('\t');
in_string = '';
for c in row[1]: #{
in_string = in_string + ' ' + c ;
#}
for tag in row[2].split('|'): #{
in_string = in_string + ' ' + tag ;
#}
return in_string;
#}
def morph_lookup(l): #{
# print('@_morph_lookup()', l[0], file=sys.stderr);
lkp = tuple(l[0]);
if lkp in Globals.lookup: #{
# print('@_morph_lookup()', Globals.lookup[lkp], file=sys.stderr);
return (Globals.lookup[lkp],);
else: #{
for x in Globals.lookup: #{
print(x,'→', Globals.lookup[x], file=sys.stderr);
#}
sys.exit(-1);
#}
#}
def _is_nan(log): #{
return math.isnan(log.current_row['total_gradient_norm'])
#}
f_vocab = '';
f_train = '';
f_model = '';
n_epochs = 5 ;
# Maximum for my platform: 16100
sys.setrecursionlimit(16000);
if len(sys.argv) < 5: #{
print('train.py <vocab> <training> <nepochs> <model>');
sys.exit(-1);
else: #{
f_vocab = sys.argv[1];
f_train = sys.argv[2];
n_epochs = int(sys.argv[3]);
f_model = sys.argv[4];
#}
#print("Train:",f_train,file=sys.stderr);
Globals.read_alphabet(f_vocab);
#print("Vocab:",Globals.char2code, file=sys.stderr);
Globals.read_lookup(f_train);
m = MorphGen(100, len(Globals.char2code));
dataset_options = dict(dictionary=Globals.char2code, level="word", preprocess=_tokenise);
dataset = TextFile([f_train], **dataset_options)
data_stream = dataset.get_example_stream()
# Read examples and look up the right surface form
data_stream = Mapping(data_stream, morph_lookup, add_sources=("targets",))
# Read in 10 samples at a time
data_stream = Batch(data_stream, iteration_scheme=ConstantScheme(10))
# Pad the examples
data_stream = Padding(data_stream)
data_stream = Mapping(data_stream, _transpose)
# Initialisation settings
m.weights_init = IsotropicGaussian(0.1)
m.biases_init = Constant(0.0)
m.push_initialization_config()
m.encoder.weights_init = Orthogonal()
m.generator.transition.weights_init = Orthogonal()
# Build the cost computation graph
chars = tensor.lmatrix("features")
chars_mask = tensor.matrix("features_mask")
targets = tensor.lmatrix("targets")
targets_mask = tensor.matrix("targets_mask")
batch_cost = m.cost(chars, chars_mask, targets, targets_mask).sum()
batch_size = chars.shape[1].copy(name="batch_size")
cost = aggregation.mean(batch_cost, batch_size)
cost.name = "sequence_log_likelihood"
print("Cost graph is built", file=sys.stderr)
model = Model(cost)
parameters = model.get_parameter_dict()
for brick in model.get_top_bricks(): #{
brick.initialize();
#}
cg = ComputationGraph(cost);
algo = GradientDescent(cost=cost, parameters=cg.parameters,step_rule=CompositeRule([StepClipping(10.0), Scale(0.01)]))
#algo = RMSProp(learning_rate=1.0, decay_rate=0.9)
max_length = chars.shape[0].copy(name="max_length")
observables = [batch_size, max_length,algo.total_step_norm, algo.total_gradient_norm, cost]
# Construct the main loop and start training!
average_monitoring = TrainingDataMonitoring(observables, prefix="average", every_n_batches=10)
checkpoint_after=n_epochs/5;
#checkpoint_after=100;
main_loop = MainLoop(
model=model, data_stream=data_stream, algorithm=algo,
extensions=[
Timing(),
TrainingDataMonitoring(observables, after_batch=True),
average_monitoring,
# FinishAfter(after_n_batches=n_batches)
FinishAfter(after_n_epochs=n_epochs)
# This shows a way to handle NaN emerging during training: simply finish it.
.add_condition(["after_batch"], _is_nan),
# Saving the model and the log separately is convenient,
# because loading the whole pickle takes quite some time.
Checkpoint(f_model, every_n_epochs=1,save_separately=["model", "log"]),
Printing(every_n_batches=100)]);
main_loop.run()