-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
226 lines (196 loc) · 7.84 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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.tf_dynamics_models.constructor import construct_model
from models.policy_models import MLP, ActorProb, Critic, DiagGaussian
from sac import SACPolicy
from mopo import MOPO
from buffer import ReplayBuffer
from logger import Logger
from trainer import Trainer
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = "3"
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
#os.environ["CUDA_VISIBLE_DEVICES"]= "3"
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--algo-name", type=str, default="cnt-mopo-sig")
parser.add_argument("--task", type=str, default="hopper-medium-replay-v0")
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)
parser.add_argument("--bit", type=int, default=8)
parser.add_argument("--cnt_type", type=str, default="AVG")
parser.add_argument("--cnt_coef", type = float, default=1)
parser.add_argument("--use_count", type=str2bool, default=True)
# dynamics model's arguments
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=5)
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)
if args.seed == 1:
args.seed = np.random.randint(1000)
# 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)
# 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 = construct_model(
obs_dim=np.prod(args.obs_shape),
act_dim=args.action_dim,
bit=args.bit,
hidden_dim=200,
num_networks=args.n_ensembles,
num_elites=args.n_elites,
model_type="mlp",
separate_mean_var=True,
use_count=args.use_count,
load_dir=args.dynamics_model_dir
)
# 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
task = args.task.split('-')[0]
import_path = f"static_fns.{task}"
static_fns = importlib.import_module(import_path).StaticFns
algo = MOPO(
sac_policy,
dynamics_model,
static_fns=static_fns,
offline_buffer=offline_buffer,
model_buffer=model_buffer,
reward_penalty_coef=args.reward_penalty_coef,
cnt_coef=args.cnt_coef,
rollout_length=args.rollout_length,
rollout_batch_size=args.rollout_batch_size,
batch_size=args.batch_size,
real_ratio=args.real_ratio,
use_count=args.use_count,
cnt_type=args.cnt_type,
)
# log
t0 = datetime.datetime.now().strftime("%m%d_%H%M%S")
if args.use_count:
log_file = f'{args.rollout_length}_{args.reward_penalty_coef}_{args.cnt_coef}_{args.bit}_{t0}_seed_{args.seed}-{args.task.replace("-", "_")}_{args.algo_name}'
log_folder = f'{args.algo_name}-{args.cnt_type}'
else:
log_file = f'{args.rollout_length}_{args.reward_penalty_coef}_{t0}_seed_{args.seed}-{args.task.replace("-", "_")}_{args.algo_name.split("-")[-1]}'
log_folder = f'{args.algo_name.split("-")[-1]}'
log_path = os.path.join(args.logdir, args.task, log_folder, log_file)
writer = SummaryWriter(log_path)
writer.add_text("args", str(args))
logger = Logger(writer)
# create trainer
trainer = Trainer(
algo,
eval_env=env,
task=args.task,
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()