|
2 | 2 | Build a tweet sentiment analyzer |
3 | 3 | ''' |
4 | 4 | from collections import OrderedDict |
5 | | -import copy |
6 | 5 | import cPickle as pkl |
7 | 6 | import random |
8 | 7 | import sys |
@@ -159,8 +158,8 @@ def lstm_layer(tparams, state_below, options, prefix='lstm', mask=None): |
159 | 158 |
|
160 | 159 | def _slice(_x, n, dim): |
161 | 160 | if _x.ndim == 3: |
162 | | - return _x[:, :, n*dim:(n+1)*dim] |
163 | | - return _x[:, n*dim:(n+1)*dim] |
| 161 | + return _x[:, :, n * dim:(n + 1) * dim] |
| 162 | + return _x[:, n * dim:(n + 1) * dim] |
164 | 163 |
|
165 | 164 | def _step(m_, x_, h_, c_): |
166 | 165 | preact = tensor.dot(h_, tparams[_p(prefix, 'U')]) |
@@ -243,7 +242,7 @@ def adadelta(lr, tparams, grads, x, mask, y, cost): |
243 | 242 | rg2up = [(rg2, 0.95 * rg2 + 0.05 * (g ** 2)) |
244 | 243 | for rg2, g in zip(running_grads2, grads)] |
245 | 244 |
|
246 | | - f_grad_shared = theano.function([x, mask, y], cost, updates=zgup+rg2up, |
| 245 | + f_grad_shared = theano.function([x, mask, y], cost, updates=zgup + rg2up, |
247 | 246 | name='adadelta_f_grad_shared') |
248 | 247 |
|
249 | 248 | updir = [-tensor.sqrt(ru2 + 1e-6) / tensor.sqrt(rg2 + 1e-6) * zg |
@@ -254,7 +253,7 @@ def adadelta(lr, tparams, grads, x, mask, y, cost): |
254 | 253 | for ru2, ud in zip(running_up2, updir)] |
255 | 254 | param_up = [(p, p + ud) for p, ud in zip(tparams.values(), updir)] |
256 | 255 |
|
257 | | - f_update = theano.function([lr], [], updates=ru2up+param_up, |
| 256 | + f_update = theano.function([lr], [], updates=ru2up + param_up, |
258 | 257 | on_unused_input='ignore', |
259 | 258 | name='adadelta_f_update') |
260 | 259 |
|
@@ -289,7 +288,7 @@ def rmsprop(lr, tparams, grads, x, mask, y, cost): |
289 | 288 | running_grads2)] |
290 | 289 | param_up = [(p, p + udn[1]) |
291 | 290 | for p, udn in zip(tparams.values(), updir_new)] |
292 | | - f_update = theano.function([lr], [], updates=updir_new+param_up, |
| 291 | + f_update = theano.function([lr], [], updates=updir_new + param_up, |
293 | 292 | on_unused_input='ignore', |
294 | 293 | name='rmsprop_f_update') |
295 | 294 |
|
@@ -321,7 +320,7 @@ def build_model(tparams, options): |
321 | 320 | if options['use_dropout']: |
322 | 321 | proj = dropout_layer(proj, use_noise, trng) |
323 | 322 |
|
324 | | - pred = tensor.nnet.softmax(tensor.dot(proj, tparams['U'])+tparams['b']) |
| 323 | + pred = tensor.nnet.softmax(tensor.dot(proj, tparams['U']) + tparams['b']) |
325 | 324 |
|
326 | 325 | f_pred_prob = theano.function([x, mask], pred, name='f_pred_prob') |
327 | 326 | f_pred = theano.function([x, mask], pred.argmax(axis=1), name='f_pred') |
@@ -408,7 +407,7 @@ def train_lstm( |
408 | 407 | train, valid, test = load_data(n_words=n_words, valid_portion=0.05, |
409 | 408 | maxlen=maxlen) |
410 | 409 |
|
411 | | - ydim = numpy.max(train[1])+1 |
| 410 | + ydim = numpy.max(train[1]) + 1 |
412 | 411 |
|
413 | 412 | model_options['ydim'] = ydim |
414 | 413 |
|
@@ -432,7 +431,7 @@ def train_lstm( |
432 | 431 | if decay_c > 0.: |
433 | 432 | decay_c = theano.shared(numpy.float32(decay_c), name='decay_c') |
434 | 433 | weight_decay = 0. |
435 | | - weight_decay += (tparams['U']**2).sum() |
| 434 | + weight_decay += (tparams['U'] ** 2).sum() |
436 | 435 | weight_decay *= decay_c |
437 | 436 | cost += weight_decay |
438 | 437 |
|
@@ -460,9 +459,9 @@ def train_lstm( |
460 | 459 | bad_count = 0 |
461 | 460 |
|
462 | 461 | if validFreq == -1: |
463 | | - validFreq = len(train[0])/batch_size |
| 462 | + validFreq = len(train[0]) / batch_size |
464 | 463 | if saveFreq == -1: |
465 | | - saveFreq = len(train[0])/batch_size |
| 464 | + saveFreq = len(train[0]) / batch_size |
466 | 465 |
|
467 | 466 | uidx = 0 # the number of update done |
468 | 467 | estop = False # early stop |
@@ -514,7 +513,8 @@ def train_lstm( |
514 | 513 | if numpy.mod(uidx, validFreq) == 0: |
515 | 514 | use_noise.set_value(0.) |
516 | 515 | train_err = pred_error(f_pred, prepare_data, train, kf) |
517 | | - valid_err = pred_error(f_pred, prepare_data, valid, kf_valid) |
| 516 | + valid_err = pred_error(f_pred, prepare_data, valid, |
| 517 | + kf_valid) |
518 | 518 | test_err = pred_error(f_pred, prepare_data, test, kf_test) |
519 | 519 |
|
520 | 520 | history_errs.append([valid_err, test_err]) |
|