Skip to content

Commit 4375a36

Browse files
0.15.25
batch_norm as a new option
1 parent a9499fd commit 4375a36

4 files changed

Lines changed: 32 additions & 7 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.15.24"
10+
version = "0.15.25"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/hyperdict/light_hyper_dict.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,18 @@
391391
"lower": 2,
392392
"upper": 6
393393
},
394+
"batch_norm": {
395+
"levels": [
396+
0,
397+
1
398+
],
399+
"type": "factor",
400+
"default": 0,
401+
"transform": "None",
402+
"core_model_parameter_type": "bool",
403+
"lower": 0,
404+
"upper": 1
405+
},
394406
"initialization": {
395407
"levels": [
396408
"Default",

src/spotpython/light/regression/nn_linear_regressor.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class NNLinearRegressor(L.LightningModule):
2929
The learning rate multiplier for the optimizer.
3030
patience (int):
3131
The number of epochs to wait before early stopping.
32+
batch_norm (bool):
33+
Whether to use batch normalization or not.
3234
_L_in (int):
3335
The number of input features.
3436
_L_out (int):
@@ -104,6 +106,7 @@ def __init__(
104106
dropout_prob: float,
105107
lr_mult: float,
106108
patience: int,
109+
batch_norm: bool,
107110
_L_in: int,
108111
_L_out: int,
109112
_torchmetric: str,
@@ -130,6 +133,8 @@ def __init__(
130133
The learning rate multiplier for the optimizer.
131134
patience (int):
132135
The number of epochs to wait before early stopping.
136+
batch_norm (bool):
137+
Whether to use batch normalization or not.
133138
_L_in (int):
134139
The number of input features. Not a hyperparameter, but needed to create the network.
135140
_L_out (int):
@@ -173,12 +178,19 @@ def __init__(
173178
for i in range(len(layer_sizes) - 1):
174179
current_layer_size = layer_sizes[i]
175180
next_layer_size = layer_sizes[i + 1]
176-
layers += [
177-
nn.Linear(current_layer_size, next_layer_size),
178-
nn.BatchNorm1d(next_layer_size), # Add Batch Normalization here
179-
self.hparams.act_fn,
180-
nn.Dropout(self.hparams.dropout_prob),
181-
]
181+
if self.hparams.initialization:
182+
layers += [
183+
nn.Linear(current_layer_size, next_layer_size),
184+
nn.BatchNorm1d(next_layer_size), # Add Batch Normalization here
185+
self.hparams.act_fn,
186+
nn.Dropout(self.hparams.dropout_prob),
187+
]
188+
else:
189+
layers += [
190+
nn.Linear(current_layer_size, next_layer_size),
191+
self.hparams.act_fn,
192+
nn.Dropout(self.hparams.dropout_prob),
193+
]
182194
layers += [nn.Linear(layer_sizes[-1], self._L_out)]
183195

184196
# Wrap the layers into a sequential container

test/test_nn_linear_regressor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"dropout_prob": 0.1,
4646
"lr_mult": 0.1,
4747
"patience": 5,
48+
"batch_norm": 0,
4849
"_L_in": 10,
4950
"_L_out": 1,
5051
"_torchmetric": "mean_squared_error",

0 commit comments

Comments
 (0)