Skip to content

Commit 5981890

Browse files
test netlightregression
1 parent a890976 commit 5981890

6 files changed

Lines changed: 229 additions & 1334 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,4 @@ notebooks/data/spotPython/data_sensitive.csv
300300
notebooks/data/spotPython/data_sensitive_rmNA.csv
301301
notebooks/runs_OLD/*
302302
runs/lightning_logs/*
303+
lightning_logs/*

notebooks/00_spotPython_tests.ipynb

Lines changed: 109 additions & 1252 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotPython"
10-
version = "0.6.54"
10+
version = "0.6.55"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/hyperparameters/optimizer.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,41 @@
55
def optimizer_handler(
66
optimizer_name: str, params: Union[list, torch.Tensor], lr_mult: float = 1.0, **kwargs: Any
77
) -> torch.optim.Optimizer:
8-
"""Returns an instance of the specified optimizer.
8+
"""Returns an instance of the specified optimizer. See Notes below for supported optimizers.
99
1010
Args:
11-
optimizer_name (str): The name of the optimizer to use.
12-
params (list or torch.Tensor): The parameters to optimize.
13-
lr_mult (float, optional): A multiplier for the learning rate. Defaults to 1.0.
14-
**kwargs: Additional keyword arguments for the optimizer.
11+
optimizer_name (str):
12+
The name of the optimizer to use.
13+
params (list or torch.Tensor):
14+
The parameters to optimize.
15+
lr_mult (float, optional):
16+
A multiplier for the learning rate. Defaults to 1.0.
17+
**kwargs:
18+
Additional keyword arguments for the optimizer.
19+
20+
Notes:
21+
The following optimizers are supported (see also: https://pytorch.org/docs/stable/optim.html#base-class):
22+
23+
* Adadelta
24+
* Adagrad
25+
* Adam
26+
* AdamW
27+
* SparseAdam
28+
* ASGD
29+
* LBFGS
30+
* NAdam
31+
* RAdam
32+
* RMSprop
33+
* Rprop
34+
* SGD
35+
1536
1637
Returns:
1738
(torch.optim.Optimizer):
1839
An instance of the specified optimizer.
1940
2041
Examples:
42+
>>>
2143
>>> model = torch.nn.Linear(10, 1)
2244
>>> optimizer = optimizer_handler("Adadelta", model.parameters(), lr_mult=0.5)
2345
>>> print(optimizer)

src/spotPython/light/netlightregression.py

Lines changed: 57 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,58 @@ class NetLightRegression(L.LightningModule):
3737
3838
Examples:
3939
>>> from torch.utils.data import DataLoader
40-
>>> from torchvision.datasets import MNIST
41-
>>> from torchvision.transforms import ToTensor
42-
>>> train_data = MNIST(PATH_DATASETS,
43-
train=True,
44-
download=True,
45-
transform=ToTensor())
46-
>>> train_loader = DataLoader(train_data,
47-
batch_size=BATCH_SIZE)
48-
>>> net_light_base = NetLightRegression(l1=128,
49-
epochs=10,
50-
batch_size=BATCH_SIZE,
51-
initialization='xavier',
52-
act_fn=nn.ReLU(),
53-
optimizer='Adam',
54-
dropout_prob=0.1,
55-
lr_mult=0.1,
56-
patience=5)
57-
>>> trainer = L.Trainer(max_epochs=10)
58-
>>> trainer.fit(net_light_base, train_loader)
40+
from spotPython.data.diabetes import Diabetes
41+
from spotPython.light.netlightregression import NetLightRegression
42+
from torch import nn
43+
import lightning as L
44+
PATH_DATASETS = './data'
45+
BATCH_SIZE = 8
46+
dataset = Diabetes()
47+
train_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
48+
test_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
49+
val_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
50+
batch_x, batch_y = next(iter(train_loader))
51+
print(batch_x.shape)
52+
print(batch_y.shape)
53+
net_light_base = NetLightRegression(l1=128,
54+
epochs=10,
55+
batch_size=BATCH_SIZE,
56+
initialization='xavier',
57+
act_fn=nn.ReLU(),
58+
optimizer='Adam',
59+
dropout_prob=0.1,
60+
lr_mult=0.1,
61+
patience=5,
62+
_L_in=10,
63+
_L_out=1)
64+
trainer = L.Trainer(max_epochs=2, enable_progress_bar=True)
65+
trainer.fit(net_light_base, train_loader)
66+
trainer.validate(net_light_base, val_loader)
67+
trainer.test(net_light_base, test_loader)
68+
69+
| Name | Type | Params | In sizes | Out sizes
70+
-------------------------------------------------------------
71+
0 | layers | Sequential | 15.9 K | [8, 10] | [8, 1]
72+
-------------------------------------------------------------
73+
15.9 K Trainable params
74+
0 Non-trainable params
75+
15.9 K Total params
76+
0.064 Total estimated model params size (MB)
77+
78+
─────────────────────────────────────────────────────────────
79+
Validate metric DataLoader 0
80+
─────────────────────────────────────────────────────────────
81+
hp_metric 29010.7734375
82+
val_loss 29010.7734375
83+
─────────────────────────────────────────────────────────────
84+
─────────────────────────────────────────────────────────────
85+
Test metric DataLoader 0
86+
─────────────────────────────────────────────────────────────
87+
hp_metric 29010.7734375
88+
val_loss 29010.7734375
89+
─────────────────────────────────────────────────────────────
90+
91+
[{'val_loss': 28981.529296875, 'hp_metric': 28981.529296875}]
5992
"""
6093

6194
def __init__(
@@ -93,18 +126,6 @@ def __init__(
93126
94127
Raises:
95128
ValueError: If l1 is less than 4.
96-
Examples:
97-
>>> from torch.utils.data import DataLoader
98-
>>> from torchvision.datasets import MNIST
99-
>>> from torchvision.transforms import ToTensor
100-
>>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
101-
>>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
102-
>>> net_light_base = NetLightRegression(l1=128, epochs=10, batch_size=BATCH_SIZE,
103-
initialization='xavier', act_fn=nn.ReLU(),
104-
optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
105-
patience=5)
106-
>>> trainer = L.Trainer(max_epochs=10)
107-
>>> trainer.fit(net_light_base, train_loader)
108129
109130
"""
110131
super().__init__()
@@ -148,19 +169,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
148169
x (torch.Tensor): A tensor containing a batch of input data.
149170
150171
Returns:
151-
torch.Tensor: A tensor containing the probabilities for each class.
152-
Examples:
153-
>>> from torch.utils.data import DataLoader
154-
>>> from torchvision.datasets import MNIST
155-
>>> from torchvision.transforms import ToTensor
156-
>>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
157-
>>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
158-
>>> net_light_base = NetLightRegression(l1=128,
159-
epochs=10,
160-
batch_size=BATCH_SIZE,
161-
initialization='xavier', act_fn=nn.ReLU(),
162-
optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
163-
patience=5)
172+
torch.Tensor: A tensor containing the output of the model.
164173
165174
"""
166175
x = self.layers(x)
@@ -175,20 +184,6 @@ def training_step(self, batch: tuple) -> torch.Tensor:
175184
176185
Returns:
177186
torch.Tensor: A tensor containing the loss for this batch.
178-
Examples:
179-
>>> from torch.utils.data import DataLoader
180-
>>> from torchvision.datasets import MNIST
181-
>>> from torchvision.transforms import ToTensor
182-
>>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
183-
>>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
184-
>>> net_light_base = NetLightRegression(l1=128,
185-
epochs=10,
186-
batch_size=BATCH_SIZE,
187-
initialization='xavier', act_fn=nn.ReLU(),
188-
optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
189-
patience=5)
190-
>>> trainer = L.Trainer(max_epochs=10)
191-
>>> trainer.fit(net_light_base, train_loader)
192187
193188
"""
194189
x, y = batch
@@ -200,7 +195,7 @@ def training_step(self, batch: tuple) -> torch.Tensor:
200195
# self.log("train_mae_loss", mae_loss, on_step=True, on_epoch=True, prog_bar=True)
201196
return val_loss
202197

203-
def validation_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False):
198+
def validation_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False) -> torch.Tensor:
204199
"""
205200
Performs a single validation step.
206201
@@ -210,21 +205,7 @@ def validation_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False):
210205
prog_bar (bool, optional): Whether to display the progress bar. Defaults to False.
211206
212207
Returns:
213-
(NoneType): None
214-
Examples:
215-
>>> from torch.utils.data import DataLoader
216-
>>> from torchvision.datasets import MNIST
217-
>>> from torchvision.transforms import ToTensor
218-
>>> val_data = MNIST(PATH_DATASETS, train=False, download=True, transform=ToTensor())
219-
>>> val_loader = DataLoader(val_data, batch_size=BATCH_SIZE)
220-
>>> net_light_base = NetLightRegression(l1=128,
221-
epochs=10,
222-
batch_size=BATCH_SIZE,
223-
initialization='xavier', act_fn=nn.ReLU(),
224-
optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
225-
patience=5)
226-
>>> trainer = L.Trainer(max_epochs=10)
227-
>>> trainer.fit(net_light_base, val_loader)
208+
torch.Tensor: A tensor containing the loss for this batch.
228209
229210
"""
230211
x, y = batch
@@ -237,7 +218,7 @@ def validation_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False):
237218
self.log("hp_metric", val_loss, prog_bar=prog_bar)
238219
return val_loss
239220

240-
def test_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False) -> tuple:
221+
def test_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False) -> torch.Tensor:
241222
"""
242223
Performs a single test step.
243224
@@ -247,7 +228,7 @@ def test_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False) -> tup
247228
prog_bar (bool, optional): Whether to display the progress bar. Defaults to False.
248229
249230
Returns:
250-
tuple: A tuple containing the loss and accuracy for this batch.
231+
torch.Tensor: A tensor containing the loss for this batch.
251232
"""
252233
x, y = batch
253234
y_hat = self(x)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
from torch.utils.data import DataLoader
3+
from spotPython.data.diabetes import Diabetes
4+
from spotPython.light.netlightregression import NetLightRegression
5+
from torch import nn
6+
import lightning as L
7+
8+
9+
def test_net_light_regression_class():
10+
BATCH_SIZE = 8
11+
12+
dataset = Diabetes()
13+
train_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
14+
test_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
15+
val_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
16+
17+
net_light_base = NetLightRegression(l1=128,
18+
epochs=10,
19+
batch_size=BATCH_SIZE,
20+
initialization='xavier',
21+
act_fn=nn.ReLU(), optimizer='Adam',
22+
dropout_prob=0.1,
23+
lr_mult=0.1, patience=5,
24+
_L_in=10,
25+
_L_out=1)
26+
trainer = L.Trainer(max_epochs=2, enable_progress_bar=False)
27+
trainer.fit(net_light_base, train_loader)
28+
trainer.validate(net_light_base, val_loader)
29+
res = trainer.test(net_light_base, test_loader)
30+
# test if the entry 'hp_metric' is in the res dict
31+
assert 'hp_metric' in res[0].keys()
32+
33+
if __name__ == "__main__":
34+
pytest.main(["-v", __file__])

0 commit comments

Comments
 (0)