-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
37 changed files
with
456 additions
and
224 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
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
Empty file.
Binary file not shown.
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,80 @@ | ||
import numpy as np | ||
import optuna | ||
import torch | ||
|
||
from genrl.agents.a2c.a2c import A2C | ||
from genrl.environments.suite import VectorEnv | ||
from genrl.trainers.onpolicy import OnPolicyTrainer | ||
|
||
env = VectorEnv("CartPole-v0") | ||
|
||
|
||
def tune_A2C(trial): | ||
# Define hyperparameters that are relevant for training | ||
# Choose a suggestion type and range (float/int and log/uniform) | ||
lr_value = trial.suggest_float("lr_value", 1e-5, 1e-2, log=True) | ||
lr_policy = trial.suggest_float("lr_policy", 1e-5, 1e-2, log=True) | ||
rollout_size = trial.suggest_int("rollout_size", 100, 10000, log=True) | ||
entropy_coeff = trial.suggest_float("entropy_coeff", 5e-4, 2e-1, log=True) | ||
|
||
agent = A2C( | ||
"mlp", | ||
env, | ||
lr_value=lr_value, | ||
lr_policy=lr_policy, | ||
rollout_size=rollout_size, | ||
entropy_coeff=entropy_coeff, | ||
) | ||
trainer = OnPolicyTrainer( | ||
agent, env, log_interval=10, epochs=100, evaluate_episodes=10, | ||
) | ||
trainer.train() | ||
|
||
episode, episode_reward = 0, np.zeros(trainer.env.n_envs) | ||
episode_rewards = [] | ||
state = trainer.env.reset() | ||
while True: | ||
if trainer.off_policy: | ||
action = trainer.agent.select_action(state, deterministic=True) | ||
else: | ||
action, _, _ = trainer.agent.select_action(state) | ||
|
||
if isinstance(action, torch.Tensor): | ||
action = action.numpy() | ||
|
||
next_state, reward, done, _ = trainer.env.step(action) | ||
|
||
episode_reward += reward | ||
state = next_state | ||
if np.any(done): | ||
for i, di in enumerate(done): | ||
if di: | ||
episode += 1 | ||
episode_rewards.append(episode_reward[i]) | ||
episode_reward[i] = 0 | ||
if episode == trainer.evaluate_episodes: | ||
print( | ||
"Evaluated for {} episodes, Mean Reward: {}, Std Deviation for the Reward: {}".format( | ||
trainer.evaluate_episodes, | ||
np.mean(episode_rewards), | ||
np.std(episode_rewards), | ||
) | ||
) | ||
break | ||
|
||
return np.mean(episode_rewards) | ||
|
||
|
||
agent_name = "A2C" # replace | ||
study_name = "{}-3".format(agent_name) | ||
study = optuna.create_study( | ||
study_name=study_name, | ||
direction="maximize", | ||
storage="sqlite:///{}.db".format(study_name), | ||
# load_if_exists=True | ||
) | ||
study.optimize(tune_A2C, n_trials=20) | ||
|
||
print("Best Trial Results:") | ||
for key, value in study.best_trial.__dict__.items(): | ||
print("{} : {}".format(key, value)) |
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,13 @@ | ||
import optuna | ||
|
||
path_name = "A2C-CartPole-v0-ep100" | ||
study = optuna.create_study( | ||
study_name=path_name, | ||
direction="maximize", | ||
storage="sqlite:///{}.db".format(path_name), | ||
load_if_exists=True, | ||
) | ||
|
||
print("Best Trial Results:") | ||
for key, value in study.best_trial.__dict__.items(): | ||
print("{} : {}".format(key, value)) |
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,79 @@ | ||
import numpy as np | ||
import optuna | ||
import torch | ||
|
||
from genrl.agents.td3.td3 import TD3 | ||
from genrl.environments.suite import VectorEnv | ||
from genrl.trainers.offpolicy import OffPolicyTrainer | ||
|
||
env = VectorEnv("Pendulum-v0") | ||
|
||
|
||
def objective(trial): | ||
lr_value = trial.suggest_float("lr_value", 1e-6, 1e-1, log=True) | ||
lr_policy = trial.suggest_float("lr_policy", 1e-6, 1e-1, log=True) | ||
replay_size = trial.suggest_int("replay_size", 1e2, 1e5, log=True) | ||
max_ep_len = trial.suggest_int("max_ep_len", 1e3, 50000, log=True) | ||
|
||
agent = TD3( | ||
"mlp", env, lr_value=lr_value, lr_policy=lr_policy, replay_size=replay_size | ||
) | ||
trainer = OffPolicyTrainer( | ||
agent, | ||
env, | ||
log_interval=5, | ||
epochs=100, | ||
max_timesteps=16500, | ||
evaluate_episodes=10, | ||
max_ep_len=max_ep_len, | ||
) | ||
trainer.train() | ||
|
||
episode = 0 | ||
episode_rewards = [] | ||
state = trainer.env.reset() | ||
|
||
while True: | ||
if trainer.off_policy: | ||
action = trainer.agent.select_action(state, deterministic=True) | ||
else: | ||
action, _, _ = trainer.agent.select_action(state) | ||
|
||
if isinstance(action, torch.Tensor): | ||
action = action.numpy() | ||
|
||
next_state, reward, done, _ = trainer.env.step(action) | ||
|
||
state = next_state | ||
|
||
if np.any(done): | ||
for i, di in enumerate(done): | ||
if di: | ||
episode += 1 | ||
episode_rewards.append(trainer.env.episode_reward[i]) | ||
trainer.env.episode_reward[i] = 0 | ||
|
||
if episode == trainer.evaluate_episodes: | ||
eval_reward = float(np.mean(episode_rewards)) | ||
|
||
trial.report(eval_reward, int(episode)) | ||
break | ||
|
||
return eval_reward | ||
|
||
|
||
study = optuna.create_study( | ||
study_name="1", | ||
direction="maximize", | ||
storage="sqlite:///td3--pendulum-v0--replay_size-max_ep_len-lr_value-lr_policy.db", | ||
load_if_exists=True, | ||
) | ||
study.optimize(objective, n_trials=20) | ||
df = study.trials_dataframe(attrs=("number", "value", "params")) | ||
df.to_pickle("logs/optuna_logs.pkl") | ||
|
||
print("Best trial: ") | ||
for key, value in study.best_trial.__dict__.items(): | ||
print("{}: {}".format(key, value)) | ||
print("Eval Reward: ", study.best_value) | ||
print("Params: ", study.best_params) |
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
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
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
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
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
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
Oops, something went wrong.