11import numpy as np
22from sklearn .model_selection import KFold
33import torch
4- from torch import nn as nn
54from spotPython .utils .device import getDevice
65from torch .utils .data import random_split
76from 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