Esempio n. 1
0
def train():
    EMBEDDING_DIM = 100
    HIDDEN_DIM = 50
    EPOCH = 100
    BATCH_SIZE = 10
    text_field = data.Field(lower=True)
    label_field = data.Field(sequential=False)
    train_iter, dev_iter, test_iter = classification_datasets.load_mr(
        text_field, label_field, batch_size=BATCH_SIZE)

    # text_field.vocab.load_vectors('glove.6B.50d')
    text_field.vocab.load_vectors('glove.6B.100d')

    best_dev_acc = 0.0

    model = LSTMClassifier(embedding_dim=EMBEDDING_DIM,
                           hidden_dim=HIDDEN_DIM,
                           vocab_size=len(text_field.vocab),
                           label_size=len(label_field.vocab) - 1,
                           batch_size=BATCH_SIZE)
    model.word_embeddings.weight.data = text_field.vocab.vectors
    loss_function = nn.NLLLoss()
    optimizer = optim.Adam(model.parameters(), lr=1e-3)

    with missinglink_project.create_experiment(
            model,
            display_name='LSTM Sentence Classifier PyTorch',
            optimizer=optimizer,
            metrics={
                'Loss': loss_function,
                'Accuracy': get_accuracy,
            }) as experiment:
        wrapped_loss_function = experiment.metrics['Loss']
        global wrapped_accuracy_function
        wrapped_accuracy_function = experiment.metrics['Accuracy']

        no_up = 0
        for i in experiment.epoch_loop(
                condition=lambda epoch: epoch < EPOCH and no_up < 10):
            print('epoch: %d start!' % i)
            train_epoch(experiment, model, train_iter, wrapped_loss_function,
                        optimizer, text_field, label_field, i)
            print('now best dev acc:', best_dev_acc)
            dev_acc = evaluate(experiment, model, dev_iter,
                               wrapped_loss_function, 'dev')
            test_acc = evaluate(experiment, model, test_iter,
                                wrapped_loss_function, 'test')
            if dev_acc > best_dev_acc:
                best_dev_acc = dev_acc
                os.system('rm best_models/mr_best_model_minibatch_acc_*.model')
                print('New Best Dev!!!')
                torch.save(
                    model.state_dict(),
                    'best_models/mr_best_model_minibatch_acc_' +
                    str(int(test_acc * 10000)) + '.model')
                no_up = 0
            else:
                no_up += 1
Esempio n. 2
0
def train():
    EMBEDDING_DIM = 100
    HIDDEN_DIM = 50
    EPOCH = 100
    BATCH_SIZE = 10
    text_field = data.Field(lower=True)
    label_field = data.Field(sequential=False)
    train_iter, dev_iter, test_iter = classification_datasets.load_mr(
        text_field, label_field, batch_size=BATCH_SIZE)

    text_field.vocab.load_vectors(wv_type='glove.6B', wv_dim=100)

    best_dev_acc = 0.0

    model = LSTMClassifier(embedding_dim=EMBEDDING_DIM,
                           hidden_dim=HIDDEN_DIM,
                           vocab_size=len(text_field.vocab),
                           label_size=len(label_field.vocab) - 1,
                           batch_size=BATCH_SIZE)
    model.word_embeddings.weight.data = text_field.vocab.vectors
    loss_function = nn.NLLLoss()
    optimizer = optim.Adam(model.parameters(), lr=1e-3)

    no_up = 0
    for i in range(EPOCH):
        print('epoch: %d start!' % i)
        train_epoch(model, train_iter, loss_function, optimizer, text_field,
                    label_field, i)
        print('now best dev acc:', best_dev_acc)
        dev_acc = evaluate(model, dev_iter, loss_function, 'dev')
        test_acc = evaluate(model, test_iter, loss_function, 'test')
        if dev_acc > best_dev_acc:
            best_dev_acc = dev_acc
            os.system('rm best_models/mr_best_model_minibatch_acc_*.model')
            print('New Best Dev!!!')
            torch.save(
                model.state_dict(),
                'best_models/mr_best_model_minibatch_acc_' +
                str(int(test_acc * 10000)) + '.model')
            no_up = 0
        else:
            no_up += 1
            if no_up >= 10:
                exit()
Esempio n. 3
0
parser.add_argument('-bi-gru', action='store_true', default=False)
parser.add_argument('-bnlstm', action='store_true', default=False)

parser.add_argument('-train-cnn', action='store_true', default=False)
args = parser.parse_args()

# load dataset
print("\nLoading data...")
text_field = data.Field(lower=True)
label_field = data.Field(sequential=False)
text_nonstatic_field = data.Field(lower=True)
label_nonstatic_field = data.Field(sequential=False)
if args.class_five:
    train_iter, dev_iter, test_iter = data_five.load_mr(text_field, label_field, batch_size=args.batch_size)
else:
    train_iter, dev_iter, test_iter = classification_datasets.load_mr(text_field, label_field, batch_size=args.batch_size)
    # train_iter, dev_iter, test_iter = classification_datasets_nonstatic.load_mr(text_field, label_field, text_nonstatic_field, label_nonstatic_field, batch_size=args.batch_size)
    # train_iter_char, dev_iter_char, test_iter_char = datasets_char.load_mr(text_field, label_field, batch_size=args.batch_size)
    # print(type(train_iter_char))
    # print(train_iter_char)

# dict_vocab = text_field.vocab.freqs
count_words_reset = text_field.vocab.itos
# count_train_words = text_nonstatic_field.vocab.itos
# print(len(count_train_words))                         # 14830
# print(count_words_reset)
# text_field_words = set(count_words_reset)
# print(len(text_field_words))                          # 17575
# print(text_field_words)
# wv_cat = loader.vector_loader(text_field_words)
# wv_cat = loader.vector_loader_zero(text_field_words)
Esempio n. 4
0
parser.add_argument('-bilstm', action='store_true', default=True)

parser.add_argument('-train-cnn', action='store_true', default=False)
parser.add_argument('-use-cuda', action='store_true', default=False)
args = parser.parse_args()

# load dataset
print("\nLoading data...")
text_field = data.Field(lower=True)
label_field = data.Field(sequential=False)

if args.class_five:
    train_iter, dev_iter, test_iter = data_five.load_mr(
        text_field, label_field, batch_size=args.batch_size)
else:
    train_iter, dev_iter, test_iter = classification_datasets.load_mr(
        text_field, label_field, batch_size=args.batch_size)

# args.word_dict = text_field.vocab.freqs
# print(args.word_dict)
args.word_list = text_field.vocab.itos
# print(args.word_list)
word_size = len(args.word_list)
args.word_dict = dict(zip(args.word_list, list(range(0, word_size))))
# print(args.word_dict)
args.padID = args.word_dict['<pad>']
# print(padID)

# wv_cat = loader.vector_loader(count_words_reset)
# pretrained_weight = wv_cat
# args.pretrained_weight = pretrained_weight