示例#1
0
print('\n> Starting 10-fold CV.')
for k, (train_loader, test_loader) in enumerate(dataset.split_cross_val(10)):
    # initialize model and optimizer every fold
    model = BaseModel(lstm_units,
                      dataset.max_sentence_length, args.num_sentiments,
                      initial_word_embedding.clone(), args.use_gaze)
    optimizer = SGD(model.parameters(),
                    lr=INITIAL_LR,
                    momentum=0.95,
                    nesterov=True)

    # optimizer_scheduler = lr_scheduler.StepLR(
    #     optimizer, step_size=halve_lr_every_passes, gamma=0.5)
    if USE_CUDA:
        model = model.cuda()

    for e in range(NUM_EPOCHS):
        train_loss, train_results = iterate(train_loader)

    # save the training metrics of last epoch
    for metric, value in train_results.items():
        train_metrics[metric].append(value)

    test_loss, test_results = iterate(test_loader, train=False)
    for metric, value in test_results.items():
        test_metrics[metric].append(value)

    print('\nFold', k, end='')
    print_metrics(train_results, 'TRAIN')
    print_metrics(test_results, 'TEST')
示例#2
0
def build_model(args, pretrained_embedding, v_dim, a_dim, l_dim):
    from model import BaseModel
    model = BaseModel(args, pretrained_embedding, v_dim, a_dim, l_dim)
    return model.cuda()