This repository has been archived by the owner on Jun 12, 2020. It is now read-only.
forked from YerevaNN/translit-rnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
1121 lines (961 loc) · 46.5 KB
/
utils.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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*
from __future__ import print_function
import numpy as np
import theano
import theano.tensor as T
import lasagne
import codecs
import json
import random
from lasagne.init import Orthogonal, Normal
#Lasagne Seed for Reproducibility
random.seed(1)
lasagne.random.set_rng(np.random.RandomState(1))
def isNativeLetter(s, transliteration):
### Checks if a character is from a native languge
for c in s:
if c not in transliteration:
return False
return True
def valid(transliteration, sequence):
### Replaces all non given characters with '#'
valids = [u'\u2000', u'\u2001',';',':','-',',',' ','\n','\t'] + \
[chr(ord('0') + i) for i in range(10)] + \
list(set(''.join([''.join([s for s in transliteration[c]]) for c in transliteration])))
ans = []
non_valids = []
for c in sequence:
if c in valids:
ans.append(c)
else:
ans.append('#')
non_valids.append(c)
return (ans,non_valids)
def toTranslit(prevc,c,nextc,trans):
### Translates one character to translit, given probabilistic mapping.
### Previous and next characters are given for some armenian-specific parts
if not isNativeLetter(c, trans):
return c
### Armenian Specific Snippet
if(c == u'ո'):
if(isNativeLetter(prevc, trans)):
return u'o'
return u'vo'
if(c == u'Ո'):
if(isNativeLetter(prevc, trans)):
return u'O'
return u'Vo'
###
x = random.random()
s = 0
eps = 1e-6
for i in trans[c]:
s += trans[c][i]
if( s > x - eps):
return i
print (c,s,"error")
def make_vocabulary_files(data, language, transliteration):
### Makes jsons for future mapping of letters to indices and vice versa
pointer = 0
done = False
s_l = 100000
chars = set()
trans_chars = set()
data = ' \t' + u'\u2001' + data # to get these symbols in vocab
while not done:
new_p = min(pointer + s_l ,len(data))
raw_native = data[pointer : new_p]
if new_p != len(data):
pointer = new_p
raw_native = ' ' + raw_native + ' '
else:
raw_native = ' ' + raw_native + ' '
done = True
native = []
translit = []
for ind in range(1,len(raw_native)-1):
trans_char = toTranslit(raw_native[ind-1], raw_native[ind], raw_native[ind+1], transliteration)
try:
translit.append(trans_char[0])
except IndexError:
continue
native.append(raw_native[ind])
if len(trans_char) > 1:
native.append(u'\u2000')
translit.append(trans_char[1])
translit = valid(transliteration, translit)[0]
for i in range(len(native)):
if translit[i] == '#':
native[i] = '#'
chars = chars.union(set(native))
trans_chars = trans_chars.union(set(translit))
print(str(100.0*pointer/len(data)) + "% done ", end='\r')
chars = list(chars)
char_to_index = { chars[i] : i for i in range(len(chars)) }
index_to_char = { i : chars[i] for i in range(len(chars)) }
open('languages/' + language + '/char_to_index.json','w').write(json.dumps(char_to_index))
open('languages/' + language + '/index_to_char.json','w').write(json.dumps(index_to_char))
trans_chars = list(trans_chars)
trans_to_index = { trans_chars[i] : i for i in range(len(trans_chars)) }
index_to_trans = { i : trans_chars[i] for i in range(len(trans_chars)) }
trans_vocab_size = len(trans_chars)
open('languages/' + language + '/trans_to_index.json','w').write(json.dumps(trans_to_index))
open('languages/' + language + '/index_to_trans.json','w').write(json.dumps(index_to_trans))
def load_vocabulary(language):
### Loads vocabulary mappings from specified json files
char_to_index = json.loads(open('languages/' + language + '/char_to_index.json').read())
char_to_index = { i : int(char_to_index[i]) for i in char_to_index}
index_to_char = json.loads(open('languages/' + language + '/index_to_char.json').read())
index_to_char = { int(i) : index_to_char[i] for i in index_to_char}
vocab_size = len(char_to_index)
trans_to_index = json.loads(open('languages/' + language + '/trans_to_index.json').read())
trans_to_index = { i : int(trans_to_index[i]) for i in trans_to_index}
index_to_trans = json.loads(open('languages/' + language + '/index_to_trans.json').read())
index_to_trans = { int(i) : index_to_trans[i] for i in index_to_trans}
trans_vocab_size = len(trans_to_index)
return (char_to_index, index_to_char, vocab_size, trans_to_index, index_to_trans, trans_vocab_size)
def one_hot_matrix_to_sentence(data, index_to_character):
### Converts one sequence of one hot vectors to a string sentence
if data.shape[0] == 1:
data = data[0]
sentence = ""
for i in data:
sentence += index_to_character[np.argmax(i)]
return sentence
def load_language_data(language, is_train = True):
TEST_DATA_PATH = 'languages/' + language + '/data/test.txt'
VALIDATION_DATA_PATH = 'languages/' + language + '/data/val.txt'
TRAIN_DATA_PATH = 'languages/' + language + '/data/train.txt'
long_letters = json.loads(codecs.open('languages/' + language + '/long_letters.json','r',encoding='utf-8').read())
long_letter_mapping = { long_letters[i] : chr(ord(u'\u2002') + i) for i in range(len(long_letters)) }
trans = json.loads(codecs.open('languages/' + language + '/transliteration.json','r',encoding='utf-8').read())
tmp_trans = trans.copy()
for c in tmp_trans:
if c in long_letters:
trans[long_letter_mapping[c]] = trans[c]
del tmp_trans
if is_train:
train_text = codecs.open(TRAIN_DATA_PATH, encoding='utf-8').read()
val_text = codecs.open(VALIDATION_DATA_PATH, encoding='utf-8').read()
for letter in long_letter_mapping:
train_text = train_text.replace(letter,long_letter_mapping[letter])
val_text = val_text.replace(letter,long_letter_mapping[letter])
return (train_text, val_text, trans)
else:
test_text = codecs.open(TEST_DATA_PATH, encoding='utf-8').read()
for letter in long_letter_mapping:
test_text = test_text.replace(letter,long_letter_mapping[letter])
long_letter_reverse_mapping = { long_letter_mapping[i] : i for i in long_letter_mapping }
return (test_text, trans, long_letter_reverse_mapping)
def gen_data(p, seq_len, batch_size, data, transliteration, trans_to_index, char_to_index, is_train = True):
### Generates training examples from data, starting from given index
### and returns the index where it stopped
### also returns the number of sequences skipped (because of lack of native characters)
### and a boolean showing whether generation passed one iteration over data or not
trans_vocab_size = len(trans_to_index)
vocab_size = len(char_to_index)
samples = []
batch_seq_len = 0
non_native_sequences = 0
turned = False
for i in range(batch_size):
while True:
new_p = min(p+seq_len,len(data))
raw_native = data[p:new_p]
if new_p != len(data):
if max([raw_native.rfind(u' '),raw_native.rfind(u'\t'),raw_native.rfind(u'\n')]) > 0:
new_p = max([raw_native.rfind(u' '),raw_native.rfind(u'\t'),raw_native.rfind(u'\n')])
raw_native = ' ' + raw_native[:new_p+1] + ' '
p += new_p + 1
else:
p = new_p + 1
raw_native = ' ' + raw_native + ' '
else:
raw_native = ' ' + raw_native + ' '
p = 0
turned = True
native_letter_count = sum([1 for c in raw_native if isNativeLetter(c, transliteration)])
if not is_train or native_letter_count * 3 > len(raw_native):
break
else:
non_native_sequences += 1
native = []
translit = []
for ind in range(1,len(raw_native)-1):
trans_char = toTranslit(raw_native[ind-1], raw_native[ind], raw_native[ind+1], transliteration)
translit.append(trans_char[0])
trans_ind = 1
native.append(raw_native[ind])
while len(trans_char) > trans_ind:
native.append(u'\u2000')
translit.append(trans_char[trans_ind])
trans_ind += 1
(translit,non_valids) = valid(transliteration, translit)
for ind in range(len(native)):
if translit[ind] == '#':
native[ind] = '#'
x = np.zeros((len(native), trans_vocab_size))
y = np.zeros((len(native), vocab_size))
for ind in range(len(native)):
x[ind,trans_to_index[translit[ind]]] = 1
y[ind,char_to_index[native[ind]]] = 1
batch_seq_len = max(batch_seq_len, len(native))
samples.append((x,y))
x = np.zeros((batch_size, batch_seq_len, trans_vocab_size))
y = np.zeros((batch_size, batch_seq_len, vocab_size))
for i in range(batch_size):
x[i, : len(samples[i][0]), :] = samples[i][0]
y[i, : len(samples[i][1]), :] = samples[i][1]
for j in range(len(samples[i][0]), batch_seq_len):
x[i, j, trans_to_index[u'\u2001']] = 1
y[i, j, char_to_index[u'\u2001']] = 1
if is_train:
return (x,y,p,turned,non_native_sequences)
else:
return (x,y,non_valids,p,turned)
def define_model(N_HIDDEN, depth, LEARNING_RATE = 0.01, GRAD_CLIP = 100, trans_vocab_size=0, vocab_size=0, is_train = False):
### Defines lasagne model
### Returns output layer and theano functions for training and computing the cost
l_input = lasagne.layers.InputLayer(shape=(None, None, trans_vocab_size))
network = l_input
symbolic_batch_size = lasagne.layers.get_output(network).shape[0]
while depth > 0 :
l_forward = lasagne.layers.LSTMLayer(
network, N_HIDDEN, grad_clipping=GRAD_CLIP,
ingate=lasagne.layers.Gate(
W_in=Orthogonal(gain=1.5),
W_hid=Orthogonal(gain=1.5),
W_cell=Normal(0.1)),
forgetgate=lasagne.layers.Gate(
W_in=Orthogonal(gain=1.5),
W_hid=Orthogonal(gain=1.5),
W_cell=Normal(0.1)),
cell=lasagne.layers.Gate(W_cell=None,
nonlinearity=lasagne.nonlinearities.tanh,
W_in=Orthogonal(gain=1.5),
W_hid=Orthogonal(gain=1.5)),
outgate=lasagne.layers.Gate(
W_in=Orthogonal(gain=1.5),
W_hid=Orthogonal(gain=1.5),
W_cell=Normal(0.1)),
backwards=False
)
l_backward = lasagne.layers.LSTMLayer(
network, N_HIDDEN, grad_clipping=GRAD_CLIP,
ingate=lasagne.layers.Gate(
W_in=Orthogonal(gain=1.5),
W_hid=Orthogonal(gain=1.5),
W_cell=Normal(0.1)),
forgetgate=lasagne.layers.Gate(
W_in=Orthogonal(gain=1.5),
W_hid=Orthogonal(gain=1.5),
W_cell=Normal(0.1)),
cell=lasagne.layers.Gate(W_cell=None,
nonlinearity=lasagne.nonlinearities.tanh,
W_in=Orthogonal(gain=1.5),
W_hid=Orthogonal(gain=1.5)),
outgate=lasagne.layers.Gate(
W_in=Orthogonal(gain=1.5),
W_hid=Orthogonal(gain=1.5),
W_cell=Normal(0.1)),
backwards=True
)
if depth == 1:
l_cell_forward = LSTMLayer(
network, N_HIDDEN, grad_clipping=GRAD_CLIP,
ingate=lasagne.layers.Gate(
W_in=l_forward.W_in_to_ingate,
W_hid=l_forward.W_hid_to_ingate,
# W_cell=l_forward.W_cell_to_ingate,
b=l_forward.b_ingate),
forgetgate=lasagne.layers.Gate(
W_in=l_forward.W_in_to_forgetgate,
W_hid=l_forward.W_hid_to_forgetgate,
# W_cell=l_forward.W_cell_to_forgetgate,
b=l_forward.b_forgetgate),
cell=lasagne.layers.Gate(W_cell=None,
nonlinearity=lasagne.nonlinearities.tanh,
W_in=l_forward.W_in_to_cell,
W_hid=l_forward.W_hid_to_cell,
b=l_forward.b_cell),
outgate=lasagne.layers.Gate(
W_in=l_forward.W_in_to_outgate,
W_hid=l_forward.W_hid_to_outgate,
# W_cell=l_forward.W_cell_to_outgate,
b=l_forward.b_outgate),
backwards=False,
peepholes=False)
l_cell_backwards = LSTMLayer(
network, N_HIDDEN, grad_clipping=GRAD_CLIP,
ingate=lasagne.layers.Gate(
W_in=l_backward.W_in_to_ingate,
W_hid=l_backward.W_hid_to_ingate,
# W_cell=l_backward.W_cell_to_ingate,
b=l_backward.b_ingate),
forgetgate=lasagne.layers.Gate(
W_in=l_backward.W_in_to_forgetgate,
W_hid=l_backward.W_hid_to_forgetgate,
# W_cell=l_backward.W_cell_to_forgetgate,
b=l_backward.b_forgetgate),
cell=lasagne.layers.Gate(W_cell=None,
nonlinearity=lasagne.nonlinearities.tanh,
W_in=l_backward.W_in_to_cell,
W_hid=l_backward.W_hid_to_cell,
b=l_backward.b_cell),
outgate=lasagne.layers.Gate(
W_in=l_backward.W_in_to_outgate,
W_hid=l_backward.W_hid_to_outgate,
# W_cell=l_backward.W_cell_to_outgate,
b=l_backward.b_outgate),
backwards=True,
peepholes=False)
concat_layer = lasagne.layers.ConcatLayer(incomings=[l_forward, l_backward], axis = 2)
concat_layer = lasagne.layers.ReshapeLayer(concat_layer, (-1, 2*N_HIDDEN))
network = lasagne.layers.DenseLayer(concat_layer, num_units=N_HIDDEN, W = Orthogonal(), nonlinearity=lasagne.nonlinearities.tanh)
network = lasagne.layers.ReshapeLayer(network, (symbolic_batch_size, -1, N_HIDDEN))
depth -= 1
network = lasagne.layers.ReshapeLayer(network, (-1, N_HIDDEN) )
l_input_reshape = lasagne.layers.ReshapeLayer(l_input, (-1, trans_vocab_size))
network = lasagne.layers.ConcatLayer(incomings=[network,l_input_reshape], axis = 1)
l_out = lasagne.layers.DenseLayer(network, num_units=vocab_size, W = lasagne.init.Normal(), nonlinearity=lasagne.nonlinearities.softmax)
target_values = T.dmatrix('target_output')
network_output = lasagne.layers.get_output(l_out)
network = lasagne.layers.get_output(network)
concat_layer = lasagne.layers.get_output(concat_layer)
last_lstm_cells_forward = lasagne.layers.get_output(l_cell_forward)
last_lstm_cells_backwards = lasagne.layers.get_output(l_cell_backwards)
#gates = l_cell_forward.get_gates()
cost = T.nnet.categorical_crossentropy(network_output,target_values).mean()
all_params = lasagne.layers.get_all_params(l_out,trainable=True)
print("Compiling Functions ...")
if is_train:
print("Computing Updates ...")
#updates = lasagne.updates.adagrad(cost, all_params, LEARNING_RATE)
updates = lasagne.updates.adam(cost, all_params, beta1=0.5, learning_rate=LEARNING_RATE) # from DCGAN paper
compute_cost = theano.function([l_input.input_var, target_values], cost, allow_input_downcast=True)
train = theano.function([l_input.input_var, target_values], cost, updates=updates, allow_input_downcast=True)
return(l_out, train, compute_cost)
else:
guess = theano.function([l_input.input_var],
[network_output, network, concat_layer,
last_lstm_cells_forward, last_lstm_cells_backwards
#gates[0], gates[1], gates[2]
],
allow_input_downcast=True)
return(l_out, guess)
def isDelimiter(c):
return c in [u'\n', u'\t', u' ']
def chunk_parse(chunk, seq_len, batch_size, transliteration, trans_to_index, char_to_index, is_train = False):
trans_vocab_size = len(trans_to_index)
vocab_size = len(char_to_index)
delimiters = [u'']
words = []
word = ''
i = 0
while i < len(chunk):
if isDelimiter(chunk[i]):
words.append(word)
word = ''
delimiter = chunk[i]
while i+1 < len(chunk) and isDelimiter(chunk[i+1]):
i += 1
delimiter += chunk[i]
delimiters.append(delimiter)
else:
word += chunk[i]
i += 1
if word != '':
words.append(word)
sequences = []
s = ""
sequence_delimiters = [u'']
for (word,delimiter) in zip(words,delimiters):
if len(s) + len(word) <= seq_len:
s += delimiter + word
elif len(s) != 0:
sequences.append(s);
s = word
sequence_delimiters.append(delimiter)
if s != '':
sequences.append(s)
samples = []
for seq in sequences:
native_letter_count = sum([1 for c in seq if isNativeLetter(c, transliteration)])
if is_train and native_letter_count * 3 < len(seq):
continue
seq = u' ' + seq + u' '
translit = []
native = []
for ind in range(1,len(seq)-1):
trans_char = toTranslit(seq[ind-1], seq[ind], seq[ind+1], transliteration)
translit.append(trans_char[0])
trans_ind = 1
native.append(seq[ind])
while len(trans_char) > trans_ind:
native.append(u'\u2000')
translit.append(trans_char[trans_ind])
trans_ind += 1
translit, non_valids = valid(transliteration, translit)
for ind in range(len(native)):
if translit[ind] == '#':
native[ind] = '#'
samples.append( (translit, native, non_valids))
'''
translits.append(translit)
natives.append(native)
non_valids_list.append(non_valids)
'''
if is_train:
samples.sort(key = lambda x: len(x[0]), reverse = True)
buckets = {}
for tmp in samples:
if len(tmp[0]) not in buckets.keys():
buckets[len(tmp[0])] = []
buckets[len(tmp[0])].append(tmp)
del samples
for i in buckets:
random.shuffle(buckets[i])
batches = []
for i in buckets.keys():
j = 0
while j < len(buckets[i]):
batches.append(list(buckets[i][j:j+batch_size]))
j += batch_size
del buckets
np_batches = []
for batch in batches:
x = np.zeros( (len(batch), len(batch[0][0]), trans_vocab_size) )
y = np.zeros( (len(batch), len(batch[0][0]), vocab_size) )
for i in range(len(batch)):
for j in range(len(batch[i][0])):
x[i, j, trans_to_index[batch[i][0][j]] ] = 1
y[i, j, char_to_index[batch[i][1][j]] ] = 1
np_batches.append((x,y))
return np_batches
else:
indexed_samples = sorted(zip(samples, range(len(samples)), sequence_delimiters) , key = lambda x: (len(x[0][0]), x[1]) , reverse = True)
#samples, indices = zip(indexed_samples)
#del indexed_samples
buckets = {}
for tmp in indexed_samples:
if len(tmp[0][0]) not in buckets.keys():
buckets[len(tmp[0][0])] = []
buckets[len(tmp[0][0])].append(tmp)
del indexed_samples
batches = []
for i in buckets.keys():
j = 0
while j < len(buckets[i]):
batches.append(list(buckets[i][j:j+batch_size]))
j += batch_size
del buckets
np_batches = []
non_vals = []
for batch in batches:
indices = np.zeros( len(batch) )
delimiters = [0] * len(batch)
x = np.zeros( (len(batch), len(batch[0][0][0]), trans_vocab_size) )
y = np.zeros( (len(batch), len(batch[0][0][0]), vocab_size) )
non_vals.append([])
for i in range(len(batch)):
indices[i] = batch[i][1]
delimiters[i] = batch[i][2]
non_vals[-1].append(batch[i][0][2])
for j in range(len(batch[i][0][0])):
x[i, j, trans_to_index[batch[i][0][0][j]] ] = 1
y[i, j, char_to_index[batch[i][0][1][j]] ] = 1
np_batches.append( (x, y, indices, delimiters) )
return (np_batches, non_vals)
def data_generator(data, seq_len, batch_size, transliteration, trans_to_index, char_to_index, is_train = False):
p = 0
while p < len(data):
if is_train:
parsed_data = chunk_parse(data[p:p+700000], seq_len, batch_size, transliteration, trans_to_index, char_to_index, is_train)
p += 700000
random.shuffle(parsed_data)
for batch in parsed_data:
yield batch
else:
parsed_data, non_valids = chunk_parse(data[p:p+700000], seq_len, batch_size, transliteration, trans_to_index, char_to_index, is_train)
p += 700000
for batch in zip(parsed_data, non_valids):
yield batch
"""
def temp_data_generator(data, seq_len, batch_size, transliteration, trans_to_index, char_to_index, is_train = False):
p = 0
size = 7000000 / 35
while p < len(data):
if is_train:
p += 7000000
parsed_data = []
for i in range(35):
start_index = np.random().randint(7000000 - size + 1)
parsed_data.extend(chunk_parse(data[start_index:start_index + size], seq_len, batch_size, transliteration, trans_to_index, char_to_index, is_train))
random.shuffle(parsed_data)
for batch in parsed_data:
yield batch
else:
parsed_data, non_valids = chunk_parse(data[p:p+7000000], seq_len, batch_size, transliteration, trans_to_index, char_to_index, is_train)
p += 7000000
for batch in zip(parsed_data, non_valids):
yield batch
"""
from lasagne import nonlinearities
from lasagne import init
from lasagne.utils import unroll_scan
from lasagne.layers.base import MergeLayer, Layer
from lasagne.layers.input import InputLayer
from lasagne.layers.dense import DenseLayer
from lasagne.layers import helper
__all__ = [
"CustomRecurrentLayer",
"RecurrentLayer",
"Gate",
"LSTMLayer",
"GRULayer"
]
class Gate(object):
"""
lasagne.layers.recurrent.Gate(W_in=lasagne.init.Normal(0.1),
W_hid=lasagne.init.Normal(0.1), W_cell=lasagne.init.Normal(0.1),
b=lasagne.init.Constant(0.), nonlinearity=lasagne.nonlinearities.sigmoid)
Simple class to hold the parameters for a gate connection. We define
a gate loosely as something which computes the linear mix of two inputs,
optionally computes an element-wise product with a third, adds a bias, and
applies a nonlinearity.
Parameters
----------
W_in : Theano shared variable, numpy array or callable
Initializer for input-to-gate weight matrix.
W_hid : Theano shared variable, numpy array or callable
Initializer for hidden-to-gate weight matrix.
W_cell : Theano shared variable, numpy array, callable, or None
Initializer for cell-to-gate weight vector. If None, no cell-to-gate
weight vector will be stored.
b : Theano shared variable, numpy array or callable
Initializer for input gate bias vector.
nonlinearity : callable or None
The nonlinearity that is applied to the input gate activation. If None
is provided, no nonlinearity will be applied.
Examples
--------
For :class:`LSTMLayer` the bias of the forget gate is often initialized to
a large positive value to encourage the layer initially remember the cell
value, see e.g. [1]_ page 15.
>>> import lasagne
>>> forget_gate = Gate(b=lasagne.init.Constant(5.0))
>>> l_lstm = LSTMLayer((10, 20, 30), num_units=10,
... forgetgate=forget_gate)
References
----------
.. [1] Gers, Felix A., Jürgen Schmidhuber, and Fred Cummins. "Learning to
forget: Continual prediction with LSTM." Neural computation 12.10
(2000): 2451-2471.
"""
def __init__(self, W_in=init.Normal(0.1), W_hid=init.Normal(0.1),
W_cell=init.Normal(0.1), b=init.Constant(0.),
nonlinearity=nonlinearities.sigmoid):
self.W_in = W_in
self.W_hid = W_hid
# Don't store a cell weight vector when cell is None
if W_cell is not None:
self.W_cell = W_cell
self.b = b
# For the nonlinearity, if None is supplied, use identity
if nonlinearity is None:
self.nonlinearity = nonlinearities.identity
else:
self.nonlinearity = nonlinearity
class LSTMLayer(MergeLayer):
r"""
lasagne.layers.recurrent.LSTMLayer(incoming, num_units,
ingate=lasagne.layers.Gate(), forgetgate=lasagne.layers.Gate(),
cell=lasagne.layers.Gate(
W_cell=None, nonlinearity=lasagne.nonlinearities.tanh),
outgate=lasagne.layers.Gate(),
nonlinearity=lasagne.nonlinearities.tanh,
cell_init=lasagne.init.Constant(0.),
hid_init=lasagne.init.Constant(0.), backwards=False, learn_init=False,
peepholes=True, gradient_steps=-1, grad_clipping=0, unroll_scan=False,
precompute_input=True, mask_input=None, only_return_final=False, **kwargs)
A long short-term memory (LSTM) layer.
Includes optional "peephole connections" and a forget gate. Based on the
definition in [1]_, which is the current common definition. The output is
computed by
.. math ::
i_t &= \sigma_i(x_t W_{xi} + h_{t-1} W_{hi}
+ w_{ci} \odot c_{t-1} + b_i)\\
f_t &= \sigma_f(x_t W_{xf} + h_{t-1} W_{hf}
+ w_{cf} \odot c_{t-1} + b_f)\\
c_t &= f_t \odot c_{t - 1}
+ i_t \odot \sigma_c(x_t W_{xc} + h_{t-1} W_{hc} + b_c)\\
o_t &= \sigma_o(x_t W_{xo} + h_{t-1} W_{ho} + w_{co} \odot c_t + b_o)\\
h_t &= o_t \odot \sigma_h(c_t)
Parameters
----------
incoming : a :class:`lasagne.layers.Layer` instance or a tuple
The layer feeding into this layer, or the expected input shape.
num_units : int
Number of hidden/cell units in the layer.
ingate : Gate
Parameters for the input gate (:math:`i_t`): :math:`W_{xi}`,
:math:`W_{hi}`, :math:`w_{ci}`, :math:`b_i`, and :math:`\sigma_i`.
forgetgate : Gate
Parameters for the forget gate (:math:`f_t`): :math:`W_{xf}`,
:math:`W_{hf}`, :math:`w_{cf}`, :math:`b_f`, and :math:`\sigma_f`.
cell : Gate
Parameters for the cell computation (:math:`c_t`): :math:`W_{xc}`,
:math:`W_{hc}`, :math:`b_c`, and :math:`\sigma_c`.
outgate : Gate
Parameters for the output gate (:math:`o_t`): :math:`W_{xo}`,
:math:`W_{ho}`, :math:`w_{co}`, :math:`b_o`, and :math:`\sigma_o`.
nonlinearity : callable or None
The nonlinearity that is applied to the output (:math:`\sigma_h`). If
None is provided, no nonlinearity will be applied.
cell_init : callable, np.ndarray, theano.shared or :class:`Layer`
Initializer for initial cell state (:math:`c_0`).
hid_init : callable, np.ndarray, theano.shared or :class:`Layer`
Initializer for initial hidden state (:math:`h_0`).
backwards : bool
If True, process the sequence backwards and then reverse the
output again such that the output from the layer is always
from :math:`x_1` to :math:`x_n`.
learn_init : bool
If True, initial hidden values are learned.
peepholes : bool
If True, the LSTM uses peephole connections.
When False, `ingate.W_cell`, `forgetgate.W_cell` and
`outgate.W_cell` are ignored.
gradient_steps : int
Number of timesteps to include in the backpropagated gradient.
If -1, backpropagate through the entire sequence.
grad_clipping : float
If nonzero, the gradient messages are clipped to the given value during
the backward pass. See [1]_ (p. 6) for further explanation.
unroll_scan : bool
If True the recursion is unrolled instead of using scan. For some
graphs this gives a significant speed up but it might also consume
more memory. When `unroll_scan` is True, backpropagation always
includes the full sequence, so `gradient_steps` must be set to -1 and
the input sequence length must be known at compile time (i.e., cannot
be given as None).
precompute_input : bool
If True, precompute input_to_hid before iterating through
the sequence. This can result in a speedup at the expense of
an increase in memory usage.
mask_input : :class:`lasagne.layers.Layer`
Layer which allows for a sequence mask to be input, for when sequences
are of variable length. Default `None`, which means no mask will be
supplied (i.e. all sequences are of the same length).
only_return_final : bool
If True, only return the final sequential output (e.g. for tasks where
a single target value for the entire sequence is desired). In this
case, Theano makes an optimization which saves memory.
References
----------
.. [1] Graves, Alex: "Generating sequences with recurrent neural networks."
arXiv preprint arXiv:1308.0850 (2013).
"""
def __init__(self, incoming, num_units,
ingate=Gate(),
forgetgate=Gate(),
cell=Gate(W_cell=None, nonlinearity=nonlinearities.tanh),
outgate=Gate(),
nonlinearity=nonlinearities.tanh,
cell_init=init.Constant(0.),
hid_init=init.Constant(0.),
backwards=False,
learn_init=False,
peepholes=True,
gradient_steps=-1,
grad_clipping=0,
unroll_scan=False,
precompute_input=True,
mask_input=None,
only_return_final=False,
**kwargs):
# This layer inherits from a MergeLayer, because it can have four
# inputs - the layer input, the mask, the initial hidden state and the
# inital cell state. We will just provide the layer input as incomings,
# unless a mask input, inital hidden state or initial cell state was
# provided.
incomings = [incoming]
self.mask_incoming_index = -1
self.hid_init_incoming_index = -1
self.cell_init_incoming_index = -1
if mask_input is not None:
incomings.append(mask_input)
self.mask_incoming_index = len(incomings)-1
if isinstance(hid_init, Layer):
incomings.append(hid_init)
self.hid_init_incoming_index = len(incomings)-1
if isinstance(cell_init, Layer):
incomings.append(cell_init)
self.cell_init_incoming_index = len(incomings)-1
# Initialize parent layer
super(LSTMLayer, self).__init__(incomings, **kwargs)
# If the provided nonlinearity is None, make it linear
if nonlinearity is None:
self.nonlinearity = nonlinearities.identity
else:
self.nonlinearity = nonlinearity
self.learn_init = learn_init
self.num_units = num_units
self.backwards = backwards
self.peepholes = peepholes
self.gradient_steps = gradient_steps
self.grad_clipping = grad_clipping
self.unroll_scan = unroll_scan
self.precompute_input = precompute_input
self.only_return_final = only_return_final
if unroll_scan and gradient_steps != -1:
raise ValueError(
"Gradient steps must be -1 when unroll_scan is true.")
# Retrieve the dimensionality of the incoming layer
input_shape = self.input_shapes[0]
if unroll_scan and input_shape[1] is None:
raise ValueError("Input sequence length cannot be specified as "
"None when unroll_scan is True")
num_inputs = np.prod(input_shape[2:])
def add_gate_params(gate, gate_name):
""" Convenience function for adding layer parameters from a Gate
instance. """
return (self.add_param(gate.W_in, (num_inputs, num_units),
name="W_in_to_{}".format(gate_name)),
self.add_param(gate.W_hid, (num_units, num_units),
name="W_hid_to_{}".format(gate_name)),
self.add_param(gate.b, (num_units,),
name="b_{}".format(gate_name),
regularizable=False),
gate.nonlinearity)
# Add in parameters from the supplied Gate instances
(self.W_in_to_ingate, self.W_hid_to_ingate, self.b_ingate,
self.nonlinearity_ingate) = add_gate_params(ingate, 'ingate')
(self.W_in_to_forgetgate, self.W_hid_to_forgetgate, self.b_forgetgate,
self.nonlinearity_forgetgate) = add_gate_params(forgetgate,
'forgetgate')
(self.W_in_to_cell, self.W_hid_to_cell, self.b_cell,
self.nonlinearity_cell) = add_gate_params(cell, 'cell')
(self.W_in_to_outgate, self.W_hid_to_outgate, self.b_outgate,
self.nonlinearity_outgate) = add_gate_params(outgate, 'outgate')
# If peephole (cell to gate) connections were enabled, initialize
# peephole connections. These are elementwise products with the cell
# state, so they are represented as vectors.
if self.peepholes:
self.W_cell_to_ingate = self.add_param(
ingate.W_cell, (num_units, ), name="W_cell_to_ingate")
self.W_cell_to_forgetgate = self.add_param(
forgetgate.W_cell, (num_units, ), name="W_cell_to_forgetgate")
self.W_cell_to_outgate = self.add_param(
outgate.W_cell, (num_units, ), name="W_cell_to_outgate")
# Setup initial values for the cell and the hidden units
if isinstance(cell_init, Layer):
self.cell_init = cell_init
else:
self.cell_init = self.add_param(
cell_init, (1, num_units), name="cell_init",
trainable=learn_init, regularizable=False)
if isinstance(hid_init, Layer):
self.hid_init = hid_init
else:
self.hid_init = self.add_param(
hid_init, (1, self.num_units), name="hid_init",
trainable=learn_init, regularizable=False)
def get_output_shape_for(self, input_shapes):
# The shape of the input to this layer will be the first element
# of input_shapes, whether or not a mask input is being used.
input_shape = input_shapes[0]
# When only_return_final is true, the second (sequence step) dimension
# will be flattened
if self.only_return_final:
return input_shape[0], self.num_units
# Otherwise, the shape will be (n_batch, n_steps, num_units)
else:
return input_shape[0], input_shape[1], self.num_units
def get_output_for(self, inputs, **kwargs):
"""
Compute this layer's output function given a symbolic input variable
Parameters
----------
inputs : list of theano.TensorType
`inputs[0]` should always be the symbolic input variable. When
this layer has a mask input (i.e. was instantiated with
`mask_input != None`, indicating that the lengths of sequences in
each batch vary), `inputs` should have length 2, where `inputs[1]`
is the `mask`. The `mask` should be supplied as a Theano variable
denoting whether each time step in each sequence in the batch is
part of the sequence or not. `mask` should be a matrix of shape
``(n_batch, n_time_steps)`` where ``mask[i, j] = 1`` when ``j <=
(length of sequence i)`` and ``mask[i, j] = 0`` when ``j > (length
of sequence i)``. When the hidden state of this layer is to be
pre-filled (i.e. was set to a :class:`Layer` instance) `inputs`
should have length at least 2, and `inputs[-1]` is the hidden state
to prefill with. When the cell state of this layer is to be
pre-filled (i.e. was set to a :class:`Layer` instance) `inputs`
should have length at least 2, and `inputs[-1]` is the hidden state
to prefill with. When both the cell state and the hidden state are
being pre-filled `inputs[-2]` is the hidden state, while
`inputs[-1]` is the cell state.
Returns
-------
layer_output : theano.TensorType
Symbolic output variable.
"""
# Retrieve the layer input
input = inputs[0]
# Retrieve the mask when it is supplied
mask = None
hid_init = None
cell_init = None
if self.mask_incoming_index > 0:
mask = inputs[self.mask_incoming_index]
if self.hid_init_incoming_index > 0:
hid_init = inputs[self.hid_init_incoming_index]
if self.cell_init_incoming_index > 0:
cell_init = inputs[self.cell_init_incoming_index]
# Treat all dimensions after the second as flattened feature dimensions
if input.ndim > 3:
input = T.flatten(input, 3)
# Because scan iterates over the first dimension we dimshuffle to
# (n_time_steps, n_batch, n_features)
input = input.dimshuffle(1, 0, 2)
seq_len, num_batch, _ = input.shape
# Stack input weight matrices into a (num_inputs, 4*num_units)
# matrix, which speeds up computation
W_in_stacked = T.concatenate(
[self.W_in_to_ingate, self.W_in_to_forgetgate,
self.W_in_to_cell, self.W_in_to_outgate], axis=1)
# Same for hidden weight matrices
W_hid_stacked = T.concatenate(
[self.W_hid_to_ingate, self.W_hid_to_forgetgate,
self.W_hid_to_cell, self.W_hid_to_outgate], axis=1)
# Stack biases into a (4*num_units) vector
b_stacked = T.concatenate(
[self.b_ingate, self.b_forgetgate,
self.b_cell, self.b_outgate], axis=0)
if self.precompute_input:
# Because the input is given for all time steps, we can
# precompute_input the inputs dot weight matrices before scanning.
# W_in_stacked is (n_features, 4*num_units). input is then
# (n_time_steps, n_batch, 4*num_units).
input = T.dot(input, W_in_stacked) + b_stacked
# When theano.scan calls step, input_n will be (n_batch, 4*num_units).
# We define a slicing function that extract the input to each LSTM gate
def slice_w(x, n):
return x[:, n*self.num_units:(n+1)*self.num_units]
# Create single recurrent computation step function
# input_n is the n'th vector of the input
def step(input_n, cell_previous, hid_previous, *args):
if not self.precompute_input:
input_n = T.dot(input_n, W_in_stacked) + b_stacked
# Calculate gates pre-activations and slice
gates = input_n + T.dot(hid_previous, W_hid_stacked)
# Clip gradients
if self.grad_clipping:
gates = theano.gradient.grad_clip(
gates, -self.grad_clipping, self.grad_clipping)
# Extract the pre-activation gate values