Example #1
0
    train_data = batchify(corpus.train, batch_size)
    val_data = batchify(corpus.valid, eval_batch_size)
    test_data = batchify(corpus.test, eval_batch_size)
    '''

    ###############################################################################
    # Build the model
    ###############################################################################

    #ntokens = len(corpus.dictionary)
    ntokens = len(label_field.vocab.itos)
    if model_type == 'LangModel':
        model = Model.LangModel(rnn_model, ntokens, emsize, nhid, nlayers,
                                cuda, device)
    elif model_type == 'BiLangModel':
        model = Model.BiLangModel(rnn_model, ntokens, emsize, nhid, nlayers,
                                  dropout, cuda, device)
    if cuda:
        model.cuda(device)

    print(model)

    if param_init != 0.0:
        model.init_weights(param_init)
    model.init_encoder(text_field.vocab.vectors)

    print('voc size:', len(text_field.vocab.itos))
    #criterion = nn.CrossEntropyLoss()
    criterion = utils.LanguageModelCriterion()

    ###############################################################################
    # Training code