-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Callback] Introduce custom callbacks
Signed-off-by: Matteo Bettini <[email protected]>
- Loading branch information
1 parent
b32c0a2
commit f4a1b5f
Showing
4 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from tensordict import TensorDictBase | ||
|
||
|
||
class Callback: | ||
def __init__(self): | ||
self.experiment = None | ||
|
||
def on_batch_collected(self, batch: TensorDictBase): | ||
pass | ||
|
||
def on_train_end(self, training_td: TensorDictBase): | ||
pass | ||
|
||
def on_evaluation_end(self, rollouts: List[TensorDictBase]): | ||
pass | ||
|
||
|
||
class CallbackNotifier: | ||
def __init__(self, experiment, callbacks: List[Callback]): | ||
self.callbacks = callbacks | ||
for callback in self.callbacks: | ||
callback.experiment = experiment | ||
|
||
def on_batch_collected(self, batch: TensorDictBase): | ||
for callback in self.callbacks: | ||
callback.on_batch_collected(batch) | ||
|
||
def on_train_end(self, training_td: TensorDictBase): | ||
for callback in self.callbacks: | ||
callback.on_train_end(training_td) | ||
|
||
def on_evaluation_end(self, rollouts: List[TensorDictBase]): | ||
for callback in self.callbacks: | ||
callback.on_evaluation_end(rollouts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import List | ||
|
||
from benchmarl.algorithms import MappoConfig | ||
from benchmarl.environments import VmasTask | ||
from benchmarl.experiment import Experiment, ExperimentConfig | ||
from benchmarl.experiment.callback import Callback | ||
from benchmarl.models.mlp import MlpConfig | ||
from tensordict import TensorDictBase | ||
|
||
|
||
class MyCallbackA(Callback): | ||
def on_batch_collected(self, batch: TensorDictBase): | ||
print(f"Callback A is doing something with the sampling batch {batch}") | ||
|
||
def on_train_end(self, training_td: TensorDictBase): | ||
print( | ||
f"Callback A is doing something with the training tensordict {training_td}" | ||
) | ||
|
||
def on_evaluation_end(self, rollouts: List[TensorDictBase]): | ||
print(f"Callback A is doing something with the evaluation rollouts {rollouts}") | ||
|
||
|
||
class MyCallbackB(Callback): | ||
def on_evaluation_end(self, rollouts: List[TensorDictBase]): | ||
print( | ||
"Callback B just reminds you that you fo not need to implement all methods and" | ||
f"you always have access to the experiment {self.experiment} and all its contents" | ||
f"like the policy {self.experiment.policy}" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
experiment_config = ExperimentConfig.get_from_yaml() | ||
task = VmasTask.BALANCE.get_from_yaml() | ||
algorithm_config = MappoConfig.get_from_yaml() | ||
model_config = MlpConfig.get_from_yaml() | ||
critic_model_config = MlpConfig.get_from_yaml() | ||
|
||
experiment = Experiment( | ||
task=task, | ||
algorithm_config=algorithm_config, | ||
model_config=model_config, | ||
critic_model_config=critic_model_config, | ||
seed=0, | ||
config=experiment_config, | ||
callbacks=[MyCallbackA(), MyCallbackB()], | ||
) | ||
experiment.run() |