@@ -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