class DummyRegression(DummyRegressionPlainLightning, InnerEyeInference): def __init__(self, in_features: int = 1, *args, **kwargs) -> None: # type: ignore super().__init__(in_features=in_features, *args, **kwargs) # type: ignore self.l_rate = 1e-1 self.dataset_split = ModelExecutionMode.TRAIN activation = Identity() layers = [ torch.nn.Linear(in_features=in_features, out_features=1, bias=True), activation ] self.model = torch.nn.Sequential(*layers) # type: ignore def forward(self, x: Tensor) -> Tensor: # type: ignore return self.model(x) def training_step(self, batch, *args, **kwargs) -> torch.Tensor: # type: ignore input, target = batch prediction = self.forward(input) loss = torch.nn.functional.mse_loss(prediction, target) self.log("loss", loss, on_epoch=True, on_step=True) return loss def on_inference_start(self) -> None: Path("on_inference_start.txt").touch() self.inference_mse: Dict[ModelExecutionMode, float] = {} def on_inference_epoch_start(self, dataset_split: ModelExecutionMode, is_ensemble_model: bool) -> None: self.dataset_split = dataset_split Path(f"on_inference_start_{self.dataset_split.value}.txt").touch() self.mse = MeanSquaredError() def inference_step(self, item: Tuple[Tensor, Tensor], batch_idx: int, model_output: torch.Tensor) -> None: input, target = item prediction = self.forward(input) self.mse(prediction, target) with Path(f"inference_step_{self.dataset_split.value}.txt").open( mode="a") as f: f.write(f"{prediction.item()},{target.item()}\n") def on_inference_epoch_end(self) -> None: Path(f"on_inference_end_{self.dataset_split.value}.txt").touch() self.inference_mse[self.dataset_split] = self.mse.compute().item() self.mse.reset() def on_inference_end(self) -> None: Path("on_inference_end.txt").touch() df = pd.DataFrame(columns=["Split", "MSE"], data=[[split.value, mse] for split, mse in self.inference_mse.items()]) df.to_csv("metrics_per_split.csv", index=False)
def __init__(self): super().__init__() setattr(self, "layer_0", nn.Linear(16, 64)) setattr(self, "layer_0a", torch.nn.ReLU()) for i in range(1, 3): setattr(self, f"layer_{i}", nn.Linear(64, 64)) setattr(self, f"layer_{i}a", torch.nn.ReLU()) setattr(self, "layer_end", nn.Linear(64, 1)) self.train_mse = MeanSquaredError() self.valid_mse = MeanSquaredError() self.test_mse = MeanSquaredError()
def __init__( self, *, input_dim: int, hidden_dim: int, output_dim: int = 1, num_layers: int = 2, dropout_prob: float = 0, loss_fn: Callable[[Tensor, Tensor], Tensor] = MeanSquaredError(), optimizer: optim.Optimizer = optim.AdamW, learning_rate: float = 1e-3, ): """ Create a LSTM model. Parameters ---------- input_dim number of expected features in the input hidden_dim number of hidden state in each LSTM layer output_dim number of features to be outputted num_layers number of LSTM layers stacking on top of each other dropout_prob dropout probability loss_fn loss function optimizer optimizer learning_rate learning rate for the optimiser """ super().__init__() self.save_hyperparameters() self.input_dim = input_dim self.hidden_dim = hidden_dim self.num_layers = num_layers self.loss_fn = loss_fn self.optimizer = optimizer self.learning_rate = learning_rate # the LSTM layer self.lstm = nn.LSTM( input_size=input_dim, hidden_size=hidden_dim, num_layers=num_layers, dropout=dropout_prob, ) # the output layer self.linear = nn.Linear( in_features=hidden_dim, out_features=output_dim, )
def __init__(self, prefix, loss_type: str, threshold=0.5, top_k=[1, 5, 10], n_classes: int = None, multilabel: bool = None, metrics=["precision", "recall", "top_k", "accuracy"]): super().__init__() self.loss_type = loss_type.upper() self.threshold = threshold self.n_classes = n_classes self.multilabel = multilabel self.top_ks = top_k self.prefix = prefix self.metrics = {} for metric in metrics: if "precision" == metric: self.metrics[metric] = Precision(average=True, is_multilabel=multilabel) elif "recall" == metric: self.metrics[metric] = Recall(average=True, is_multilabel=multilabel) elif "top_k" in metric: if n_classes: top_k = [k for k in top_k if k < n_classes] if multilabel: self.metrics[metric] = TopKMultilabelAccuracy(k_s=top_k) else: self.metrics[metric] = TopKCategoricalAccuracy(k=max(int(np.log(n_classes)), 1), output_transform=None) elif "macro_f1" in metric: self.metrics[metric] = F1(num_classes=n_classes, average="macro", multilabel=multilabel) elif "micro_f1" in metric: self.metrics[metric] = F1(num_classes=n_classes, average="micro", multilabel=multilabel) elif "mse" == metric: self.metrics[metric] = MeanSquaredError() elif "auroc" == metric: self.metrics[metric] = AUROC(num_classes=n_classes) elif "avg_precision" in metric: self.metrics[metric] = AveragePrecision(num_classes=n_classes, ) elif "accuracy" in metric: self.metrics[metric] = Accuracy(top_k=int(metric.split("@")[-1]) if "@" in metric else None) elif "ogbn" in metric: self.metrics[metric] = OGBNodeClfMetrics(NodeEvaluator(metric)) elif "ogbg" in metric: self.metrics[metric] = OGBNodeClfMetrics(GraphEvaluator(metric)) elif "ogbl" in metric: self.metrics[metric] = OGBLinkPredMetrics(LinkEvaluator(metric)) else: print(f"WARNING: metric {metric} doesn't exist") # Needed to add the PytorchGeometric methods as Modules, so they'll be on the correct CUDA device during training if isinstance(self.metrics[metric], torchmetrics.metric.Metric): setattr(self, metric, self.metrics[metric]) self.reset_metrics()
def test_v1_5_metric_regress(): ExplainedVariance.__init__._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): ExplainedVariance() MeanAbsoluteError.__init__._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): MeanAbsoluteError() MeanSquaredError.__init__._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): MeanSquaredError() MeanSquaredLogError.__init__._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): MeanSquaredLogError() target = torch.tensor([3, -0.5, 2, 7]) preds = torch.tensor([2.5, 0.0, 2, 8]) explained_variance._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): res = explained_variance(preds, target) assert torch.allclose(res, torch.tensor(0.9572), atol=1e-4) x = torch.tensor([0., 1, 2, 3]) y = torch.tensor([0., 1, 2, 2]) mean_absolute_error._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): assert mean_absolute_error(x, y) == 0.25 mean_relative_error._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): assert mean_relative_error(x, y) == 0.125 mean_squared_error._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): assert mean_squared_error(x, y) == 0.25 mean_squared_log_error._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): res = mean_squared_log_error(x, y) assert torch.allclose(res, torch.tensor(0.0207), atol=1e-4) PSNR.__init__._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): PSNR() R2Score.__init__._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): R2Score() SSIM.__init__._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): SSIM() preds = torch.tensor([[0.0, 1.0], [2.0, 3.0]]) target = torch.tensor([[3.0, 2.0], [1.0, 0.0]]) psnr._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): res = psnr(preds, target) assert torch.allclose(res, torch.tensor(2.5527), atol=1e-4) target = torch.tensor([3, -0.5, 2, 7]) preds = torch.tensor([2.5, 0.0, 2, 8]) r2score._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): res = r2score(preds, target) assert torch.allclose(res, torch.tensor(0.9486), atol=1e-4) preds = torch.rand([16, 1, 16, 16]) target = preds * 0.75 ssim._warned = False with pytest.deprecated_call(match='It will be removed in v1.5.0'): res = ssim(preds, target) assert torch.allclose(res, torch.tensor(0.9219), atol=1e-4)
def on_inference_epoch_start(self, dataset_split: ModelExecutionMode, is_ensemble_model: bool) -> None: self.dataset_split = dataset_split Path(f"on_inference_start_{self.dataset_split.value}.txt").touch() self.mse = MeanSquaredError()