-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Draft: for explicit seed mechanism of train seed, specific test seeds #1031
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0548533
Draft for explicit seed mechanism of train seed, specific test seeds
bordeauxred ee56baf
Add enum instead of literal, fix subset of comments
bordeauxred 945891e
Fix tuple type hint, add seed of env generating a transtition to the …
bordeauxred 5063f1a
load tensorboard logs into python using tbparse
bordeauxred File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,103 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
from collections.abc import Sequence | ||
from typing import Literal | ||
|
||
import torch | ||
|
||
from examples.mujoco.mujoco_env import MujocoEnvFactory | ||
from tianshou.highlevel.config import SamplingConfig | ||
from tianshou.highlevel.experiment import ( | ||
ExperimentConfig, | ||
EvaluationProtocolExperimentConfig, | ||
PPOExperimentBuilder, | ||
) | ||
from tianshou.highlevel.params.dist_fn import ( | ||
DistributionFunctionFactoryIndependentGaussians, | ||
) | ||
from tianshou.highlevel.params.lr_scheduler import LRSchedulerFactoryLinear | ||
from tianshou.highlevel.params.policy_params import PPOParams | ||
from tianshou.utils import logging | ||
from tianshou.utils.logging import datetime_tag | ||
|
||
|
||
def main( | ||
experiment_config: EvaluationProtocolExperimentConfig, | ||
task: str = "Ant-v4", | ||
buffer_size: int = 4096, | ||
hidden_sizes: Sequence[int] = (64, 64), | ||
lr: float = 3e-4, | ||
gamma: float = 0.99, | ||
epoch: int = 100, | ||
step_per_epoch: int = 30000, | ||
step_per_collect: int = 2048, | ||
repeat_per_collect: int = 10, | ||
batch_size: int = 64, | ||
training_num: int = 3, #64, | ||
test_num: int = 2, #10, | ||
rew_norm: bool = True, | ||
vf_coef: float = 0.25, | ||
ent_coef: float = 0.0, | ||
gae_lambda: float = 0.95, | ||
bound_action_method: Literal["clip", "tanh"] | None = "clip", | ||
lr_decay: bool = True, | ||
max_grad_norm: float = 0.5, | ||
eps_clip: float = 0.2, | ||
dual_clip: float | None = None, | ||
value_clip: bool = False, | ||
norm_adv: bool = False, | ||
recompute_adv: bool = True, | ||
): | ||
b = 5 | ||
log_name = os.path.join(task, "ppo", str(experiment_config.seed), datetime_tag()) | ||
|
||
sampling_config = SamplingConfig( | ||
num_epochs=epoch, | ||
step_per_epoch=step_per_epoch, | ||
batch_size=batch_size, | ||
num_train_envs=training_num, | ||
num_test_envs=test_num, | ||
buffer_size=buffer_size, | ||
step_per_collect=step_per_collect, | ||
repeat_per_collect=repeat_per_collect, | ||
) | ||
|
||
env_factory = MujocoEnvFactory(task, | ||
experiment_config.seed, | ||
obs_norm=True, | ||
train_seed_mechanism=experiment_config.train_seed_mechanism, | ||
test_seeds=experiment_config.test_seeds) | ||
|
||
experiment = ( | ||
PPOExperimentBuilder(env_factory, experiment_config, sampling_config) | ||
.with_ppo_params( | ||
PPOParams( | ||
discount_factor=gamma, | ||
gae_lambda=gae_lambda, | ||
action_bound_method=bound_action_method, | ||
reward_normalization=rew_norm, | ||
ent_coef=ent_coef, | ||
vf_coef=vf_coef, | ||
max_grad_norm=max_grad_norm, | ||
value_clip=value_clip, | ||
advantage_normalization=norm_adv, | ||
eps_clip=eps_clip, | ||
dual_clip=dual_clip, | ||
recompute_advantage=recompute_adv, | ||
lr=lr, | ||
lr_scheduler_factory=LRSchedulerFactoryLinear(sampling_config) | ||
if lr_decay | ||
else None, | ||
dist_fn=DistributionFunctionFactoryIndependentGaussians(), | ||
), | ||
) | ||
.with_actor_factory_default(hidden_sizes, torch.nn.Tanh, continuous_unbounded=True) | ||
.with_critic_factory_default(hidden_sizes, torch.nn.Tanh) | ||
.build() | ||
) | ||
experiment.run(log_name, experiment_config.record_seed_of_transition_to_buffer_test) | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.run_cli(main) |
This file contains hidden or 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,71 @@ | ||
from test.highlevel.env_factory import ContinuousTestEnvFactory, DiscreteTestEnvFactory | ||
|
||
import pytest | ||
|
||
from tianshou.highlevel.config import SamplingConfig | ||
from tianshou.highlevel.experiment import ( | ||
PPOExperimentBuilder, | ||
ExperimentConfig, | ||
EvaluationProtocolExperimentConfig, | ||
TrainSeedMechanism | ||
) | ||
from examples.mujoco.mujoco_env import MujocoEnvFactory | ||
|
||
|
||
def test_standard_experiment_config(): | ||
experiment_config = ExperimentConfig | ||
sampling_config = SamplingConfig( | ||
num_epochs=1, | ||
step_per_epoch=100, | ||
num_train_envs=2, | ||
num_test_envs=2, | ||
) | ||
env_factory = MujocoEnvFactory(task="Ant-v4", | ||
seed=experiment_config.seed, | ||
obs_norm=True,) | ||
|
||
ppo = PPOExperimentBuilder( | ||
experiment_config=experiment_config, | ||
env_factory=env_factory, | ||
sampling_config=sampling_config, | ||
) | ||
experiment = ppo.build() | ||
experiment.run("test") | ||
print(experiment) | ||
|
||
|
||
@pytest.mark.parametrize("experiment_config", | ||
[ | ||
EvaluationProtocolExperimentConfig( | ||
persistence_enabled=False, | ||
train_seed_mechanism=TrainSeedMechanism.CONSECUTIVE, | ||
test_seeds=(2,3)), | ||
EvaluationProtocolExperimentConfig(persistence_enabled=False, train_seed_mechanism=TrainSeedMechanism.REPEAT, | ||
test_seeds=(2,3)), | ||
EvaluationProtocolExperimentConfig(persistence_enabled=False, train_seed_mechanism=TrainSeedMechanism.NONE, | ||
test_seeds=(2,3)), | ||
|
||
], | ||
) | ||
def test_experiment_builder_continuous_default_params(experiment_config): | ||
sampling_config = SamplingConfig( | ||
num_epochs=1, | ||
step_per_epoch=100, | ||
num_train_envs=2, | ||
num_test_envs=2, | ||
) | ||
env_factory = MujocoEnvFactory(task="Ant-v4", | ||
seed=experiment_config.seed, | ||
obs_norm=True, | ||
train_seed_mechanism=experiment_config.train_seed_mechanism, | ||
test_seeds=experiment_config.test_seeds) | ||
|
||
ppo = PPOExperimentBuilder( | ||
experiment_config=experiment_config, | ||
env_factory=env_factory, | ||
sampling_config=sampling_config, | ||
) | ||
experiment = ppo.build() | ||
experiment.run("test", | ||
record_seed_of_transition_to_buffer_test=experiment_config.record_seed_of_transition_to_buffer_test) | ||
print(experiment) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forbid seed=None?