Skip to content

Commit 09780af

Browse files
v0.2.37
Improved PyTorch printout
1 parent 0a826f2 commit 09780af

2 files changed

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

src/spotPython/torch/traintest.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
22
from sklearn.model_selection import KFold
33
import torch
4-
from torch import nn as nn
54
from spotPython.utils.device import getDevice
65
from torch.utils.data import random_split
76
from spotPython.utils.classes import get_additional_attributes
@@ -70,14 +69,13 @@ def validate_one_epoch(net, valloader, loss_function, metric, device, task):
7069
val_loss += loss.cpu().numpy()
7170
val_steps += 1
7271
loss = val_loss / val_steps
73-
print(f"Loss on hold-out set: {loss}")
74-
if task == "classification":
75-
accuracy = correct / total
76-
print(f"Accuracy on hold-out set: {accuracy}")
77-
# metric on all batches using custom accumulation
7872
metric_value = metric.compute()
7973
metric_name = type(metric).__name__
80-
print(f"{metric_name} value on hold-out data: {metric_value}")
74+
print(f"{metric_name}: {metric_value:.16f}", end=" | ")
75+
print(f"Loss: {loss:.16f}", end=" | ")
76+
if task == "classification":
77+
accuracy = correct / total
78+
print(f"Acc: {accuracy:.16f}.")
8179
return metric_value, loss
8280

8381

@@ -131,7 +129,7 @@ def evaluate_cv(
131129
best_val_loss = float("inf")
132130
counter = 0
133131
for epoch in range(epochs_instance):
134-
print(f"Epoch: {epoch + 1}")
132+
print(f"Epoch: {epoch +1}", end=" | ")
135133
# training loss from one epoch:
136134
training_loss = train_one_epoch(
137135
net=net,
@@ -209,9 +207,6 @@ def evaluate_hold_out(
209207
try:
210208
device = getDevice(device=device)
211209
net.to(device)
212-
# loss_function = nn.CrossEntropyLoss()
213-
# TODO: optimizer = optim.Adam(net.parameters(), lr=lr)
214-
# optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9)
215210
optimizer = optimizer_handler(
216211
optimizer_name=optimizer_instance,
217212
params=net.parameters(),
@@ -233,7 +228,7 @@ def evaluate_hold_out(
233228
# We only have "one fold" which is trained for several epochs
234229
# (we do not have to reset the weights for each fold):
235230
for epoch in range(epochs_instance):
236-
print(f"Epoch: {epoch + 1}")
231+
print(f"Epoch: {epoch + 1}", end=" | ")
237232
# training loss from one epoch:
238233
training_loss = train_one_epoch(
239234
net=net,
@@ -282,7 +277,6 @@ def evaluate_hold_out(
282277
if writer is not None:
283278
writer.flush()
284279
print(f"Returned to Spot: Validation loss: {df_eval}")
285-
print("----------------------------------------------")
286280
return df_eval, df_preds
287281

288282

@@ -384,11 +378,6 @@ def test_tuned(net, shuffle, test_dataset=None, loss_function=None, metric=None,
384378
net.eval()
385379
try:
386380
device = getDevice(device=device)
387-
if torch.cuda.is_available():
388-
device = "cuda:0"
389-
if torch.cuda.device_count() > 1:
390-
print("We will use", torch.cuda.device_count(), "GPUs!")
391-
net = nn.DataParallel(net)
392381
net.to(device)
393382
valloader = torch.utils.data.DataLoader(
394383
test_dataset, batch_size=int(batch_size_instance), shuffle=shuffle, num_workers=0

0 commit comments

Comments
 (0)