-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmujoco_sac.py
More file actions
executable file
·199 lines (179 loc) · 6.33 KB
/
mujoco_sac.py
File metadata and controls
executable file
·199 lines (179 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
import datetime
import os
import pprint
import numpy as np
import torch
from mujoco_env import make_mujoco_env
from sensai.util import logging
from tianshou.algorithm import SAC
from tianshou.algorithm.algorithm_base import Algorithm
from tianshou.algorithm.modelfree.sac import AutoAlpha, SACPolicy
from tianshou.algorithm.optim import AdamOptimizerFactory
from tianshou.data import Collector, CollectStats, ReplayBuffer, VectorReplayBuffer
from tianshou.highlevel.logger import LoggerFactoryDefault
from tianshou.trainer import OffPolicyTrainerParams
from tianshou.utils.net.common import Net
from tianshou.utils.net.continuous import ContinuousActorProbabilistic, ContinuousCritic
log = logging.getLogger(__name__)
def main(
task: str = "Ant-v4",
persistence_base_dir: str = "log",
seed: int = 0,
buffer_size: int = 1000000,
hidden_sizes: list | None = None,
actor_lr: float = 1e-3,
critic_lr: float = 1e-3,
gamma: float = 0.99,
tau: float = 0.005,
alpha: float = 0.2,
auto_alpha: bool = False,
alpha_lr: float = 3e-4,
start_timesteps: int = 10000,
epoch: int = 50,
epoch_num_steps: int = 5000,
collection_step_num_env_steps: int = 1,
update_per_step: int = 1,
n_step: int = 1,
batch_size: int = 256,
num_training_envs: int = 1,
num_test_envs: int = 10,
render: float = 0.0,
device: str | None = None,
resume_path: str | None = None,
resume_id: str | None = None,
logger_type: str = "tensorboard",
wandb_project: str = "mujoco.benchmark",
watch: bool = False,
) -> None:
# Set defaults for mutable arguments
if hidden_sizes is None:
hidden_sizes = [256, 256]
if device is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
# Get all local variables as config
params_log_info = locals()
log.info(f"Starting training with config:\n{params_log_info}")
env, training_envs, test_envs = make_mujoco_env(
task,
seed,
num_training_envs,
num_test_envs,
obs_norm=False,
)
state_shape = env.observation_space.shape or env.observation_space.n
action_shape = env.action_space.shape or env.action_space.n
max_action = env.action_space.high[0]
log.info(f"Observations shape: {state_shape}")
log.info(f"Actions shape: {action_shape}")
log.info(f"Action range: {np.min(env.action_space.low)}, {np.max(env.action_space.high)}")
# seed
np.random.seed(seed)
torch.manual_seed(seed)
# model
net_a = Net(state_shape=state_shape, hidden_sizes=hidden_sizes)
actor = ContinuousActorProbabilistic(
preprocess_net=net_a,
action_shape=action_shape,
unbounded=True,
conditioned_sigma=True,
).to(device)
actor_optim = AdamOptimizerFactory(lr=actor_lr)
net_c1 = Net(
state_shape=state_shape,
action_shape=action_shape,
hidden_sizes=hidden_sizes,
concat=True,
)
net_c2 = Net(
state_shape=state_shape,
action_shape=action_shape,
hidden_sizes=hidden_sizes,
concat=True,
)
critic1 = ContinuousCritic(preprocess_net=net_c1).to(device)
critic1_optim = AdamOptimizerFactory(lr=critic_lr)
critic2 = ContinuousCritic(preprocess_net=net_c2).to(device)
critic2_optim = AdamOptimizerFactory(lr=critic_lr)
if auto_alpha:
target_entropy = -np.prod(env.action_space.shape)
log_alpha = 0.0
alpha_optim = AdamOptimizerFactory(lr=alpha_lr)
alpha = AutoAlpha(target_entropy, log_alpha, alpha_optim).to(device) # type: ignore
policy = SACPolicy(
actor=actor,
action_space=env.action_space,
)
algorithm: SAC = SAC(
policy=policy,
policy_optim=actor_optim,
critic=critic1,
critic_optim=critic1_optim,
critic2=critic2,
critic2_optim=critic2_optim,
tau=tau,
gamma=gamma,
alpha=alpha,
n_step_return_horizon=n_step,
)
# load a previous policy
if resume_path:
algorithm.load_state_dict(torch.load(resume_path, map_location=device))
log.info(f"Loaded agent from: {resume_path}")
# collector
buffer: VectorReplayBuffer | ReplayBuffer
if num_training_envs > 1:
buffer = VectorReplayBuffer(buffer_size, len(training_envs))
else:
buffer = ReplayBuffer(buffer_size)
training_collector = Collector[CollectStats](
algorithm, training_envs, buffer, exploration_noise=True
)
test_collector = Collector[CollectStats](algorithm, test_envs)
training_collector.reset()
training_collector.collect(n_step=start_timesteps, random=True)
# log
now = datetime.datetime.now().strftime("%y%m%d-%H%M%S")
algo_name = "sac"
log_name = os.path.join(task, algo_name, str(seed), now)
log_path = os.path.join(persistence_base_dir, log_name)
# logger
logger_factory = LoggerFactoryDefault()
if logger_type == "wandb":
logger_factory.logger_type = "wandb"
logger_factory.wandb_project = wandb_project
else:
logger_factory.logger_type = "tensorboard"
logger = logger_factory.create_logger(
log_dir=log_path,
experiment_name=log_name,
run_id=resume_id,
config_dict=params_log_info,
)
def save_best_fn(policy: Algorithm) -> None:
torch.save(policy.state_dict(), os.path.join(log_path, "policy.pth"))
if not watch:
# train
result = algorithm.run_training(
OffPolicyTrainerParams(
training_collector=training_collector,
test_collector=test_collector,
max_epochs=epoch,
epoch_num_steps=epoch_num_steps,
collection_step_num_env_steps=collection_step_num_env_steps,
test_step_num_episodes=num_test_envs,
batch_size=batch_size,
save_best_fn=save_best_fn,
logger=logger,
update_step_num_gradient_steps_per_sample=update_per_step,
test_in_training=False,
)
)
pprint.pprint(result)
# Let's watch its performance!
test_envs.seed(seed)
test_collector.reset()
collector_stats = test_collector.collect(n_episode=num_test_envs, render=render)
log.info(collector_stats)
if __name__ == "__main__":
result = logging.run_cli(main, level=logging.INFO)