(tparams, options)
| 365 | |
| 366 | |
| 367 | def build_model(tparams, options): |
| 368 | trng = RandomStreams(SEED) |
| 369 | |
| 370 | # Used for dropout. |
| 371 | use_noise = theano.shared(numpy_floatX(0.)) |
| 372 | |
| 373 | x = tensor.matrix('x', dtype='int64') |
| 374 | mask = tensor.matrix('mask', dtype=config.floatX) |
| 375 | y = tensor.vector('y', dtype='int64') |
| 376 | |
| 377 | n_timesteps = x.shape[0] |
| 378 | n_samples = x.shape[1] |
| 379 | |
| 380 | emb = tparams['Wemb'][x.flatten()].reshape([n_timesteps, |
| 381 | n_samples, |
| 382 | options['dim_proj']]) |
| 383 | proj = get_layer(options['encoder'])[1](tparams, emb, options, |
| 384 | prefix=options['encoder'], |
| 385 | mask=mask) |
| 386 | if options['encoder'] == 'lstm': |
| 387 | proj = (proj * mask[:, :, None]).sum(axis=0) |
| 388 | proj = proj / mask.sum(axis=0)[:, None] |
| 389 | if options['use_dropout']: |
| 390 | proj = dropout_layer(proj, use_noise, trng) |
| 391 | |
| 392 | pred = tensor.nnet.softmax(tensor.dot(proj, tparams['U']) + tparams['b']) |
| 393 | |
| 394 | f_pred_prob = theano.function([x, mask], pred, name='f_pred_prob') |
| 395 | f_pred = theano.function([x, mask], pred.argmax(axis=1), name='f_pred') |
| 396 | |
| 397 | off = 1e-8 |
| 398 | if pred.dtype == 'float16': |
| 399 | off = 1e-6 |
| 400 | |
| 401 | cost = -tensor.log(pred[tensor.arange(n_samples), y] + off).mean() |
| 402 | |
| 403 | return use_noise, x, mask, y, f_pred_prob, f_pred, cost |
| 404 | |
| 405 | |
| 406 | def pred_probs(f_pred_prob, prepare_data, data, iterator, verbose=False): |
no test coverage detected