Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing early stop for benchmark and making methods consistent #23

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
implementing early stop for benchmark and making methods consistent
  • Loading branch information
NathanGavenski committed Aug 5, 2024
commit dbdc346266d63c74cecb1ee1a1ec390b52b20a42
1 change: 1 addition & 0 deletions src/benchmark/methods/abco.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def train(
n_epochs: int,
train_dataset: Dict[str, DataLoader],
eval_dataset: Dict[str, DataLoader] = None,
always_ave: bool = False,
folder: str = None
) -> Self:
if folder is None:
Expand Down
8 changes: 6 additions & 2 deletions src/benchmark/methods/bc.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,22 @@ def train(
train_dataset: DataLoader,
eval_dataset: DataLoader = None,
always_save: bool = False,
folder: str = None
) -> Self:
"""Train process.

Args:
n_epochs (int): amount of epoch to run.
train_dataset (DataLoader): data to train.
eval_dataset (DataLoader): data to eval. Defaults to None.
always_save (bool): whether it should save all eval steps.
folder (str): a specific folder to save the benchmark results.

Returns:
method (Self): trained method.
"""
folder = f"./benchmark_results/bc/{self.environment_name}"
if folder is None:
folder = f"./benchmark_results/bc/{self.environment_name}"
if not os.path.exists(folder):
os.makedirs(f"{folder}/")

Expand Down Expand Up @@ -158,7 +162,7 @@ def train(
best_model = metrics["aer"]
self.save(name=epoch if always_save else None)

if early_stop(metrics["aer"]):
if self.early_stop(metrics["aer"]):
return self

return self
Expand Down
12 changes: 9 additions & 3 deletions src/benchmark/methods/bco.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def train(
n_epochs: int,
train_dataset: Dict[str, DataLoader],
eval_dataset: Dict[str, DataLoader] = None,
always_save: bool = False,
folder: str = None
) -> Self:
"""Train process.
Expand All @@ -154,6 +155,8 @@ def train(
n_epochs (int): amount of epoch to run.
train_dataset (DataLoader): data to train.
eval_dataset (DataLoader): data to eval. Defaults to None.
always_save (bool): whether it should save all eval steps.
folder (str): a specific folder to save the benchmark results.

Returns:
method (Self): trained method.
Expand Down Expand Up @@ -209,12 +212,15 @@ def train(
if epoch % self.enjoy_criteria == 0:
train_dataset = self._append_samples(train_dataset)

if epoch % self.enjoy_criteria == 0 or epoch + 1 == n_epochs:
if epoch > 0 and epoch % self.enjoy_criteria == 0:
metrics = self._enjoy()
board.add_scalars("Enjoy", epoch="enjoy", **metrics)
board.step("enjoy")
if best_model < metrics["aer"]:
self.save()
if best_model < metrics["aer"] or always_save:
self.save(name=epoch if always_save else None)

if self.early_stop(metrics["aer"]):
return self

return self

Expand Down
1 change: 1 addition & 0 deletions src/benchmark/methods/iupe.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def train(
n_epochs: int,
train_dataset: Dict[str, DataLoader],
eval_dataset: Dict[str, DataLoader] = None,
always_save: bool = False,
folder: str = None
) -> Self:
if folder is None:
Expand Down
2 changes: 1 addition & 1 deletion src/benchmark/methods/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,4 @@ def early_stop(self, metric: Metrics) -> bool:
Returns:
stop (bool): if it should stop or not.
"""
raise NotImplementedError()
raise False
Loading