def evaluation(model, fname, show_progress=False): model.predictor.forget_history() model.predictor.to_cpu() devset = data_util.load_data(fname) dev_data_resource = data_util.data_spliter(devset, batchsize=1, n_epoch=1) dev_loss = 0 pred = [] target = [] for dev_idx, x_batch, y_batch, epoch, percentage, eos in dev_data_resource: if not dev_idx: break if show_progress: print_stats(percentage, 0, 0, dev_loss) x = Variable(np.asarray(x_batch)) t = Variable(np.asarray(y_batch)) loss_i = model(x, t) target.append(y_batch[0]) pred.append(model.y.data[0].argmax()) dev_loss = (dev_loss * (dev_idx - 1) + loss_i.data) / dev_idx if eos and opts.forget_on_new_utt: model.predictor.forget_history() model.predictor.forget_history() return dev_loss, pred, target
def train_mode(): nnet_model = None if opts.nnet_struct: nnet_model = NNET_Model.parse_structure(opts.nnet_struct) elif opts.fname_in_model: nnet_model = NNET_Model.load(opts.fname_in_model) trainset = data_util.load_data(opts.fname_train) eos_pad = misc.get_pading(opts.eos_pad) train_data_resource = data_util.data_spliter(trainset, batchsize=opts.batchsize, n_epoch=opts.n_epoch, EOS=eos_pad) optimizer, model = setup_training(nnet_model, opts) train_nnet(model, optimizer, train_data_resource, opts) if opts.fname_test: print('====================TESTING=========================') test_loss, pred, target = evaluation(model, opts.fname_test, show_progress=True) if 'cross_entropy' in opts.loss_function: misc.f_measure(pred, target) print(' test loss: %.3f' % test_loss) if opts.fname_out_model: nnet_model.save(opts.fname_out_model)