From 1e178c1c2638231d55d8be5989d2fb60ce641d34 Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Sat, 13 Jan 2024 13:26:05 -0300 Subject: [PATCH 1/3] Fix sweep to keep the best model and add best_score of the first model --- src/pytorch_tabular/tabular_model_sweep.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pytorch_tabular/tabular_model_sweep.py b/src/pytorch_tabular/tabular_model_sweep.py index 047806f7..7a483b64 100644 --- a/src/pytorch_tabular/tabular_model_sweep.py +++ b/src/pytorch_tabular/tabular_model_sweep.py @@ -325,15 +325,16 @@ def _init_tabular_model(m): res_dict["params"] = params results.append(res_dict) if best_model is None: - best_model = tabular_model + best_model = copy.deepcopy(tabular_model) + best_score = res_dict[f"test_{rank_metric[0]}"] else: if is_lower_better: if res_dict[f"test_{rank_metric[0]}"] < best_score: - best_model = tabular_model + best_model = copy.deepcopy(tabular_model) best_score = res_dict[f"test_{rank_metric[0]}"] else: if res_dict[f"test_{rank_metric[0]}"] > best_score: - best_model = tabular_model + best_model = copy.deepcopy(tabular_model) best_score = res_dict[f"test_{rank_metric[0]}"] if verbose: logger.info("Model Sweep Finished") From 42e95cb6d895a74b43741fe15455b46fde0eff75 Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Sun, 14 Jan 2024 07:45:46 -0300 Subject: [PATCH 2/3] Remove datamodule before deepcopy best_model --- src/pytorch_tabular/tabular_model_sweep.py | 33 ++++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/pytorch_tabular/tabular_model_sweep.py b/src/pytorch_tabular/tabular_model_sweep.py index 69e6909a..5fdd7d5e 100644 --- a/src/pytorch_tabular/tabular_model_sweep.py +++ b/src/pytorch_tabular/tabular_model_sweep.py @@ -359,28 +359,31 @@ def _init_tabular_model(m): res_dict["time_taken"] = time.time() - start_time res_dict["time_taken_per_epoch"] = res_dict["time_taken"] / res_dict["epochs"] - if verbose: - logger.info(f"Finished Training {name}") - logger.info("Results:" f" {', '.join([f'{k}: {v}' for k,v in res_dict.items()])}") - res_dict["params"] = params - results.append(res_dict) - if best_model is None: - best_model = copy.deepcopy(tabular_model) - best_score = res_dict[f"test_{rank_metric[0]}"] - else: - if is_lower_better: - if res_dict[f"test_{rank_metric[0]}"] < best_score: - best_model = copy.deepcopy(tabular_model) - best_score = res_dict[f"test_{rank_metric[0]}"] - else: - if res_dict[f"test_{rank_metric[0]}"] > best_score: + if verbose: + logger.info(f"Finished Training {name}") + logger.info("Results:" f" {', '.join([f'{k}: {v}' for k,v in res_dict.items()])}") + res_dict["params"] = params + results.append(res_dict) + if return_best_model: + tabular_model.datamodule = None + if best_model is None: best_model = copy.deepcopy(tabular_model) best_score = res_dict[f"test_{rank_metric[0]}"] + else: + if is_lower_better: + if res_dict[f"test_{rank_metric[0]}"] < best_score: + best_model = copy.deepcopy(tabular_model) + best_score = res_dict[f"test_{rank_metric[0]}"] + else: + if res_dict[f"test_{rank_metric[0]}"] > best_score: + best_model = copy.deepcopy(tabular_model) + best_score = res_dict[f"test_{rank_metric[0]}"] if verbose: logger.info("Model Sweep Finished") logger.info(f"Best Model: {best_model.name}") results = pd.DataFrame(results).sort_values(by=f"test_{rank_metric[0]}", ascending=is_lower_better) if return_best_model: + best_model.datamodule = datamodule return results, best_model else: return results From f13a0e3af2cf7dc03dac94118bb17ec5c09658e1 Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Sun, 14 Jan 2024 10:01:20 -0300 Subject: [PATCH 3/3] Add sanity check --- src/pytorch_tabular/tabular_model_sweep.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytorch_tabular/tabular_model_sweep.py b/src/pytorch_tabular/tabular_model_sweep.py index 5fdd7d5e..bf3a192f 100644 --- a/src/pytorch_tabular/tabular_model_sweep.py +++ b/src/pytorch_tabular/tabular_model_sweep.py @@ -382,7 +382,7 @@ def _init_tabular_model(m): logger.info("Model Sweep Finished") logger.info(f"Best Model: {best_model.name}") results = pd.DataFrame(results).sort_values(by=f"test_{rank_metric[0]}", ascending=is_lower_better) - if return_best_model: + if return_best_model and best_model is not None: best_model.datamodule = datamodule return results, best_model else: