-
Notifications
You must be signed in to change notification settings - Fork 6
/
train.py
198 lines (170 loc) · 6.92 KB
/
train.py
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
import argparse
import datetime
import os
import random
import importlib
import gym
import d4rl
import numpy as np
import torch
from torch.utils.tensorboard import SummaryWriter
from models.transition_model import TransitionModel
from models.policy_models import MLP, ActorProb, Critic, DiagGaussian
from algo.sac import SACPolicy
from algo.mopo import MOPO
from common.buffer import ReplayBuffer
from common.logger import Logger
from trainer import Trainer
from common.util import set_device_and_logger
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--algo-name", type=str, default="mopo")
parser.add_argument("--task", type=str, default="walker2d-medium-replay-v2")
parser.add_argument("--seed", type=int, default=1)
parser.add_argument("--actor-lr", type=float, default=3e-4)
parser.add_argument("--critic-lr", type=float, default=3e-4)
parser.add_argument("--gamma", type=float, default=0.99)
parser.add_argument("--tau", type=float, default=0.005)
parser.add_argument("--alpha", type=float, default=0.2)
parser.add_argument('--auto-alpha', default=True)
parser.add_argument('--target-entropy', type=int, default=-3)
parser.add_argument('--alpha-lr', type=float, default=3e-4)
# dynamics model's arguments
parser.add_argument("--dynamics-lr", type=float, default=0.001)
parser.add_argument("--n-ensembles", type=int, default=7)
parser.add_argument("--n-elites", type=int, default=5)
parser.add_argument("--reward-penalty-coef", type=float, default=1.0)
parser.add_argument("--rollout-length", type=int, default=1)
parser.add_argument("--rollout-batch-size", type=int, default=50000)
parser.add_argument("--rollout-freq", type=int, default=1000)
parser.add_argument("--model-retain-epochs", type=int, default=5)
parser.add_argument("--real-ratio", type=float, default=0.05)
parser.add_argument("--dynamics-model-dir", type=str, default=None)
parser.add_argument("--epoch", type=int, default=1000)
parser.add_argument("--step-per-epoch", type=int, default=1000)
parser.add_argument("--eval_episodes", type=int, default=10)
parser.add_argument("--batch-size", type=int, default=256)
parser.add_argument("--logdir", type=str, default="log")
parser.add_argument("--log-freq", type=int, default=1000)
parser.add_argument("--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu")
return parser.parse_args()
def train(args=get_args()):
# create env and dataset
env = gym.make(args.task)
dataset = d4rl.qlearning_dataset(env)
args.obs_shape = env.observation_space.shape
args.action_dim = np.prod(env.action_space.shape)
# seed
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.device != "cpu":
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
env.seed(args.seed)
# log
t0 = datetime.datetime.now().strftime("%m%d_%H%M%S")
log_file = f'seed_{args.seed}_{t0}-{args.task.replace("-", "_")}_{args.algo_name}'
log_path = os.path.join(args.logdir, args.task, args.algo_name, log_file)
writer = SummaryWriter(log_path)
writer.add_text("args", str(args))
logger = Logger(writer=writer,log_path=log_path)
Devid = 0 if args.device == 'cuda' else -1
set_device_and_logger(Devid,logger)
# import configs
task = args.task.split('-')[0]
import_path = f"static_fns.{task}"
static_fns = importlib.import_module(import_path).StaticFns
config_path = f"config.{task}"
config = importlib.import_module(config_path).default_config
# create policy model
actor_backbone = MLP(input_dim=np.prod(args.obs_shape), hidden_dims=[256, 256])
critic1_backbone = MLP(input_dim=np.prod(args.obs_shape) + args.action_dim, hidden_dims=[256, 256])
critic2_backbone = MLP(input_dim=np.prod(args.obs_shape) + args.action_dim, hidden_dims=[256, 256])
dist = DiagGaussian(
latent_dim=getattr(actor_backbone, "output_dim"),
output_dim=args.action_dim,
unbounded=True,
conditioned_sigma=True
)
actor = ActorProb(actor_backbone, dist, args.device)
critic1 = Critic(critic1_backbone, args.device)
critic2 = Critic(critic2_backbone, args.device)
actor_optim = torch.optim.Adam(actor.parameters(), lr=args.actor_lr)
critic1_optim = torch.optim.Adam(critic1.parameters(), lr=args.critic_lr)
critic2_optim = torch.optim.Adam(critic2.parameters(), lr=args.critic_lr)
if args.auto_alpha:
target_entropy = args.target_entropy if args.target_entropy \
else -np.prod(env.action_space.shape)
args.target_entropy = target_entropy
log_alpha = torch.zeros(1, requires_grad=True, device=args.device)
alpha_optim = torch.optim.Adam([log_alpha], lr=args.alpha_lr)
args.alpha = (target_entropy, log_alpha, alpha_optim)
# create policy
sac_policy = SACPolicy(
actor,
critic1,
critic2,
actor_optim,
critic1_optim,
critic2_optim,
action_space=env.action_space,
dist=dist,
tau=args.tau,
gamma=args.gamma,
alpha=args.alpha,
device=args.device
)
# create dynamics model
dynamics_model = TransitionModel(obs_space=env.observation_space,
action_space=env.action_space,
static_fns=static_fns,
lr=args.dynamics_lr,
**config["transition_params"]
)
# create buffer
offline_buffer = ReplayBuffer(
buffer_size=len(dataset["observations"]),
obs_shape=args.obs_shape,
obs_dtype=np.float32,
action_dim=args.action_dim,
action_dtype=np.float32
)
offline_buffer.load_dataset(dataset)
model_buffer = ReplayBuffer(
buffer_size=args.rollout_batch_size * args.rollout_length * args.model_retain_epochs,
obs_shape=args.obs_shape,
obs_dtype=np.float32,
action_dim=args.action_dim,
action_dtype=np.float32
)
# create MOPO algo
algo = MOPO(
sac_policy,
dynamics_model,
offline_buffer=offline_buffer,
model_buffer=model_buffer,
reward_penalty_coef=args.reward_penalty_coef,
rollout_length=args.rollout_length,
batch_size=args.batch_size,
real_ratio=args.real_ratio,
logger=logger,
**config["mopo_params"]
)
# create trainer
trainer = Trainer(
algo,
eval_env=env,
epoch=args.epoch,
step_per_epoch=args.step_per_epoch,
rollout_freq=args.rollout_freq,
logger=logger,
log_freq=args.log_freq,
eval_episodes=args.eval_episodes
)
# pretrain dynamics model on the whole dataset
trainer.train_dynamics()
# begin train
trainer.train_policy()
if __name__ == "__main__":
train()