def train(args):
vocab, row, col, counts = get_train_data(args)<EOL>model = GloVe(token_to_idx=vocab.token_to_idx, output_dim=args.emsize,<EOL>dropout=args.dropout, x_max=args.x_max, alpha=args.alpha,<EOL>weight_initializer=mx.init.Uniform(scale=<NUM_LIT:1> / args.emsize))<EOL>context = get_context(args)<EOL>model.initialize(ctx=context)<EOL>if not args.no_hybridize:<EOL><INDENT>model.hybridize(static_alloc=not args.no_static_alloc)<EOL><DEDENT>optimizer_kwargs = dict(learning_rate=args.lr, eps=args.adagrad_eps)<EOL>params = list(model.collect_params().values())<EOL>try:<EOL><INDENT>trainer = mx.gluon.Trainer(params, '<STR_LIT>', optimizer_kwargs)<EOL><DEDENT>except ValueError:<EOL><INDENT>logging.warning('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>trainer = mx.gluon.Trainer(params, '<STR_LIT>', optimizer_kwargs)<EOL><DEDENT>index_dtype = '<STR_LIT>'<EOL>if counts.shape[<NUM_LIT:0>] >= np.iinfo(np.int32).max:<EOL><INDENT>index_dtype = '<STR_LIT>'<EOL>logging.info('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>indices = mx.nd.arange(counts.shape[<NUM_LIT:0>], dtype=index_dtype)<EOL>for epoch in range(args.epochs):<EOL><INDENT>log_wc = <NUM_LIT:0><EOL>log_start_time = time.time()<EOL>log_avg_loss = <NUM_LIT:0><EOL>mx.nd.shuffle(indices, indices)  <EOL>bs = args.batch_size<EOL>num_batches = indices.shape[<NUM_LIT:0>] // bs<EOL>for i in range(num_batches):<EOL><INDENT>batch_indices = indices[bs * i:bs * (i + <NUM_LIT:1>)]<EOL>ctx = context[i % len(context)]<EOL>batch_row = row[batch_indices].as_in_context(ctx)<EOL>batch_col = col[batch_indices].as_in_context(ctx)<EOL>batch_counts = counts[batch_indices].as_in_context(ctx)<EOL>with mx.autograd.record():<EOL><INDENT>loss = model(batch_row, batch_col, batch_counts)<EOL>loss.backward()<EOL><DEDENT>if len(context) == <NUM_LIT:1> or (i + <NUM_LIT:1>) % len(context) == <NUM_LIT:0>:<EOL><INDENT>trainer.step(batch_size=<NUM_LIT:1>)<EOL><DEDENT>log_wc += loss.shape[<NUM_LIT:0>]<EOL>log_avg_loss += loss.mean().as_in_context(context[<NUM_LIT:0>])<EOL>if (i + <NUM_LIT:1>) % args.log_interval == <NUM_LIT:0>:<EOL><INDENT>log_avg_loss = log_avg_loss.asscalar() / args.log_interval<EOL>wps = log_wc / (time.time() - log_start_time)<EOL>logging.info('<STR_LIT>'<EOL>'<STR_LIT>'.format(<EOL>epoch, i + <NUM_LIT:1>, num_batches, log_avg_loss,<EOL>wps / <NUM_LIT:1000>, log_wc / <NUM_LIT:1000>))<EOL>log_dict = dict(<EOL>global_step=epoch * len(indices) + i * args.batch_size,<EOL>epoch=epoch, batch=i + <NUM_LIT:1>, loss=log_avg_loss,<EOL>wps=wps / <NUM_LIT:1000>)<EOL>log(args, log_dict)<EOL>log_start_time = time.time()<EOL>log_avg_loss = <NUM_LIT:0><EOL>log_wc = <NUM_LIT:0><EOL><DEDENT>if args.eval_interval and (i + <NUM_LIT:1>) % args.eval_interval == <NUM_LIT:0>:<EOL><INDENT>with print_time('<STR_LIT>'):<EOL><INDENT>mx.nd.waitall()<EOL><DEDENT>with print_time('<STR_LIT>'):<EOL><INDENT>evaluate(args, model, vocab, i + num_batches * epoch)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>with print_time('<STR_LIT>'):<EOL><INDENT>mx.nd.waitall()<EOL><DEDENT>with print_time('<STR_LIT>'):<EOL><INDENT>evaluate(args, model, vocab, num_batches * args.epochs,<EOL>eval_analogy=not args.no_eval_analogy)<EOL><DEDENT>with print_time('<STR_LIT>'):<EOL><INDENT>model.save_parameters(os.path.join(args.logdir, '<STR_LIT>'))<EOL><DEDENT>
Training helper.