Skip to content

Commit 2690982

Browse files
v0.2.11
1 parent a142f6d commit 2690982

9 files changed

Lines changed: 2651 additions & 1408 deletions

File tree

.gitignore

Lines changed: 231 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/21_spot_torch_vbdp.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13122,7 +13122,7 @@
1312213122
"name": "python",
1312313123
"nbconvert_exporter": "python",
1312413124
"pygments_lexer": "ipython3",
13125-
"version": "3.10.6"
13125+
"version": "3.10.11"
1312613126
}
1312713127
},
1312813128
"nbformat": 4,

notebooks/23_spot_torch_vbdp.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13916,7 +13916,7 @@
1391613916
"name": "python",
1391713917
"nbconvert_exporter": "python",
1391813918
"pygments_lexer": "ipython3",
13919-
"version": "3.10.10"
13919+
"version": "3.10.11"
1392013920
}
1392113921
},
1392213922
"nbformat": 4,

notebooks/24_spot_torch_regression.ipynb

Lines changed: 47 additions & 1379 deletions
Large diffs are not rendered by default.

notebooks/25_spot_torch_vbdp.ipynb

Lines changed: 2350 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/plot.png

9.06 KB
Loading

pyproject.toml

Lines changed: 3 additions & 2 deletions
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.2.10"
10+
version = "0.2.11"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]
@@ -33,7 +33,8 @@ dependencies = [
3333
"scipy",
3434
"river",
3535
"torch",
36-
"torchmetrics"
36+
"torchmetrics",
37+
"tensorboard",
3738
]
3839
# dynamic = ["version"]
3940

src/spotPython/fun/hypertorch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def fun_torch(self, X, fun_control=None):
8282
show_batch_interval=self.fun_control["show_batch_interval"],
8383
task=self.fun_control["task"],
8484
writer=self.fun_control["writer"],
85+
writerId=config_id,
8586
)
8687
elif self.fun_control["eval"] == "test_cv":
8788
df_eval, _ = evaluate_cv(
@@ -92,6 +93,7 @@ def fun_torch(self, X, fun_control=None):
9293
show_batch_interval=self.fun_control["show_batch_interval"],
9394
task=self.fun_control["task"],
9495
writer=self.fun_control["writer"],
96+
writerId=config_id,
9597
)
9698
elif self.fun_control["eval"] == "test_hold_out":
9799
df_eval, _ = evaluate_hold_out(

src/spotPython/torch/traintest.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,11 @@ def evaluate_cv(
130130
valloader = torch.utils.data.DataLoader(
131131
dataset, batch_size=batch_size_instance, sampler=val_subsampler, num_workers=num_workers
132132
)
133+
# each fold starts with new weights:
133134
reset_weights(net)
134-
# Train fold for several epochs:
135-
# train_fold(
136-
# net,
137-
# trainloader,
138-
# epochs_instance,
139-
# loss_function,
140-
# optimizer,
141-
# device,
142-
# show_batch_interval=show_batch_interval,
143-
# writer=writer,
144-
# )
145-
# # Validate fold: use only loss for tuning
146-
# metric_values[fold], loss_values[fold] = validate_fold_or_hold_out(
147-
# net, valloader=valloader, loss_function=loss_function, metric=metric, device=device, task=task
148-
# )
149135
# Early stopping parameters
150136
best_val_loss = float("inf")
151137
counter = 0
152-
# We only have "one fold" which is trained for several epochs
153-
# (we do not have to reset the weights for each fold):
154138
for epoch in range(epochs_instance):
155139
print(f"Epoch: {epoch + 1}")
156140
# training loss from one epoch:
@@ -167,24 +151,23 @@ def evaluate_cv(
167151
)
168152
# TODO: scheduler.step()
169153
# Early stopping check. Calculate validation loss from one epoch:
170-
metric_val, val_loss = validate_fold_or_hold_out(
154+
metric_values[fold], loss_values[fold] = validate_fold_or_hold_out(
171155
net, valloader=valloader, loss_function=loss_function, metric=metric, device=device, task=task
172156
)
173-
metric_values[fold], loss_values[fold] = metric_val, val_loss
174157
# Log the running loss averaged per batch
175158
metric_name = "Metric"
176159
if metric is None:
177160
metric_name = type(metric).__name__
178-
print(f"{metric_name} value on hold-out data: {metric_val}")
161+
print(f"{metric_name} value on hold-out data: {metric_values[fold]}")
179162
if writer is not None:
180163
writer.add_scalars(
181-
"evaluate_hold_out: Train & Val Loss and Val Metric" + writerId,
182-
{"Train loss": training_loss, "Val loss": val_loss, metric_name: metric_val},
164+
"evaluate_cv fold:" + str(fold + 1) + ". Train & Val Loss and Val Metric" + writerId,
165+
{"Train loss": training_loss, "Val loss": loss_values[fold], metric_name: metric_values[fold]},
183166
epoch + 1,
184167
)
185168
writer.flush()
186-
if val_loss < best_val_loss:
187-
best_val_loss = val_loss
169+
if loss_values[fold] < best_val_loss:
170+
best_val_loss = loss_values[fold]
188171
counter = 0
189172
# save model:
190173
if path is not None:
@@ -194,7 +177,7 @@ def evaluate_cv(
194177
if counter >= patience_instance:
195178
print(f"Early stopping at epoch {epoch}")
196179
break
197-
# TODO: Compute Metric on all folds
180+
# TODO: Compute Metric on all folds and return average to spotPython
198181
df_eval = sum(loss_values.values()) / len(loss_values.values())
199182
df_metrics = sum(metric_values.values()) / len(metric_values.values())
200183
df_preds = np.nan
@@ -204,6 +187,14 @@ def evaluate_cv(
204187
df_preds = np.nan
205188
add_attributes(net, removed_attributes)
206189
if writer is not None:
190+
metric_name = "Metric"
191+
if metric is None:
192+
metric_name = type(metric).__name__
193+
writer.add_scalars(
194+
"CV: Val Loss and Val Metric" + writerId,
195+
{"CV-loss": df_eval, metric_name: df_metrics},
196+
epoch + 1,
197+
)
207198
writer.flush()
208199
return df_eval, df_preds, df_metrics
209200

0 commit comments

Comments
 (0)