def train(args):
if not args.model.lower() in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>logging.error('<STR_LIT>', args.model)<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>if args.data.lower() == '<STR_LIT>':<EOL><INDENT>data = mx.gluon.data.SimpleDataset(nlp.data.Text8(segment='<STR_LIT:train>')[:<NUM_LIT:2>])<EOL>data, vocab, idx_to_counts = preprocess_dataset(<EOL>data, max_vocab_size=args.max_vocab_size)<EOL><DEDENT>elif args.data.lower() == '<STR_LIT>':<EOL><INDENT>data = nlp.data.Text8(segment='<STR_LIT:train>')<EOL>data, vocab, idx_to_counts = preprocess_dataset(<EOL>data, max_vocab_size=args.max_vocab_size)<EOL><DEDENT>elif args.data.lower() == '<STR_LIT>':<EOL><INDENT>data = nlp.data.Fil9(max_sentence_length=<NUM_LIT>)<EOL>data, vocab, idx_to_counts = preprocess_dataset(<EOL>data, max_vocab_size=args.max_vocab_size)<EOL><DEDENT>elif args.data.lower() == '<STR_LIT>':<EOL><INDENT>data, vocab, idx_to_counts = wiki(args.wiki_root, args.wiki_date,<EOL>args.wiki_language,<EOL>args.max_vocab_size)<EOL><DEDENT>if args.ngram_buckets > <NUM_LIT:0>:<EOL><INDENT>data, batchify_fn, subword_function = transform_data_fasttext(<EOL>data, vocab, idx_to_counts, cbow=args.model.lower() == '<STR_LIT>',<EOL>ngram_buckets=args.ngram_buckets, ngrams=args.ngrams,<EOL>batch_size=args.batch_size, window_size=args.window,<EOL>frequent_token_subsampling=args.frequent_token_subsampling)<EOL><DEDENT>else:<EOL><INDENT>subword_function = None<EOL>data, batchify_fn = transform_data_word2vec(<EOL>data, vocab, idx_to_counts, cbow=args.model.lower() == '<STR_LIT>',<EOL>batch_size=args.batch_size, window_size=args.window,<EOL>frequent_token_subsampling=args.frequent_token_subsampling)<EOL><DEDENT>num_tokens = float(sum(idx_to_counts))<EOL>model = CBOW if args.model.lower() == '<STR_LIT>' else SG<EOL>embedding = model(token_to_idx=vocab.token_to_idx, output_dim=args.emsize,<EOL>batch_size=args.batch_size, num_negatives=args.negative,<EOL>negatives_weights=mx.nd.array(idx_to_counts),<EOL>subword_function=subword_function)<EOL>context = get_context(args)<EOL>embedding.initialize(ctx=context)<EOL>if not args.no_hybridize:<EOL><INDENT>embedding.hybridize(static_alloc=True, static_shape=True)<EOL><DEDENT>optimizer_kwargs = dict(learning_rate=args.lr)<EOL>try:<EOL><INDENT>trainer = mx.gluon.Trainer(embedding.collect_params(), args.optimizer,<EOL>optimizer_kwargs)<EOL><DEDENT>except ValueError as e:<EOL><INDENT>if args.optimizer == '<STR_LIT>':<EOL><INDENT>logging.warning('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>trainer = mx.gluon.Trainer(embedding.collect_params(), '<STR_LIT>',<EOL>optimizer_kwargs)<EOL><DEDENT>else:<EOL><INDENT>raise e<EOL><DEDENT><DEDENT>try:<EOL><INDENT>if args.no_prefetch_batch:<EOL><INDENT>data = data.transform(batchify_fn)<EOL><DEDENT>else:<EOL><INDENT>from executors import LazyThreadPoolExecutor<EOL>num_cpu = len(os.sched_getaffinity(<NUM_LIT:0>))<EOL>ex = LazyThreadPoolExecutor(num_cpu)<EOL><DEDENT><DEDENT>except (ImportError, SyntaxError, AttributeError):<EOL><INDENT>logging.warning(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>data = data.transform(batchify_fn)<EOL><DEDENT>num_update = <NUM_LIT:0><EOL>prefetched_iters = []<EOL>for _ in range(min(args.num_prefetch_epoch, args.epochs)):<EOL><INDENT>prefetched_iters.append(iter(data))<EOL><DEDENT>for epoch in range(args.epochs):<EOL><INDENT>if epoch + len(prefetched_iters) < args.epochs:<EOL><INDENT>prefetched_iters.append(iter(data))<EOL><DEDENT>data_iter = prefetched_iters.pop(<NUM_LIT:0>)<EOL>try:<EOL><INDENT>batches = ex.map(batchify_fn, data_iter)<EOL><DEDENT>except NameError:  <EOL><INDENT>batches = data_iter<EOL><DEDENT>log_wc = <NUM_LIT:0><EOL>log_start_time = time.time()<EOL>log_avg_loss = <NUM_LIT:0><EOL>for i, batch in enumerate(batches):<EOL><INDENT>ctx = context[i % len(context)]<EOL>batch = [array.as_in_context(ctx) for array in batch]<EOL>with mx.autograd.record():<EOL><INDENT>loss = embedding(*batch)<EOL><DEDENT>loss.backward()<EOL>num_update += loss.shape[<NUM_LIT:0>]<EOL>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>num_batches = num_tokens // args.batch_size<EOL>if args.model.lower() == '<STR_LIT>':<EOL><INDENT>num_batches = (num_tokens * args.window * <NUM_LIT:2>) // args.batch_size<EOL><DEDENT>else:<EOL><INDENT>num_batches = num_tokens // args.batch_size<EOL><DEDENT>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_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, embedding, vocab, num_update)<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, embedding, vocab, num_update,<EOL>eval_analogy=not args.no_eval_analogy)<EOL><DEDENT>with print_time('<STR_LIT>'):<EOL><INDENT>embedding.save_parameters(os.path.join(args.logdir, '<STR_LIT>'))<EOL><DEDENT>
Training helper.