Views
No views yet
1from huggingface_sb3 import load_from_hub
2from sb3_contrib import TQC
3from stable_baselines3.common.env_util import make_vec_env
4from stable_baselines3.common.evaluation import evaluate_policy
5
6# Download checkpoint
7checkpoint = load_from_hub("araffin/tqc-BipedalWalker-v3", "tqc-BipedalWalker-v3.zip")
8# Load the model
9model = TQC.load(checkpoint)
10
11env = make_vec_env("BipedalWalker-v3", n_envs=1)
12
13# Evaluate
14print("Evaluating model")
15mean_reward, std_reward = evaluate_policy(
16 model,
17 env,
18 n_eval_episodes=20,
19 deterministic=True,
20)
21print(f"Mean reward = {mean_reward:.2f} +/- {std_reward:.2f}")
22
23# Start a new episode
24obs = env.reset()
25
26try:
27 while True:
28 action, _states = model.predict(obs, deterministic=True)
29 obs, rewards, dones, info = env.step(action)
30 env.render()
31except KeyboardInterrupt:
32 pass1from sb3_contrib import TQC
2from stable_baselines3.common.env_util import make_vec_env
3from stable_baselines3.common.callbacks import EvalCallback
4
5# Create the environment
6env_id = "BipedalWalker-v3"
7n_envs = 2
8env = make_vec_env(env_id, n_envs=n_envs)
9
10# Create the evaluation envs
11eval_envs = make_vec_env(env_id, n_envs=5)
12
13# Adjust evaluation interval depending on the number of envs
14eval_freq = int(1e5)
15eval_freq = max(eval_freq // n_envs, 1)
16
17# Create evaluation callback to save best model
18# and monitor agent performance
19eval_callback = EvalCallback(
20 eval_envs,
21 best_model_save_path="./logs/",
22 eval_freq=eval_freq,
23 n_eval_episodes=10,
24)
25
26# Instantiate the agent
27# Hyperparameters from https://github.com/DLR-RM/rl-baselines3-zoo
28model = TQC(
29 "MlpPolicy",
30 env,
31 learning_starts=10000,
32 batch_size=256,
33 buffer_size=300000,
34 learning_rate=7.3e-4,
35 # gSDE is from https://proceedings.mlr.press/v164/raffin22a.html
36 use_sde=True,
37 train_freq=8,
38 gradient_steps=8,
39 gamma=0.98,
40 tau=0.02,
41 policy_kwargs=dict(log_std_init=-3, net_arch=[400, 300]),
42 verbose=1,
43)
44
45# Train the agent (you can kill it before using ctrl+c)
46try:
47 model.learn(total_timesteps=int(5e5), callback=eval_callback, log_interval=10)
48except KeyboardInterrupt:
49 pass