def train(args):
train_file = args.input<EOL>test_file = args.validation<EOL>ngram_range = args.ngrams<EOL>logging.info('<STR_LIT>', ngram_range)<EOL>logging.info('<STR_LIT>')<EOL>train_labels, train_data = read_input_data(train_file)<EOL>logging.info('<STR_LIT>')<EOL>test_labels, test_data = read_input_data(test_file)<EOL>tokens_list = []<EOL>for line in train_data:<EOL><INDENT>tokens_list.extend(line.split())<EOL><DEDENT>cntr = Counter(tokens_list)<EOL>train_vocab = gluonnlp.Vocab(cntr)<EOL>logging.info('<STR_LIT>', len(train_vocab))<EOL>logging.info('<STR_LIT>')<EOL>embedding_matrix_len = len(train_vocab)<EOL>train_sequences = convert_to_sequences(train_data, train_vocab)<EOL>test_sequences = convert_to_sequences(test_data, train_vocab)<EOL>if ngram_range >= <NUM_LIT:2>:<EOL><INDENT>logging.info('<STR_LIT>', ngram_range)<EOL>ngram_set = set()<EOL>for input_list in train_sequences:<EOL><INDENT>for i in range(<NUM_LIT:2>, ngram_range + <NUM_LIT:1>):<EOL><INDENT>set_of_ngram = create_ngram_set(input_list, ngram_value=i)<EOL>ngram_set.update(set_of_ngram)<EOL><DEDENT><DEDENT>start_index = len(cntr)<EOL>token_indices = {v: k + start_index for k, v in enumerate(ngram_set)}<EOL>embedding_matrix_len = embedding_matrix_len + len(token_indices)<EOL>train_sequences = add_ngram(train_sequences, token_indices,<EOL>ngram_range)<EOL>test_sequences = add_ngram(test_sequences, token_indices, ngram_range)<EOL>logging.info('<STR_LIT>')<EOL><DEDENT>logging.info('<STR_LIT>')<EOL>label_mapping = get_label_mapping(train_labels)<EOL>y_train_final = list(map(lambda x: label_mapping[x], train_labels))<EOL>y_test_final = list(map(lambda x: label_mapping[x], test_labels))<EOL>train_sequences, train_data_lengths = preprocess_dataset(<EOL>train_sequences, y_train_final)<EOL>test_sequences, _ = preprocess_dataset(<EOL>test_sequences, y_test_final)<EOL>train_dataloader, test_dataloader = get_dataloader(train_sequences, train_data_lengths,<EOL>test_sequences, args.batch_size)<EOL>num_classes = len(np.unique(train_labels))<EOL>logging.info('<STR_LIT>', num_classes)<EOL>logging.info('<STR_LIT>')<EOL>ctx = get_context(args)<EOL>logging.info('<STR_LIT>', ctx)<EOL>embedding_dim = args.emsize<EOL>logging.info('<STR_LIT>', embedding_matrix_len)<EOL>net = FastTextClassificationModel(<EOL>embedding_matrix_len, embedding_dim, num_classes)<EOL>net.hybridize()<EOL>net.collect_params().initialize(mx.init.Xavier(), ctx=ctx)<EOL>logging.info('<STR_LIT>')<EOL>softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()<EOL>sigmoid_loss_fn = gluon.loss.SigmoidBinaryCrossEntropyLoss()<EOL>loss_function = softmax_cross_entropy<EOL>if num_classes == <NUM_LIT:2>:<EOL><INDENT>logging.info(<EOL>'<STR_LIT>'<EOL>)<EOL>loss_function = sigmoid_loss_fn<EOL><DEDENT>logging.info('<STR_LIT>', loss_function)<EOL>num_epochs = args.epochs<EOL>batch_size = args.batch_size<EOL>logging.info('<STR_LIT>')<EOL>learning_rate = args.lr<EOL>trainer = gluon.Trainer(net.collect_params(), '<STR_LIT>',<EOL>{'<STR_LIT>': learning_rate})<EOL>num_batches = len(train_data) / batch_size<EOL>display_batch_cadence = int(math.ceil(num_batches / <NUM_LIT:10>))<EOL>logging.info('<STR_LIT>',<EOL>len(train_data), len(test_data))<EOL>logging.info('<STR_LIT>',<EOL>num_batches, display_batch_cadence)<EOL>for epoch in range(num_epochs):<EOL><INDENT>for batch, ((data, length), label) in enumerate(train_dataloader):<EOL><INDENT>data = data.as_in_context(ctx)<EOL>label = label.as_in_context(ctx)<EOL>length = length.astype('<STR_LIT>').as_in_context(ctx)<EOL>with autograd.record():<EOL><INDENT>output = net(data, length)<EOL>loss = loss_function(output, label)<EOL><DEDENT>loss.backward()<EOL>trainer.step(data.shape[<NUM_LIT:0>])<EOL>if batch % display_batch_cadence == <NUM_LIT:0>:<EOL><INDENT>logging.info('<STR_LIT>', epoch, batch)<EOL><DEDENT><DEDENT>logging.info('<STR_LIT>', epoch)<EOL>test_accuracy, test_loss = evaluate_accuracy(<EOL>test_dataloader, net, ctx, loss_function, num_classes)<EOL>logging.info('<STR_LIT>',<EOL>epoch, test_accuracy, test_loss)<EOL>learning_rate = learning_rate * <NUM_LIT:0.5><EOL>trainer.set_learning_rate(learning_rate)<EOL><DEDENT>save_model(net, args.output)<EOL>
Training function that orchestrates the Classification!