forked from lisa-lab/DeepLearningTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlstm.py
More file actions
1238 lines (981 loc) · 41.3 KB
/
lstm.py
File metadata and controls
1238 lines (981 loc) · 41.3 KB
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
'''
Build a tweet sentiment analyzer
'''
from collections import OrderedDict
import cPickle as pkl
import sys
import time
import numpy
import theano
from theano import config
import theano.tensor as tensor
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
import imdb
import random
#config.mode = 'DebugMode'
config.floatX = 'float64'
datasets = {'imdb': (imdb.load_data, imdb.prepare_data)}
# Set the random number generators' seeds for consistency
SEED = 123
numpy.random.seed(SEED)
def numpy_floatX(data):
return numpy.asarray(data, dtype=config.floatX)
def get_minibatches_idx(n, minibatch_size, shuffle=False):
"""
Used to shuffle the dataset at each iteration.
"""
idx_list = numpy.arange(n, dtype="int32")
if shuffle:
numpy.random.shuffle(idx_list)
minibatches = []
minibatch_start = 0
for i in range(n // minibatch_size):
minibatches.append(idx_list[minibatch_start:
minibatch_start + minibatch_size])
minibatch_start += minibatch_size
if (minibatch_start != n):
# Make a minibatch out of what is left
minibatches.append(idx_list[minibatch_start:])
return zip(range(len(minibatches)), minibatches)
def get_dataset(name):
return datasets[name][0], datasets[name][1]
def zipp(params, tparams):
"""
When we reload the model. Needed for the GPU stuff.
"""
for kk, vv in params.iteritems():
tparams[kk].set_value(vv)
def unzip(zipped):
"""
When we pickle the model. Needed for the GPU stuff.
"""
new_params = OrderedDict()
for kk, vv in zipped.iteritems():
new_params[kk] = vv.get_value()
return new_params
def dropout_layer(state_before, use_noise, trng):
proj = tensor.switch(use_noise,
(state_before *
trng.binomial(state_before.shape,
p=0.5, n=1,
dtype=state_before.dtype)),
state_before * 0.5)
return proj
def _p(pp, name):
return '%s_%s' % (pp, name)
def init_params(options):
"""
Global (not LSTM) parameter. For the embeding and the classifier.
"""
params = OrderedDict()
# embedding
randn = numpy.random.rand(options['n_words'],
options['dim_proj'])
params['Wemb'] = (0.01 * randn).astype(config.floatX)
params = get_layer(options['encoder'])[0](options,
params,
prefix=options['encoder'])
# classifier
params['U'] = 0.01 * numpy.random.randn(options['dim_proj'],
options['ydim']).astype(config.floatX)
params['b'] = numpy.zeros((options['ydim'],)).astype(config.floatX)
return params
def load_params(path, params):
pp = numpy.load(path)
for kk, vv in params.iteritems():
if kk not in pp:
raise Warning('%s is not in the archive' % kk)
params[kk] = pp[kk]
return params
def init_tparams(params):
tparams = OrderedDict()
for kk, pp in params.iteritems():
tparams[kk] = theano.shared(params[kk], name=kk)
return tparams
def get_layer(name):
fns = layers[name]
return fns
def ortho_weight(ndim):
W = numpy.random.randn(ndim, ndim)
u, s, v = numpy.linalg.svd(W)
return u.astype(config.floatX)
def param_init_lstm(options, params, prefix='lstm'):
"""
Init the LSTM parameter:
:see: init_params
"""
W = numpy.concatenate([ortho_weight(options['dim_proj']),
ortho_weight(options['dim_proj']),
ortho_weight(options['dim_proj']),
ortho_weight(options['dim_proj'])], axis=1)
params[_p(prefix, 'W')] = W
U = numpy.concatenate([ortho_weight(options['dim_proj']),
ortho_weight(options['dim_proj']),
ortho_weight(options['dim_proj']),
ortho_weight(options['dim_proj'])], axis=1)
params[_p(prefix, 'U')] = U
b = numpy.zeros((4 * options['dim_proj'],))
params[_p(prefix, 'b')] = b.astype(config.floatX)
return params
def lstm_layer(tparams, state_below, options, prefix='lstm', x=None):
nsteps = state_below.shape[0]
if state_below.ndim == 3:
n_samples = state_below.shape[1]
else:
n_samples = 1
assert x is not None
def _slice(_x, n, dim):
if _x.ndim == 3:
return _x[:, :, n * dim:(n + 1) * dim]
return _x[:, n * dim:(n + 1) * dim]
def _step(m_, x_, h_, c_):
preact = tensor.dot(h_, tparams[_p(prefix, 'U')])
preact += x_
# x_printed = theano.printing.Print('this is a very important value')(preact)
# f = theano.function([preact], preact)
# f_with_print = theano.function([preact], x_printed)
i = tensor.nnet.sigmoid(_slice(preact, 0, options['dim_proj']))
f = tensor.nnet.sigmoid(_slice(preact, 1, options['dim_proj']))
o = tensor.nnet.sigmoid(_slice(preact, 2, options['dim_proj']))
c = tensor.tanh(_slice(preact, 3, options['dim_proj']))
c = f * c_ + i * c
c = m_[:, None] * c + (1. - m_)[:, None] * c_
h = o * tensor.tanh(c)
h = m_[:, None] * h + (1. - m_)[:, None] * h_
return h, c
#print state_below
state_below = (tensor.dot(state_below, tparams[_p(prefix, 'W')]) +
tparams[_p(prefix, 'b')])
dim_proj = options['dim_proj']
#print config.floatX
rval, updates = theano.scan(_step,
sequences=[x, state_below],
outputs_info=[tensor.alloc(numpy_floatX(0.),
n_samples,
dim_proj),
tensor.alloc(numpy_floatX(0.),
n_samples,
dim_proj)],
name=_p(prefix, '_layers'),
n_steps=nsteps)
return rval[0],nsteps,state_below,rval
# ff: Feed Forward (normal neural net), only useful to put after lstm
# before the classifier.
layers = {'lstm': (param_init_lstm, lstm_layer)}
def sgd(lr, tparams, grads, x, y, cost):
""" Stochastic Gradient Descent
:note: A more complicated version of sgd then needed. This is
done like that for adadelta and rmsprop.
"""
# New set of shared variable that will contain the gradient
# for a mini-batch.
gshared = [theano.shared(p.get_value() * 0., name='%s_grad' % k)
for k, p in tparams.iteritems()]
gsup = [(gs, g) for gs, g in zip(gshared, grads)]
# Function that computes gradients for a mini-batch, but do not
# updates the weights.
f_grad_shared = theano.function([x, y], cost, updates=gsup,
name='sgd_f_grad_shared')
pup = [(p, p - lr * g) for p, g in zip(tparams.values(), gshared)]
# Function that updates the weights from the previously computed
# gradient.
f_update = theano.function([lr], [], updates=pup,
name='sgd_f_update')
return f_grad_shared, f_update
def adadelta(lr, tparams, grads, x, y, cost):
"""
An adaptive learning rate optimizer
Parameters
----------
lr : Theano SharedVariable
Initial learning rate
tpramas: Theano SharedVariable
Model parameters
grads: Theano variable
Gradients of cost w.r.t to parameres
x: Theano variable
Model inputs
mask: Theano variable
Sequence mask
y: Theano variable
Targets
cost: Theano variable
Objective fucntion to minimize
Notes
-----
For more information, see [ADADELTA]_.
.. [ADADELTA] Matthew D. Zeiler, *ADADELTA: An Adaptive Learning
Rate Method*, arXiv:1212.5701.
"""
zipped_grads = [theano.shared(p.get_value() * numpy_floatX(0.),
name='%s_grad' % k)
for k, p in tparams.iteritems()]
running_up2 = [theano.shared(p.get_value() * numpy_floatX(0.),
name='%s_rup2' % k)
for k, p in tparams.iteritems()]
running_grads2 = [theano.shared(p.get_value() * numpy_floatX(0.),
name='%s_rgrad2' % k)
for k, p in tparams.iteritems()]
zgup = [(zg, g) for zg, g in zip(zipped_grads, grads)]
rg2up = [(rg2, 0.95 * rg2 + 0.05 * (g ** 2))
for rg2, g in zip(running_grads2, grads)]
# f_grad_shared = theano.function([x, mask, y], cost, updates=zgup + rg2up,
# name='adadelta_f_grad_shared', allow_input_downcast=True)
#print type(x)
#cost = theano.function([x],x,on_unused_input='warn')
# f_grad_shared = theano.function([x], x, name='adadelta_f_grad_shared',on_unused_input='warn')
f_grad_shared = theano.function([x, y], cost, updates=zgup + rg2up,
name='adadelta_f_grad_shared')
updir = [-tensor.sqrt(ru2 + 1e-6) / tensor.sqrt(rg2 + 1e-6) * zg
for zg, ru2, rg2 in zip(zipped_grads,
running_up2,
running_grads2)]
ru2up = [(ru2, 0.95 * ru2 + 0.05 * (ud ** 2))
for ru2, ud in zip(running_up2, updir)]
param_up = [(p, p + ud) for p, ud in zip(tparams.values(), updir)]
f_update = theano.function([lr], [], updates=ru2up + param_up,
on_unused_input='ignore',
name='adadelta_f_update')
return f_grad_shared, f_update
def rmsprop(lr, tparams, grads, x, y, cost):
"""
A variant of SGD that scales the step size by running average of the
recent step norms.
Parameters
----------
lr : Theano SharedVariable
Initial learning rate
tpramas: Theano SharedVariable
Model parameters
grads: Theano variable
Gradients of cost w.r.t to parameres
x: Theano variable
Model inputs
mask: Theano variable
Sequence mask
y: Theano variable
Targets
cost: Theano variable
Objective fucntion to minimize
Notes
-----
For more information, see [Hint2014]_.
.. [Hint2014] Geoff Hinton, *Neural Networks for Machine Learning*,
lecture 6a,
http://cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf
"""
zipped_grads = [theano.shared(p.get_value() * numpy_floatX(0.),
name='%s_grad' % k)
for k, p in tparams.iteritems()]
running_grads = [theano.shared(p.get_value() * numpy_floatX(0.),
name='%s_rgrad' % k)
for k, p in tparams.iteritems()]
running_grads2 = [theano.shared(p.get_value() * numpy_floatX(0.),
name='%s_rgrad2' % k)
for k, p in tparams.iteritems()]
zgup = [(zg, g) for zg, g in zip(zipped_grads, grads)]
rgup = [(rg, 0.95 * rg + 0.05 * g) for rg, g in zip(running_grads, grads)]
rg2up = [(rg2, 0.95 * rg2 + 0.05 * (g ** 2))
for rg2, g in zip(running_grads2, grads)]
f_grad_shared = theano.function([x, y], cost,
updates=zgup + rgup + rg2up,
name='rmsprop_f_grad_shared')
updir = [theano.shared(p.get_value() * numpy_floatX(0.),
name='%s_updir' % k)
for k, p in tparams.iteritems()]
updir_new = [(ud, 0.9 * ud - 1e-4 * zg / tensor.sqrt(rg2 - rg ** 2 + 1e-4))
for ud, zg, rg, rg2 in zip(updir, zipped_grads, running_grads,
running_grads2)]
param_up = [(p, p + udn[1])
for p, udn in zip(tparams.values(), updir_new)]
f_update = theano.function([lr], [], updates=updir_new + param_up,
on_unused_input='ignore',
name='rmsprop_f_update')
return f_grad_shared, f_update
def build_model(tparams, options):
trng = RandomStreams(SEED)
# Used for dropout.
use_noise = theano.shared(numpy_floatX(0.))
x = tensor.matrix('x', dtype='int64')
y = tensor.vector('y', dtype='int64')
n_timesteps = x.shape[0]
n_samples = x.shape[1]
emb = tparams['Wemb'][x.flatten()].reshape([n_timesteps,
n_samples,
options['dim_proj']])
proj,nsteps,state_below,rval = get_layer(options['encoder'])[1](tparams, emb, options,
prefix=options['encoder'],
x=x)
proj_layer = proj
if options['encoder'] == 'lstm':
proj = (proj * x[:, :, None]).sum(axis=0)
proj = proj / x.sum(axis=0)[:, None]
proj_encoder = proj
if options['use_dropout']:
proj = dropout_layer(proj, use_noise, trng)
proj_dropout = proj
pred = tensor.nnet.softmax(tensor.dot(proj, tparams['U']) + tparams['b'])
f_pred_prob = theano.function([x], pred, name='f_pred_prob')
f_pred = theano.function([x], pred.argmax(axis=1), name='f_pred')
get_nsteps = theano.function([x], nsteps, name='get_nsteps',on_unused_input='warn')
get_proj_layer = theano.function([x], proj_layer, name='proj_layer',on_unused_input='warn')
get_proj_encoder = theano.function([x], proj_encoder, name='proj_encoder',on_unused_input='warn')
get_proj_dropout = theano.function([x], proj_dropout, name='proj_dropout',on_unused_input='warn')
get_state_below = theano.function([x], state_below, name='get_state_below',on_unused_input='warn')
get_rval = theano.function([x], rval, name='get_rval',on_unused_input='warn')
off = 1e-8
if pred.dtype == 'float16':
off = 1e-6
_EPSILON = 0.01
proj /= proj.sum(axis=-1, keepdims=True)
# avoid numerical instability with _EPSILON clipping
proj = tensor.clip(proj, _EPSILON, 1.0 - _EPSILON)
cost = tensor.nnet.categorical_crossentropy(proj, y)
# train_accuracy = K.mean(K.equal(K.argmax(self.y, axis=-1),
# K.argmax(self.y_train, axis=-1)))
#cost = -tensor.log(pred[tensor.arange(n_samples), y] + off).mean()
return use_noise, x, y, f_pred_prob, f_pred, cost,get_nsteps, get_proj_layer, get_proj_encoder, get_proj_dropout,get_state_below,get_rval
def pred_probs(f_pred_prob, prepare_data, data, iterator, dic, verbose=False):
""" If you want to use a trained model, this is useful to compute
the probabilities of new examples.
"""
n_samples = len(data[0])
voc_size = len(dic['words2idx'].keys()) + 1
print "n_samples {} voc_size {}".format(n_samples,voc_size)
#print numpy.max(data[1])
#probs = numpy.zeros((n_samples, 2)).astype(config.floatX)
probs = numpy.zeros((n_samples, voc_size)).astype(config.floatX)
n_done = 0
for _, valid_index in iterator:
x, mask, y = prepare_data([data[0][t] for t in valid_index],
numpy.array(data[1])[valid_index],
maxlen=None)
pred_probs = f_pred_prob(x)
probs[valid_index, :] = pred_probs
n_done += len(valid_index)
if verbose:
print '%d/%d samples classified' % (n_done, n_samples)
return probs
def samples(f_pred,x ,dic , verbose=False):
""" If you want to use a trained model, this is useful to compute
the probabilities of new examples.
"""
n_samples = len(x)
voc_size = len(dic['words2idx'].keys()) + 1
#print "n_samples {} voc_size {}".format(n_samples,voc_size)
#print x
#x_ixes = [[x[0][0]],[x[1][0]]]
#x_ixes = x
#x_ixes = [list(x[0])]
#print x
x = [x[0][0:10]]
print x
x_ixes = list(x[0])
x = [[word] for word in x[0]]
#print x
#x_ixes = [list(x[0])]
#x_ixes = list(x[0])
#x_aux = list(x)
for i in xrange(0,100):
#pred_probs = f_pred_prob(x)
predicted = f_pred(x)[0]
#print predicted
if predicted != 0:
#predicted = predicted + 1
x_ixes.append(predicted)
x = numpy.zeros((len(x_ixes),1),dtype='int64')
for j in xrange(0,len(x_ixes)):
x[j] = [x_ixes[j]]
else:
print "there is a 0 in da hood"
sample = "".join([dic['idx2word'][w_ix] for w_ix in x_ixes])
#sample = " ".join([dic['idx2word'][w_ix] for w_ix in x_ixes[0]])
print sample
#return probs
def pred_error(f_pred, prepare_data, data, iterator, verbose=False):
"""
Just compute the error
f_pred: Theano fct computing the prediction
prepare_data: usual prepare_data for that dataset.
"""
valid_err = 0
for _, valid_index in iterator:
x, mask, y = prepare_data([data[0][t] for t in valid_index],
numpy.array(data[1])[valid_index],
maxlen=None)
preds = f_pred(x)
targets = numpy.array(data[1])[valid_index]
valid_err += (preds == targets).sum()
valid_err = 1. - numpy_floatX(valid_err) / len(data[0])
return valid_err
def pred_error_char(f_pred, prepare_data, data, iterator, verbose=False):
"""
Just compute the error
f_pred: Theano fct computing the prediction
prepare_data: usual prepare_data for that dataset.
"""
valid_err = 0
for _, valid_index in iterator:
y = [data[1][t] for t in valid_index]
x = [data[0][t] for t in valid_index]
x = numpy.array(x).T
#x, mask, y = prepare_data([data[0][t] for t in valid_index],
# numpy.array(data[1])[valid_index],
# maxlen=None)
preds = f_pred(x)
targets = numpy.array(data[1])[valid_index]
print targets
print preds
valid_err += (preds == targets).sum()
print valid_err
#print valid_err
valid_err = 1. - numpy_floatX(valid_err) / len(data[0])
return valid_err
def remove_return_lines_and_quotes(text):
text = text.replace('\n', ' ')
text = text.replace('\t', ' ')
text = text.replace('\r', ' ')
text = text.replace('"', '')
text = text.replace('-', '')
return text
def load_text():
import nltk
import numpy as np
from nltk.corpus import gutenberg
import csv
from bs4 import BeautifulSoup
# with open('product_description_clean.csv', 'rU') as csvfile:
# reader = csv.reader(csvfile, delimiter=',', quotechar='"', dialect=csv.excel_tab)
# next(reader, None) # skip the headers
# documents = []
# err_docs = 0
# for row in reader:
# try:
# #d = BeautifulSoup(row[0]).getText()
# d = BeautifulSoup(row[0], "html.parser").getText()
# documents.append(d)
# except Exception, e:
# #print e
# err_docs +=1
# print "there are {} errors".format(err_docs)
dic = {'labels2idx' : {} , 'words2idx' : {}}
classes = {}
#text = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.'
print '...loading corpus'
text = gutenberg.raw()
#text = " ".join(documents)
text = text[0:1000000]
# text = text[0:100] + '.'
# print text
#text = text[0:1000000]
text = remove_return_lines_and_quotes(text)
#print "text lengh:{}".format(len(documents))
#print text
#text = self.remove_return_lines_and_quotes(text)
print "...tokenizing"
sentences = nltk.sent_tokenize(text)
sentences = [nltk.word_tokenize(sent) for sent in sentences]
print "...creating indexes"
for sentence in sentences:
for word in sentence:
dic['words2idx'][word.lower()] = 1
for i in range(0,len(dic['words2idx'].keys())):
key = dic['words2idx'].keys()[i]
dic['words2idx'][key] = i + 1
sentences_words,sentences_ne,sentences_labels = [],[],[]
words , labels = [],[]
for sentence in sentences:
for i in range(0,len(sentence)):
words.append([dic['words2idx'][sentence[i].lower()]])
if (i + 1) == len(sentence):
labels.append(dic['words2idx']['.'])
else:
labels.append(dic['words2idx'][sentence[i+1].lower()])
print "...creating training data"
# X = np.array(sentences_words)
# Y = np.array(sentences_labels)
X = np.array(words, dtype='int64')
Y = np.array(labels, dtype='int64')
train_length = int(round(len(X) * 0.90))
valid_length = int(round(len(X) * 0.05))
test_length = int(round(len(X) * 0.05))
X_train = X[0:train_length]
X_valid = X[train_length: (train_length + valid_length)]
X_test = X[-test_length:]
Y_train = Y[0:train_length]
Y_valid = Y[train_length: (train_length + valid_length)]
Y_test = Y[-test_length:]
train = [X_train,Y_train]
valid = [X_valid,Y_valid]
test = [X_test,Y_test]
return train,valid,test,dic
def load_sequences():
import nltk
import numpy as np
from nltk.corpus import gutenberg
import csv
from bs4 import BeautifulSoup
from sys import getsizeof
dic = {'labels2idx' : {} , 'words2idx' : {}}
classes = {}
#text = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.'
print '...loading corpus'
#f = open('../data/text_input.txt', 'r')
f = open('../data/text_input_2.txt', 'r')
text = f.read()
text = text.decode('utf-8','replace')
#print text
f.close()
text = gutenberg.raw()
#text = " ".join(documents)
#text = text[0:4000000]
#text = text[0:1000000]
text = text[0:100000]
#text = text[0:300]
print getsizeof(text)
#text = text[0:100] + '.'
# print text
#text = text[0:1000000]
text = remove_return_lines_and_quotes(text)
#print "text lengh:{}".format(len(documents))
#print text
#text = self.remove_return_lines_and_quotes(text)
print "...tokenizing"
sentences = nltk.sent_tokenize(text)
sentences = [nltk.word_tokenize(sent) for sent in sentences]
print "...creating indexes"
text_words = []
for sentence in sentences:
for word in sentence:
dic['words2idx'][" ".lower()] = 1
text_words.append(" ")
#uncomment this for jsut words
# dic['words2idx'][word.lower()] = 1
# text_words.append(word)
for char in word:
dic['words2idx'][char.lower()] = 1
text_words.append(char)
for i in range(0,len(dic['words2idx'].keys())):
key = dic['words2idx'].keys()[i]
dic['words2idx'][key] = i + 1
#print dic['words2idx']
sequence_length = 1
num_sequences = len(text_words) / sequence_length
words , labels = [],[]
#for sentence in sentences:
#for sentence in sentences:
print "num_sequences: {}".format(num_sequences)
for i in range(0,len(text_words)):
sequence = text_words[ i: i + sequence_length]
words_aux = []
for item in sequence:
words_aux.append(dic['words2idx'][item.lower()])
words.append(words_aux)
if (i + sequence_length + 1) >= len(text_words):
print "only once"
labels.append(dic['words2idx']['.'])
else:
#print i
labels.append(dic['words2idx'][text_words[i + sequence_length + 1].lower()])
print "vocabulary size: {}".format(len(dic['words2idx'].keys()))
print "...creating training data"
# print words
# print labels
X = words
Y = labels
train_length = int(round(len(X) * 0.90))
valid_length = int(round(len(X) * 0.05))
test_length = int(round(len(X) * 0.05))
X_train = X[0:train_length]
X_valid = X[train_length: (train_length + valid_length)]
X_test = X[-test_length:]
Y_train = Y[0:train_length]
Y_valid = Y[train_length: (train_length + valid_length)]
Y_test = Y[-test_length:]
train = [X_train,Y_train]
valid = [X_valid,Y_valid]
test = [X_test,Y_test]
return train,valid,test,dic
def contextwin(l, win):
'''
win :: int corresponding to the size of the window
given a list of indexes composing a sentence
it will return a list of list of indexes corresponding
to context windows surrounding each word in the sentence
'''
assert (win % 2) == 1
assert win >=1
l = list(l)
lpadded = win/2 * [-1] + l + win/2 * [-1]
out = [ lpadded[i:i+win] for i in range(len(l)) ]
assert len(out) == len(l)
return out
def contextwinright(l, win):
'''
win :: int corresponding to the size of the window
given a list of indexes composing a sentence
it will return a list of list of indexes corresponding
to context windows surrounding each word in the sentence
'''
assert (win % 2) == 1
#assert (win % 2) == 0
assert win >=1
l = list(l)
#lpadded = win/2 * [-1] + l + win/2 * [-1]
lpadded = win/2 * [-1] + win/2 * [-1] + l
#lpadded = win/2 * [-1] + l
#print lpadded
out = [ lpadded[i:i+win] for i in range(len(l)) ]
assert len(out) == len(l)
return out
def context(l, win):
'''
win :: int corresponding to the size of the window
given a list of indexes composing a sentence
it will return a list of list of indexes corresponding
to context windows surrounding each word in the sentence
'''
#assert (win % 2) == 1
#assert (win % 2) == 0
assert win >=1
l = list(l)
out = []
for i in range(0,len(l)):
if i < win:
#s = l[0 : i + 1 ]
if i == 0:
s = l[0 : i + 1 ]
#s = [1] + l[0 : i + 1 ]
#else:
# s = l[0 : i + 1 ]
else:
s = l[ (i - 1) : (i - 1) + win]
#s = l[ (i - 1) : (i - 1) + win]
#s = numpy.array(s,dtype='int64')
#s = [ [i] for i in s]
s = numpy.array(s)
#out.append([s])
out.append(s)
assert len(out) == len(l)
out = numpy.array(out)
return out
def sampleK(h, seed_ix, n):
"""
sample a sequence of integers from the model
h is memory state, seed_ix is seed letter for first time step
"""
x = numpy.zeros((vocab_size, 1))
#print vocab_size
x[seed_ix] = 1
ixes = []
for t in xrange(n):
h = np.tanh(np.dot(Wxh, x) + np.dot(Whh, h) + bh)
#print h.shape
y = np.dot(Why, h) + by
#print y.shape
p = np.exp(y) / np.sum(np.exp(y))
#print p.shape
ix = np.random.choice(range(vocab_size), p=p.ravel())
#print ix
x = np.zeros((vocab_size, 1))
x[ix] = 1
ixes.append(ix)
return ixes
def train_lstm(
dim_proj=128, # word embeding dimension and LSTM number of hidden units.
patience=20, # Number of epoch to wait before early stop if no progress
max_epochs=5000, # The maximum number of epoch to run
dispFreq=10, # Display to stdout the training progress every N updates
decay_c=0., # Weight decay for the classifier applied to the U weights.
lrate=0.001, # Learning rate for sgd (not used for adadelta and rmsprop)
n_words=100, # Vocabulary size
optimizer=adadelta, # sgd, adadelta and rmsprop available, sgd very hard to use, not recommanded (probably need momentum and decaying learning rate).
encoder='lstm', # TODO: can be removed must be lstm.
saveto='lstm_model.npz', # The best model will be saved there
validFreq=370 * 2, # Compute the validation error after this number of update.
saveFreq=1110, # Save the parameters after every saveFreq updates
maxlen=100, # Sequence longer then this get ignored
batch_size=25, # The batch size during training.
valid_batch_size=25, # The batch size used for validation/test set.
dataset='imdb',
# Parameter for extra option
noise_std=0.,
use_dropout=True, # if False slightly faster, but worst test error
# This frequently need a bigger model.
reload_model=None, # Path to a saved model we want to start from.
test_size=-1, # If >0, we keep only this number of test example.
):
# load_sequences_chars()
# return 1
#print context(range(0,16), 4)
#return 1
# Model options
model_options = locals().copy()
print "model options", model_options
load_data, prepare_data = get_dataset(dataset)
print 'Loading data'
# train, valid, test = load_data(n_words=n_words, valid_portion=0.05,
# maxlen=maxlen)
#train,valid,test,dic = load_text()
train,valid,test,dic = load_sequences()
if test_size > 0:
# The test set is sorted by size, but we want to keep random
# size example. So we must select a random selection of the
# examples.
idx = numpy.arange(len(test[0]))
numpy.random.shuffle(idx)
idx = idx[:test_size]
test = ([test[0][n] for n in idx], [test[1][n] for n in idx])
ydim = numpy.max(train[1]) + 1
#print ydim
model_options['ydim'] = ydim
print 'Building model'
# This create the initial parameters as numpy ndarrays.
# Dict name (string) -> numpy ndarray
params = init_params(model_options)
if reload_model:
load_params('lstm_model.npz', params)
# This create Theano Shared Variable from the parameters.
# Dict name (string) -> Theano Tensor Shared Variable
# params and tparams have different copy of the weights.
tparams = init_tparams(params)
# use_noise is for dropout
(use_noise, x,
y, f_pred_prob, f_pred, cost,get_nsteps,get_proj_layer, get_proj_encoder, get_proj_dropout,get_state_below,get_rval) = build_model(tparams, model_options)
# print train[0][0:10]
# print train[1][0:10]
dic['idx2word'] = dict((k, v) for v, k in dic['words2idx'].iteritems())
#print dic['idx2word'][2]
# for v, k in dic['words2idx'].iteritems():
# print v
# print k
if decay_c > 0.:
decay_c = theano.shared(numpy_floatX(decay_c), name='decay_c')
weight_decay = 0.
weight_decay += (tparams['U'] ** 2).sum()
weight_decay *= decay_c
cost += weight_decay
f_cost = theano.function([x, y], cost, name='f_cost')
grads = tensor.grad(cost, wrt=tparams.values())
f_grad = theano.function([x, y], grads, name='f_grad')
lr = tensor.scalar(name='lr')
f_grad_shared, f_update = optimizer(lr, tparams, grads,
x, y, cost)
print 'Optimization'
kf_valid = get_minibatches_idx(len(valid[0]), valid_batch_size)
kf_test = get_minibatches_idx(len(test[0]), valid_batch_size)
print "%d train examples" % len(train[0])
print "%d valid examples" % len(valid[0])
print "%d test examples" % len(test[0])
history_errs = []
best_p = None
bad_count = 0
if validFreq == -1:
validFreq = len(train[0]) / batch_size
if saveFreq == -1:
saveFreq = len(train[0]) / batch_size
uidx = 0 # the number of update done
estop = False # early stop
start_time = time.time()
try:
for eidx in xrange(max_epochs):
n_samples = 0
# Get new shuffled index for the training set.
#kf = get_minibatches_idx(len(train[0]), batch_size, shuffle=True)
kf = get_minibatches_idx(len(train[0]), batch_size, shuffle=False)
#print kf
#print len(train[0])
#print kf
for batch_index, train_index in kf:
#print "batch_index".format(batch_index)
#print "--------------------------------"
#print train_index
uidx += 1
use_noise.set_value(1.)