def train():
start_pipeline_time = time.time()<EOL>best_valid_acc = <NUM_LIT:0><EOL>stop_early = <NUM_LIT:0><EOL>for epoch in range(args.epochs):<EOL><INDENT>start_epoch_time = time.time()<EOL>epoch_L = <NUM_LIT:0.0><EOL>epoch_sent_num = <NUM_LIT:0><EOL>epoch_wc = <NUM_LIT:0><EOL>start_log_interval_time = time.time()<EOL>log_interval_wc = <NUM_LIT:0><EOL>log_interval_sent_num = <NUM_LIT:0><EOL>log_interval_L = <NUM_LIT:0.0><EOL>for i, ((data, valid_length), label) in enumerate(train_dataloader):<EOL><INDENT>data = mx.nd.transpose(data.as_in_context(context))<EOL>label = label.as_in_context(context)<EOL>valid_length = valid_length.as_in_context(context).astype(np.float32)<EOL>wc = valid_length.sum().asscalar()<EOL>log_interval_wc += wc<EOL>epoch_wc += wc<EOL>log_interval_sent_num += data.shape[<NUM_LIT:1>]<EOL>epoch_sent_num += data.shape[<NUM_LIT:1>]<EOL>with autograd.record():<EOL><INDENT>output = net(data, valid_length)<EOL>L = loss(output, label).mean()<EOL><DEDENT>L.backward()<EOL>if args.clip is not None:<EOL><INDENT>grads = [p.grad(context) for p in net.collect_params().values()]<EOL>gluon.utils.clip_global_norm(grads, args.clip)<EOL><DEDENT>trainer.step(<NUM_LIT:1>)<EOL>log_interval_L += L.asscalar()<EOL>epoch_L += L.asscalar()<EOL>if (i + <NUM_LIT:1>) % args.log_interval == <NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT>' % (<EOL>epoch, i + <NUM_LIT:1>, len(train_dataloader),<EOL>log_interval_L / log_interval_sent_num,<EOL>log_interval_wc / <NUM_LIT:1000> / (time.time() - start_log_interval_time)))<EOL>start_log_interval_time = time.time()<EOL>log_interval_wc = <NUM_LIT:0><EOL>log_interval_sent_num = <NUM_LIT:0><EOL>log_interval_L = <NUM_LIT:0><EOL><DEDENT><DEDENT>end_epoch_time = time.time()<EOL>valid_avg_L, valid_acc = evaluate(valid_dataloader)<EOL>test_avg_L, test_acc = evaluate(test_dataloader)<EOL>print('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % (<EOL>epoch, epoch_L / epoch_sent_num,<EOL>valid_acc, valid_avg_L, test_acc, test_avg_L,<EOL>epoch_wc / <NUM_LIT:1000> / (end_epoch_time - start_epoch_time)))<EOL>if valid_acc < best_valid_acc:<EOL><INDENT>print('<STR_LIT>')<EOL>stop_early += <NUM_LIT:1><EOL>if stop_early == <NUM_LIT:3>:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>print('<STR_LIT>')<EOL>stop_early = <NUM_LIT:0><EOL>net.save_parameters(args.save_prefix + '<STR_LIT>'.format(epoch))<EOL>best_valid_acc = valid_acc<EOL><DEDENT><DEDENT>net.load_parameters(glob.glob(args.save_prefix+'<STR_LIT>')[-<NUM_LIT:1>], context)<EOL>valid_avg_L, valid_acc = evaluate(valid_dataloader)<EOL>test_avg_L, test_acc = evaluate(test_dataloader)<EOL>print('<STR_LIT>'%(valid_avg_L, valid_acc))<EOL>print('<STR_LIT>'%(test_avg_L, test_acc))<EOL>print('<STR_LIT>'%(time.time()-start_pipeline_time))<EOL>
Training process