Example #1
0
def test_auroc(pos_label):
    auroc = AUROC(pos_label=pos_label)
    assert auroc.name == 'auroc'

    pred, target = torch.tensor([1, 2, 3, 4]), torch.tensor([1, 2, 0, 1])
    area = auroc(pred=pred, target=target, sample_weight=[0.1, 0.2, 0.3, 0.4])
    assert isinstance(area, torch.Tensor)
    def validation_epoch_end(self, outputs):
        avg_loss = torch.stack([x["val_loss"] for x in outputs]).mean()
        y = torch.cat([x["y"] for x in outputs], dim=0).flatten()
        y_hat = torch.cat([x["y_hat"] for x in outputs], dim=0).flatten()

        avg_acc = (y_hat.round() == y).float().mean()

        auc = AUROC()(y_hat,
                      y.float()) if y.float().mean() > 0 else torch.tensor(0.5)
        metrics = {"val_loss": avg_loss, "val_acc": avg_acc, "val_auc": auc}

        self.logger.log_metrics(metrics, step=self.current_epoch)
        return metrics
Example #3
0
 def validation_epoch_end(self, outputs):
     avg_loss = torch.stack([x["val_loss"] for x in outputs]).mean()
     y = torch.cat([x["y"] for x in outputs])
     y_hat = torch.cat([x["y_hat"] for x in outputs])
     auc = (
         AUROC()(pred=y_hat, target=y) if y.float().mean() > 0 else 0.5
     )  # skip sanity check
     acc = (y_hat.round() == y).float().mean().item()
     print(f"Epoch {self.current_epoch} acc:{acc} auc:{auc}")
     tensorboard_logs = {"val_loss": avg_loss, "val_auc": auc, "val_acc": acc}
     return {
         "avg_val_loss": avg_loss,
         "val_auc": auc,
         "val_acc": acc,
         "log": tensorboard_logs,
     }
 def validation_epoch_end(self, outputs):  # 한 에폭이 끝났을 때 실행
     avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
     y = torch.cat([x['y'] for x in outputs])
     y_hat = torch.cat([x['y_hat'] for x in outputs])
     auc = AUROC()(y_hat,
                   y) if y.float().mean() > 0 else 0.5  # skip sanity check
     acc = (y_hat.round() == y).float().mean().item()
     print(f"Epoch {self.current_epoch} acc:{acc} auc:{auc}")
     tensorboard_logs = {
         'val_loss': avg_loss,
         'val_auc': auc,
         'val_acc': acc
     }
     return {
         'avg_val_loss': avg_loss,
         'val_auc': auc,
         'val_acc': acc,
         'log': tensorboard_logs
     }
Example #5
0
    def validation_epoch_end(self, outputs):
        avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
        LOGITS = torch.cat([x['logits'] for x in outputs])
        LABELS = torch.cat([x['labels'] for x in outputs])

        # Skip Sanity Check
        auc = AUROC()(pred=LOGITS.detach(), target=LABELS.detach()) if LABELS.float().mean() > 0 else 0.5
        logs = {'val/epoch_loss': avg_loss.item(), 'val/epoch_auc': auc}
        # Log loss, auc
        self.experiment.log_metrics(logs, step=self.epoch_num)
        # Update Epoch Num
        self.epoch_num += 1

        # Save Weights
        if self.best_loss > avg_loss:
            self.best_loss = avg_loss
            filename = f'{self.cfg.exp.exp_name}_epoch_{self.epoch_num}_loss_{self.best_loss:.3f}_auc_{auc:.3f}.pth'
            torch.save(self.net.state_dict(), filename)
            self.experiment.log_model(name=filename, file_or_folder='./'+filename)
            os.remove(filename)
            self.best_auc = auc

        return {'avg_val_loss': avg_loss}