Skip to content

Commit 240891a

Browse files
0.17.3
architecture.py
1 parent c8ca19e commit 240891a

3 files changed

Lines changed: 49 additions & 24 deletions

File tree

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.17.2"
10+
version = "0.17.3"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def generate_div2_list(n, n_min) -> list:
2+
result = []
3+
current = n
4+
repeats = 1
5+
max_repeats = 4
6+
while current >= n_min:
7+
result.extend([current] * min(repeats, max_repeats))
8+
current = current // 2
9+
repeats = repeats + 1
10+
return result
11+
12+
13+
def get_hidden_sizes(_L_in, l1, n=10) -> list:
14+
"""
15+
Generates a list of hidden sizes for a neural network with a given input size and l1 regularization.
16+
The list is generated by dividing the input size by 2 until the minimum size is reached.
17+
18+
Args:
19+
_L_in (int):
20+
input size.
21+
l1 (int):
22+
l1 regularization.
23+
n (int):
24+
number of hidden sizes to generate.
25+
26+
Returns:
27+
(list):
28+
list of hidden sizes.
29+
30+
Examples:
31+
>>> from spotpython.hyperparameters.architecture import get_hidden_sizes
32+
_L_in = 10
33+
l1 = 10
34+
n = 10
35+
get_hidden_sizes(_L_in, l1, n)
36+
[10, 5, 2, 1, 1, 1, 1, 1, 1, 1]
37+
"""
38+
if l1 < 4:
39+
raise ValueError("l1 must be at least 4")
40+
n_low = _L_in // 4
41+
n_high = max(l1, 2 * n_low)
42+
hidden_sizes = generate_div2_list(n_high, n_low)
43+
# keep only the first n values of hidden_sizes list
44+
if len(hidden_sizes) > n:
45+
hidden_sizes = hidden_sizes[:n]
46+
return hidden_sizes

src/spotpython/light/regression/nn_linear_regressor.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from spotpython.hyperparameters.optimizer import optimizer_handler
55
import torchmetrics.functional.regression
66
import torch.optim as optim
7+
from spotpython.hyperparameters.architecture import get_hidden_sizes
78

89

910
class NNLinearRegressor(L.LightningModule):
@@ -186,9 +187,7 @@ def __init__(
186187
# set dummy input array for Tensorboard Graphs
187188
# set log_graph=True in Trainer to see the graph (in traintest.py)
188189
self.example_input_array = torch.zeros((batch_size, self._L_in))
189-
if self.hparams.l1 < 4:
190-
raise ValueError("l1 must be at least 4")
191-
hidden_sizes = self._get_hidden_sizes()
190+
hidden_sizes = get_hidden_sizes(_L_in=self._L_in, l1=l1, n=10)
192191

193192
if batch_norm:
194193
# Add batch normalization layers
@@ -238,26 +237,6 @@ def _init_weights(self, module):
238237
if module.bias is not None:
239238
nn.init.zeros_(module.bias)
240239

241-
def _generate_div2_list(self, n, n_min) -> list:
242-
result = []
243-
current = n
244-
repeats = 1
245-
max_repeats = 4
246-
while current >= n_min:
247-
result.extend([current] * min(repeats, max_repeats))
248-
current = current // 2
249-
repeats = repeats + 1
250-
return result
251-
252-
def _get_hidden_sizes(self, n=10) -> list:
253-
n_low = self._L_in // 4
254-
n_high = max(self.hparams.l1, 2 * n_low)
255-
hidden_sizes = self._generate_div2_list(n_high, n_low)
256-
# keep only the first 10 values of hidden_sizes list
257-
if len(hidden_sizes) > n:
258-
hidden_sizes = hidden_sizes[:n]
259-
return hidden_sizes
260-
261240
def forward(self, x: torch.Tensor) -> torch.Tensor:
262241
"""
263242
Performs a forward pass through the model.

0 commit comments

Comments
 (0)