Example #1
0
def validate(args, epoch, model, loader, criterion, logger):
    steps = len(loader)
    local_loss = AverageMeter()
    local_acc = AverageMeter()
    local_recall = AverageMeter()
    aver_loss = AverageMeter()
    aver_acc = AverageMeter()
    aver_recall = AverageMeter()

    model.eval()
    if args.verbose:
        logger.info("Validating")

    with torch.no_grad():
        for i, (images, targets) in enumerate(loader, start=1):
            if args.gpu is not None:
                images = images.cuda(args.gpu, non_blocking=True)
                targets = targets.cuda(args.gpu, non_blocking=True)

            outputs = model(images)
            if args.multi:
                outputs = torch.sigmoid(outputs)
            loss = criterion(outputs, targets)

            if args.multi:
                precision, recall = calculate_metrics(
                    outputs.detach().cpu().numpy(),
                    targets.detach().cpu().numpy(), args.threshold)
            else:
                precision = accuracy(outputs, targets)[0].item()
                recall = precision

            local_loss.update(loss.item(), images.size(0))
            local_acc.update(precision, images.size(0))
            local_recall.update(recall, images.size(0))

            if args.distributed:
                running_metrics = torch.FloatTensor(
                    [loss.item(), precision, recall]).cuda(args.gpu)
                running_metrics /= args.world_size
                dist.all_reduce(running_metrics, op=dist.ReduceOp.SUM)
                aver_loss.update(running_metrics[0].item())
                aver_acc.update(running_metrics[1].item())
                aver_recall.update(running_metrics[2].item())
            else:
                aver_loss.update(loss.item(), images.size(0))
                aver_acc.update(precision, images.size(0))
                aver_recall.update(recall, images.size(0))

            if args.verbose and i % args.log_interval == 0:
                logger.info("Epoch: [{}] [{}]/[{}]({:.2%}) "
                            "Loss: {:.4f} / {:.4f} / {:.4f} "
                            "Acc: {:.2f} / {:.2f} / {:.2f} "
                            "Recall: {:.2f} / {:.2f} / {:.2f}".format(
                                epoch, i, steps, i / steps, loss,
                                local_loss.avg, aver_loss.avg, precision,
                                local_acc.avg, aver_acc.avg, recall,
                                local_recall.avg, aver_recall.avg))

    return aver_loss.avg, aver_acc.avg, aver_recall.avg
from sklearn.naive_bayes import GaussianNB

from helper import get_data, calculate_metrics

name = 'Naive Bayes'

if __name__ == '__main__':
    x_train, x_test, y_train, y_test = get_data()

    classifier = GaussianNB()
    classifier.fit(x_train, y_train)

    y_predicted = classifier.predict(x_test)

    calculate_metrics(y_predicted, y_test, name)