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 Deep Evidential Regression, generalize Packed layers, and refactor the datasets #48

Merged
merged 24 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
688ec10
:bug: Fix WideResNet training script
o-laurent Sep 16, 2023
bafc4f5
update `dist_estimation` parameter to be an integer
badrmarani Sep 19, 2023
4c689e4
add der loss function (it needs more tests)
badrmarani Sep 29, 2023
4a5df8e
add tests
badrmarani Sep 30, 2023
fd23024
add the reference to the documentation
badrmarani Sep 30, 2023
195924a
add more tests
badrmarani Oct 1, 2023
480ad52
add a toy regression problem
badrmarani Oct 6, 2023
e78f7e0
add a deep evidential regression example
badrmarani Oct 7, 2023
fbba163
add `reduction` argument to the loss
badrmarani Oct 7, 2023
f56e04e
minor changes
badrmarani Oct 7, 2023
fdc41db
minor change
badrmarani Oct 7, 2023
eb93d24
add a new method
badrmarani Oct 7, 2023
e673fca
:zap: Update dependencies
o-laurent Oct 9, 2023
f70b200
:shirt: Standardize WideResNets
o-laurent Oct 10, 2023
927c03b
add expected outcome when all four parameters and the target are set …
badrmarani Oct 11, 2023
e77a9ed
:hammer: Refactor the datasets folder & :white_check_mark: Fix loss test
o-laurent Oct 11, 2023
de76a9a
:book: Add details on datamodules in contributing
o-laurent Oct 11, 2023
5d81cfd
:hammer: Adapt CubicDM as a ds & Add test & Refine tutorial
o-laurent Oct 11, 2023
0a7c265
:sparkles: PackedConv1d is now available
alafage Oct 11, 2023
9b815f7
:bulb: Slight update in PackedConv1d docstring
alafage Oct 11, 2023
4b58408
:sparkles: PackedConv3d is now available
alafage Oct 11, 2023
ea2e56b
Merge pull request #46 from badrmarani/der
o-laurent Oct 11, 2023
cffea9c
:sparkles: Refine TinyImageNet and UCIRegression DMs
o-laurent Oct 11, 2023
c939bbe
:shirt: Rename tutorial
o-laurent Oct 11, 2023
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
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
badrmarani committed Sep 30, 2023
commit 4a5df8e8b74a3e602e597e30a99cdd3a7569b0d9
28 changes: 28 additions & 0 deletions tests/routines/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# fmt:off
from functools import partial
from pathlib import Path

from cli_test_helpers import ArgvContext
from torch import nn

from torch_uncertainty import cli_main, init_args
from torch_uncertainty.losses import NIGLoss
from torch_uncertainty.optimization_procedures import optim_cifar10_resnet18

from .._dummies import DummyRegressionBaseline, DummyRegressionDataModule
Expand Down Expand Up @@ -35,6 +37,32 @@ def test_cli_main_dummy_dist(self):

cli_main(model, dm, root, "dummy", args)

def test_cli_main_dummy_dist_der(self):
root = Path(__file__).parent.absolute().parents[0]
with ArgvContext("file.py"):
args = init_args(DummyRegressionBaseline, DummyRegressionDataModule)

# datamodule
args.root = str(root / "data")
dm = DummyRegressionDataModule(out_features=1, **vars(args))

loss = partial(
NIGLoss,
reg_weight=1e-2,
)

model = DummyRegressionBaseline(
in_features=dm.in_features,
out_features=4,
loss=loss,
optimization_procedure=optim_cifar10_resnet18,
baseline_type="single",
dist_estimation=4,
**vars(args),
)

cli_main(model, dm, root, "dummy_der", args)

def test_cli_main_dummy(self):
root = Path(__file__).parent.absolute().parents[0]
with ArgvContext("file.py"):
Expand Down
13 changes: 12 additions & 1 deletion tests/test_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from torch import nn

from torch_uncertainty.layers.bayesian import BayesLinear
from torch_uncertainty.losses import ELBOLoss
from torch_uncertainty.losses import ELBOLoss, NIGLoss


# fmt: on
Expand Down Expand Up @@ -34,3 +34,14 @@ def test_no_bayes(self):

loss = ELBOLoss(model, criterion, kl_weight=1e-5, num_samples=1)
loss(model(torch.randn(1, 1)), torch.randn(1, 1))


# fmt: on
class TestNIGLoss:
def test_main(self):
with pytest.raises(ValueError):
NIGLoss(reg_weight=-1)

loss = NIGLoss(reg_weight=1e-2)

loss(*torch.rand((1, 4)).split(1, dim=-1), torch.randn(1, 1))
6 changes: 6 additions & 0 deletions torch_uncertainty/losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ def forward(self, inputs: Tensor, targets: Tensor) -> Tensor:
class NIGLoss(nn.Module):
def __init__(self, reg_weight: float) -> None:
super().__init__()

if reg_weight < 0:
raise ValueError(
"The regularization weight should be non-negative. "
f"Got {reg_weight}."
)
self.reg_weight = reg_weight

def _nig_nll(self, gamma, v, alpha, beta, targets):
Expand Down
6 changes: 6 additions & 0 deletions torch_uncertainty/routines/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def validation_step(
beta = F.softplus(logits[..., 3])
vars = beta / (alpha - 1)
self.val_metrics.gnll.update(means, targets, vars)

if means.ndim == 1:
means = means.unsqueeze(-1)
elif self.dist_estimation == 2:
means = logits[..., 0]
vars = F.softplus(logits[..., 1])
Expand Down Expand Up @@ -162,6 +165,9 @@ def test_step(
beta = F.softplus(logits[..., 3])
vars = beta / (alpha - 1)
self.test_metrics.gnll.update(means, targets, vars)

if means.ndim == 1:
means = means.unsqueeze(-1)
elif self.dist_estimation == 2:
means = logits[..., 0]
vars = F.softplus(logits[..., 1])
Expand Down