Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add differentiability testing to more metrics [3/n] #225

Merged
merged 4 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added Specificity metric ([#210](https://github.com/PyTorchLightning/metrics/pull/210))


- Added `is_differentiable` property to `AUC`, `AUROC`, `CohenKappa` and `AveragePrecision` ([#178](https://github.com/PyTorchLightning/metrics/pull/178))

- Added `is_differentiable` property:
* To `AUC`, `AUROC`, `CohenKappa` and `AveragePrecision` ([#178](https://github.com/PyTorchLightning/metrics/pull/178))
* To `PearsonCorrCoef`, `SpearmanCorrcoef`, `R2Score` and `ExplainedVariance` ([]())
SkafteNicki marked this conversation as resolved.
Show resolved Hide resolved

- Added `add_metrics` method to `MetricCollection` for adding additional metrics after initialization ([#221](https://github.com/PyTorchLightning/metrics/pull/221))


- Added pre-gather reduction in the case of `dist_reduce_fx="cat"` to reduce communication cost ([#217](https://github.com/PyTorchLightning/metrics/pull/217))



### Changed

- `MetricCollection` should return metrics with prefix on `items()`, `keys()` ([#209](https://github.com/PyTorchLightning/metrics/pull/209))
Expand Down
6 changes: 6 additions & 0 deletions tests/regression/test_explained_variance.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def test_explained_variance_functional(self, multioutput, preds, target, sk_metr
metric_args=dict(multioutput=multioutput),
)

def test_explained_variance_differentiability(self, multioutput, preds, target, sk_metric):
self.run_differentiability_test(
preds=preds, target=target, metric_module=ExplainedVariance, metric_functional=explained_variance,
metric_args={'multioutput': multioutput}
)

@pytest.mark.skipif(
not _TORCH_GREATER_EQUAL_1_6, reason='half support of core operations on not support before pytorch v1.6'
)
Expand Down
5 changes: 5 additions & 0 deletions tests/regression/test_pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def test_pearson_corrcoef_functional(self, preds, target):
preds=preds, target=target, metric_functional=pearson_corrcoef, sk_metric=_sk_pearsonr
)

def test_pearson_corrcoef_differentiability(self, preds, target):
self.run_differentiability_test(
preds=preds, target=target, metric_module=PearsonCorrcoef, metric_functional=pearson_corrcoef
)

# Pearson half + cpu does not work due to missing support in torch.sqrt
@pytest.mark.xfail(reason="PearsonCorrcoef metric does not support cpu + half precision")
def test_pearson_corrcoef_half_cpu(self, preds, target):
Expand Down
6 changes: 6 additions & 0 deletions tests/regression/test_r2score.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def test_r2_functional(self, adjusted, multioutput, preds, target, sk_metric, nu
metric_args=dict(adjusted=adjusted, multioutput=multioutput),
)

def test_r2_differentiabilit(self, adjusted, multioutput, preds, target, sk_metric, num_outputs):
self.run_differentiability_test(
preds=preds, target=target, metric_module=partial(R2Score, num_outputs=num_outputs),
metric_functional=r2score, metric_args=dict(adjusted=adjusted, multioutput=multioutput)
)

@pytest.mark.skipif(
not _TORCH_GREATER_EQUAL_1_6, reason='half support of core operations on not support before pytorch v1.6'
)
Expand Down
5 changes: 5 additions & 0 deletions tests/regression/test_spearman.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def test_spearman_corrcoef(self, preds, target, ddp, dist_sync_on_step):
def test_spearman_corrcoef_functional(self, preds, target):
self.run_functional_metric_test(preds, target, spearman_corrcoef, _sk_metric)

def test_spearman_corrcoef_differentiability(self, preds, target):
self.run_differentiability_test(
preds=preds, target=target, metric_module=SpearmanCorrcoef, metric_functional=spearman_corrcoef
)

# Spearman half + cpu does not work due to missing support in torch.arange
@pytest.mark.xfail(reason="Spearman metric does not support cpu + half precision")
def test_spearman_corrcoef_half_cpu(self, preds, target):
Expand Down
4 changes: 4 additions & 0 deletions torchmetrics/regression/explained_variance.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ def compute(self):
self.sum_squared_target,
self.multioutput,
)

@property
def is_differentiable(self):
return True
4 changes: 4 additions & 0 deletions torchmetrics/regression/pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,7 @@ def compute(self):
preds = dim_zero_cat(self.preds)
target = dim_zero_cat(self.target)
return _pearson_corrcoef_compute(preds, target)

@property
def is_differentiable(self):
return True
4 changes: 4 additions & 0 deletions torchmetrics/regression/r2score.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,7 @@ def compute(self) -> Tensor:
return _r2score_compute(
self.sum_squared_error, self.sum_error, self.residual, self.total, self.adjusted, self.multioutput
)

@property
def is_differentiable(self):
return True
4 changes: 4 additions & 0 deletions torchmetrics/regression/spearman.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ def compute(self):
preds = dim_zero_cat(self.preds)
target = dim_zero_cat(self.target)
return _spearman_corrcoef_compute(preds, target)

@property
def is_differentiable(self):
return False