def train():
trainer = gluon.Trainer(model.collect_params(), args.optimizer, {'<STR_LIT>': args.lr})<EOL>train_data_loader, val_data_loader, test_data_loader= dataprocessor.make_dataloader(data_train, data_val, data_test, args)<EOL>best_valid_bleu = <NUM_LIT:0.0><EOL>for epoch_id in range(args.epochs):<EOL><INDENT>log_avg_loss = <NUM_LIT:0><EOL>log_avg_gnorm = <NUM_LIT:0><EOL>log_wc = <NUM_LIT:0><EOL>log_start_time = time.time()<EOL>for batch_id, (src_seq, tgt_seq, src_valid_length, tgt_valid_length)in enumerate(train_data_loader):<EOL><INDENT>src_seq = src_seq.as_in_context(ctx)<EOL>tgt_seq = tgt_seq.as_in_context(ctx)<EOL>src_valid_length = src_valid_length.as_in_context(ctx)<EOL>tgt_valid_length = tgt_valid_length.as_in_context(ctx)<EOL>with mx.autograd.record():<EOL><INDENT>out, _ = model(src_seq, tgt_seq[:, :-<NUM_LIT:1>], src_valid_length, tgt_valid_length - <NUM_LIT:1>)<EOL>loss = loss_function(out, tgt_seq[:, <NUM_LIT:1>:], tgt_valid_length - <NUM_LIT:1>).mean()<EOL>loss = loss * (tgt_seq.shape[<NUM_LIT:1>] - <NUM_LIT:1>) / (tgt_valid_length - <NUM_LIT:1>).mean()<EOL>loss.backward()<EOL><DEDENT>grads = [p.grad(ctx) for p in model.collect_params().values()]<EOL>gnorm = gluon.utils.clip_global_norm(grads, args.clip)<EOL>trainer.step(<NUM_LIT:1>)<EOL>src_wc = src_valid_length.sum().asscalar()<EOL>tgt_wc = (tgt_valid_length - <NUM_LIT:1>).sum().asscalar()<EOL>step_loss = loss.asscalar()<EOL>log_avg_loss += step_loss<EOL>log_avg_gnorm += gnorm<EOL>log_wc += src_wc + tgt_wc<EOL>if (batch_id + <NUM_LIT:1>) % args.log_interval == <NUM_LIT:0>:<EOL><INDENT>wps = log_wc / (time.time() - log_start_time)<EOL>logging.info('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>.format(epoch_id, batch_id + <NUM_LIT:1>, len(train_data_loader),<EOL>log_avg_loss / args.log_interval,<EOL>np.exp(log_avg_loss / args.log_interval),<EOL>log_avg_gnorm / args.log_interval,<EOL>wps / <NUM_LIT:1000>, log_wc / <NUM_LIT:1000>))<EOL>log_start_time = time.time()<EOL>log_avg_loss = <NUM_LIT:0><EOL>log_avg_gnorm = <NUM_LIT:0><EOL>log_wc = <NUM_LIT:0><EOL><DEDENT><DEDENT>valid_loss, valid_translation_out = evaluate(val_data_loader)<EOL>valid_bleu_score, _, _, _, _ = compute_bleu([val_tgt_sentences], valid_translation_out)<EOL>logging.info('<STR_LIT>'<EOL>.format(epoch_id, valid_loss, np.exp(valid_loss), valid_bleu_score * <NUM_LIT:100>))<EOL>test_loss, test_translation_out = evaluate(test_data_loader)<EOL>test_bleu_score, _, _, _, _ = compute_bleu([test_tgt_sentences], test_translation_out)<EOL>logging.info('<STR_LIT>'<EOL>.format(epoch_id, test_loss, np.exp(test_loss), test_bleu_score * <NUM_LIT:100>))<EOL>dataprocessor.write_sentences(valid_translation_out,<EOL>os.path.join(args.save_dir,<EOL>'<STR_LIT>').format(epoch_id))<EOL>dataprocessor.write_sentences(test_translation_out,<EOL>os.path.join(args.save_dir,<EOL>'<STR_LIT>').format(epoch_id))<EOL>if valid_bleu_score > best_valid_bleu:<EOL><INDENT>best_valid_bleu = valid_bleu_score<EOL>save_path = os.path.join(args.save_dir, '<STR_LIT>')<EOL>logging.info('<STR_LIT>'.format(save_path))<EOL>model.save_parameters(save_path)<EOL><DEDENT>if epoch_id + <NUM_LIT:1> >= (args.epochs * <NUM_LIT:2>) // <NUM_LIT:3>:<EOL><INDENT>new_lr = trainer.learning_rate * args.lr_update_factor<EOL>logging.info('<STR_LIT>'.format(new_lr))<EOL>trainer.set_learning_rate(new_lr)<EOL><DEDENT><DEDENT>if os.path.exists(os.path.join(args.save_dir, '<STR_LIT>')):<EOL><INDENT>model.load_parameters(os.path.join(args.save_dir, '<STR_LIT>'))<EOL><DEDENT>valid_loss, valid_translation_out = evaluate(val_data_loader)<EOL>valid_bleu_score, _, _, _, _ = compute_bleu([val_tgt_sentences], valid_translation_out)<EOL>logging.info('<STR_LIT>'<EOL>.format(valid_loss, np.exp(valid_loss), valid_bleu_score * <NUM_LIT:100>))<EOL>test_loss, test_translation_out = evaluate(test_data_loader)<EOL>test_bleu_score, _, _, _, _ = compute_bleu([test_tgt_sentences], test_translation_out)<EOL>logging.info('<STR_LIT>'<EOL>.format(test_loss, np.exp(test_loss), test_bleu_score * <NUM_LIT:100>))<EOL>dataprocessor.write_sentences(valid_translation_out,<EOL>os.path.join(args.save_dir, '<STR_LIT>'))<EOL>dataprocessor.write_sentences(test_translation_out,<EOL>os.path.join(args.save_dir, '<STR_LIT>'))<EOL>
Training function.