1
+
2
+ from __future__ import print_function
3
+ from six .moves import xrange
4
+ import six .moves .cPickle as pickle
5
+
1
6
from collections import OrderedDict
2
7
import copy
3
- import cPickle
4
8
import gzip
5
9
import os
6
10
import urllib
@@ -66,7 +70,10 @@ def atisfold(fold):
66
70
assert fold in range (5 )
67
71
filename = os .path .join (PREFIX , 'atis.fold' + str (fold )+ '.pkl.gz' )
68
72
f = gzip .open (filename , 'rb' )
69
- train_set , valid_set , test_set , dicts = cPickle .load (f )
73
+ try :
74
+ train_set , valid_set , test_set , dicts = pickle .load (f , encoding = 'latin1' )
75
+ except :
76
+ train_set , valid_set , test_set , dicts = pickle .load (f )
70
77
return train_set , valid_set , test_set , dicts
71
78
72
79
@@ -107,7 +114,7 @@ def download(origin, destination):
107
114
download the corresponding atis file
108
115
from http://www-etud.iro.umontreal.ca/~mesnilgr/atis/
109
116
'''
110
- print 'Downloading data from %s' % origin
117
+ print ( 'Downloading data from %s' % origin )
111
118
urllib .urlretrieve (origin , destination )
112
119
113
120
@@ -125,8 +132,10 @@ def get_perf(filename, folder):
125
132
stdin = subprocess .PIPE ,
126
133
stdout = subprocess .PIPE )
127
134
128
- stdout , _ = proc .communicate ('' .join (open (filename ).readlines ()))
135
+ stdout , _ = proc .communicate ('' .join (open (filename ).readlines ()).encode ('utf-8' ))
136
+ stdout = stdout .decode ('utf-8' )
129
137
out = None
138
+
130
139
for line in stdout .split ('\n ' ):
131
140
if 'accuracy' in line :
132
141
out = line .split ()
@@ -237,7 +246,7 @@ def recurrence(x_t, h_tm1):
237
246
def train (self , x , y , window_size , learning_rate ):
238
247
239
248
cwords = contextwin (x , window_size )
240
- words = map (lambda x : numpy .asarray (x ).astype ('int32' ), cwords )
249
+ words = list ( map (lambda x : numpy .asarray (x ).astype ('int32' ), cwords ) )
241
250
labels = y
242
251
243
252
self .sentence_train (words , labels , learning_rate )
@@ -274,7 +283,7 @@ def main(param=None):
274
283
'nepochs' : 60 ,
275
284
# 60 is recommended
276
285
'savemodel' : False }
277
- print param
286
+ print ( param )
278
287
279
288
folder_name = os .path .basename (__file__ ).split ('.' )[0 ]
280
289
folder = os .path .join (os .path .dirname (__file__ ), folder_name )
@@ -284,8 +293,8 @@ def main(param=None):
284
293
# load the dataset
285
294
train_set , valid_set , test_set , dic = atisfold (param ['fold' ])
286
295
287
- idx2label = dict ((k , v ) for v , k in dic ['labels2idx' ].iteritems ())
288
- idx2word = dict ((k , v ) for v , k in dic ['words2idx' ].iteritems ())
296
+ idx2label = dict ((k , v ) for v , k in dic ['labels2idx' ].items ())
297
+ idx2word = dict ((k , v ) for v , k in dic ['words2idx' ].items ())
289
298
290
299
train_lex , train_ne , train_y = train_set
291
300
valid_lex , valid_ne , valid_y = valid_set
@@ -323,9 +332,9 @@ def main(param=None):
323
332
324
333
for i , (x , y ) in enumerate (zip (train_lex , train_y )):
325
334
rnn .train (x , y , param ['win' ], param ['clr' ])
326
- print '[learning] epoch %i >> %2.2f%%' % (
327
- e , (i + 1 ) * 100. / nsentences ),
328
- print 'completed in %.2f (sec) <<\r ' % (timeit .default_timer () - tic ),
335
+ print ( '[learning] epoch %i >> %2.2f%%' % (
336
+ e , (i + 1 ) * 100. / nsentences ),)
337
+ print ( 'completed in %.2f (sec) <<\r ' % (timeit .default_timer () - tic ),)
329
338
sys .stdout .flush ()
330
339
331
340
# evaluation // back into the real world : idx -> words
@@ -374,7 +383,7 @@ def main(param=None):
374
383
folder + '/best.valid.txt' ])
375
384
else :
376
385
if param ['verbose' ]:
377
- print ''
386
+ print ( '' )
378
387
379
388
# learning rate decay if no improvement in 10 epochs
380
389
if param ['decay' ] and abs (param ['be' ]- param ['ce' ]) >= 10 :
@@ -384,10 +393,10 @@ def main(param=None):
384
393
if param ['clr' ] < 1e-5 :
385
394
break
386
395
387
- print ('BEST RESULT: epoch' , param ['be' ],
388
- 'valid F1' , param ['vf1' ],
389
- 'best test F1' , param ['tf1' ],
390
- 'with the model' , folder )
396
+ print (( 'BEST RESULT: epoch' , param ['be' ],
397
+ 'valid F1' , param ['vf1' ],
398
+ 'best test F1' , param ['tf1' ],
399
+ 'with the model' , folder ) )
391
400
392
401
393
402
if __name__ == '__main__' :
0 commit comments