def train_model(model, train_data_loader, val_data_loader, embedding, ctx, args):
logger.info(vars(args))<EOL>model.hybridize()<EOL>model.collect_params().initialize(mx.init.Normal(<NUM_LIT>), ctx=ctx)<EOL>model.word_emb.weight.set_data(embedding.idx_to_vec)<EOL>model.word_emb.weight.grad_req = '<STR_LIT:null>'<EOL>loss_func = gluon.loss.SoftmaxCrossEntropyLoss()<EOL>trainer = gluon.Trainer(model.collect_params(), '<STR_LIT>',<EOL>{'<STR_LIT>': args.lr,<EOL>'<STR_LIT>': args.weight_decay,<EOL>'<STR_LIT>': <NUM_LIT:5>})<EOL>checkpoints_dir = os.path.join(args.output_dir, '<STR_LIT>')<EOL>if not os.path.exists(checkpoints_dir):<EOL><INDENT>os.makedirs(checkpoints_dir)<EOL><DEDENT>best_val_acc = <NUM_LIT:0.><EOL>for epoch_id in range(args.epochs):<EOL><INDENT>avg_loss = <NUM_LIT:0.><EOL>avg_acc = <NUM_LIT:0.><EOL>for batch_id, example in enumerate(train_data_loader):<EOL><INDENT>s1, s2, label = example<EOL>s1 = s1.as_in_context(ctx)<EOL>s2 = s2.as_in_context(ctx)<EOL>label = label.as_in_context(ctx)<EOL>with autograd.record():<EOL><INDENT>output = model(s1, s2)<EOL>loss = loss_func(output, label).mean()<EOL><DEDENT>loss.backward()<EOL>trainer.step(<NUM_LIT:1>)<EOL>avg_loss += loss.sum().asscalar()<EOL>pred = output.argmax(axis=<NUM_LIT:1>)<EOL>acc = (pred == label.astype(np.float32)).mean()<EOL>avg_acc += acc.asscalar()<EOL>if (batch_id + <NUM_LIT:1>) % args.print_interval == <NUM_LIT:0>:<EOL><INDENT>avg_loss /= args.print_interval<EOL>avg_acc /= args.print_interval<EOL>logger.info('<STR_LIT>'<EOL>.format(epoch_id, batch_id + <NUM_LIT:1>, len(train_data_loader),<EOL>avg_loss, avg_acc))<EOL>avg_loss = <NUM_LIT:0.><EOL>avg_acc = <NUM_LIT:0.><EOL><DEDENT><DEDENT>val_loss, val_acc = test_model(model, val_data_loader, loss_func, ctx)<EOL>if val_acc > best_val_acc:<EOL><INDENT>best_val_acc = val_acc<EOL>checkpoint_path = os.path.join(args.output_dir, '<STR_LIT>', '<STR_LIT>')<EOL>model.save_parameters(checkpoint_path)<EOL><DEDENT>logger.info('<STR_LIT>'<EOL>.format(epoch_id, val_loss, val_acc, best_val_acc))<EOL>checkpoint_path = os.path.join(args.output_dir, '<STR_LIT>', '<STR_LIT>')<EOL>model.save_parameters(checkpoint_path)<EOL><DEDENT>
Train model and validate/save every epoch.