Пример #1
0
def training(args, train_loader, valid_loader, model, device, split):
    """Training Procedure"""
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    criterion = torch.nn.BCELoss()
    metric = Recall()
    total_iter = 0
    best_valid_f1 = 0

    for epoch in range(args.epochs):
        train_trange = tqdm(enumerate(train_loader),
                            total=len(train_loader),
                            desc='training')
        train_loss = 0
        for i, batch in train_trange:
            answer = batch['label'].to(device)
            prob = run_iter(batch, model, device, training=True)
            loss = criterion(prob, answer)
            train_loss += loss.item()
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            total_iter += 1
            metric.update(prob, answer)
            train_trange.set_postfix(loss=train_loss / (i + 1),
                                     **{metric.name: metric.print_score()})

            if total_iter % args.eval_steps == 0:
                valid_f1 = validation(valid_loader, model, device)
                if valid_f1 > best_valid_f1:
                    best_valid_f1 = valid_f1
                    torch.save(
                        model,
                        os.path.join(
                            args.model_dir,
                            'fine_tuned_roberta_{}.pkl'.format(split)))
Пример #2
0
def validation(dataloader, model, device):
    metric = Recall()
    criterion = torch.nn.BCELoss()
    valid_trange = tqdm(enumerate(dataloader),
                        total=len(dataloader),
                        desc='validation')
    model.eval()
    valid_loss = 0
    for i, batch in valid_trange:
        prob = run_iter(batch, model, device, training=False)
        answer = batch['label'].to(device)
        loss = criterion(prob, answer)
        valid_loss += loss.item()
        metric.update(prob, answer)
        valid_trange.set_postfix(loss=valid_loss / (i + 1),
                                 **{metric.name: metric.print_score()})
    return metric.get_f1()