-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
209 lines (187 loc) · 7.11 KB
/
main.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
# Import necessary libraries
import argparse
import os
import pprint
import torch
import numpy as np
from datetime import datetime
from tianshou.data import Collector, VectorReplayBuffer, PrioritizedVectorReplayBuffer
from torch.utils.tensorboard import SummaryWriter
from tianshou.utils import TensorboardLogger
from tianshou.trainer import offpolicy_trainer
from torch.distributions import Independent, Normal
from tianshou.exploration import GaussianNoise
from env import make_aigc_env
from policy import DiffusionOPT
from diffusion import Diffusion
from diffusion.model import MLP, DoubleCritic
import warnings
# Ignore warnings
warnings.filterwarnings('ignore')
# Define a function to get command line arguments
def get_args():
# Create argument parser
parser = argparse.ArgumentParser()
parser.add_argument("--exploration-noise", type=float, default=0.1)
parser.add_argument('--algorithm', type=str, default='diffusion_opt')
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--buffer-size', type=int, default=1e6)#1e6
parser.add_argument('-e', '--epoch', type=int, default=1e6)# 1000
parser.add_argument('--step-per-epoch', type=int, default=1)# 100
parser.add_argument('--step-per-collect', type=int, default=1)#1000
parser.add_argument('-b', '--batch-size', type=int, default=512)
parser.add_argument('--wd', type=float, default=1e-4)
parser.add_argument('--gamma', type=float, default=1)
parser.add_argument('--n-step', type=int, default=3)
parser.add_argument('--training-num', type=int, default=1)
parser.add_argument('--test-num', type=int, default=1)
parser.add_argument('--logdir', type=str, default='log')
parser.add_argument('--log-prefix', type=str, default='default')
parser.add_argument('--render', type=float, default=0.1)
parser.add_argument('--rew-norm', type=int, default=0)
# parser.add_argument(
# '--device', type=str, default='cuda' if torch.cuda.is_available() else 'cpu')
parser.add_argument(
'--device', type=str, default='cuda:0')
parser.add_argument('--resume-path', type=str, default=None)
parser.add_argument('--watch', action='store_true', default=False)
parser.add_argument('--lr-decay', action='store_true', default=False)
parser.add_argument('--note', type=str, default='')
# for diffusion
parser.add_argument('--actor-lr', type=float, default=1e-4)
parser.add_argument('--critic-lr', type=float, default=1e-4)
parser.add_argument('--tau', type=float, default=0.005) # for soft update
# adjust
parser.add_argument('-t', '--n-timesteps', type=int, default=6) # for diffusion chain 3 & 8 & 12
parser.add_argument('--beta-schedule', type=str, default='vp',
choices=['linear', 'cosine', 'vp'])
# With Expert: bc-coef True
# Without Expert: bc-coef False
# parser.add_argument('--bc-coef', default=False) # Apr-04-132705
parser.add_argument('--bc-coef', default=False)
# for prioritized experience replay
parser.add_argument('--prioritized-replay', action='store_true', default=False)
parser.add_argument('--prior-alpha', type=float, default=0.4)#
parser.add_argument('--prior-beta', type=float, default=0.4)#
# Parse arguments and return them
args = parser.parse_known_args()[0]
return args
def main(args=get_args()):
# create environments
env, train_envs, test_envs = make_aigc_env(args.training_num, args.test_num)
args.state_shape = env.observation_space.shape[0]
args.action_shape = env.action_space.n
args.max_action = 1.
args.exploration_noise = args.exploration_noise * args.max_action
# seed
# np.random.seed(args.seed)
# torch.manual_seed(args.seed)
# train_envs.seed(args.seed)
# test_envs.seed(args.seed)
# create actor
actor_net = MLP(
state_dim=args.state_shape,
action_dim=args.action_shape
)
# Actor is a Diffusion model
actor = Diffusion(
state_dim=args.state_shape,
action_dim=args.action_shape,
model=actor_net,
max_action=args.max_action,
beta_schedule=args.beta_schedule,
n_timesteps=args.n_timesteps,
bc_coef = args.bc_coef
).to(args.device)
actor_optim = torch.optim.AdamW(
actor.parameters(),
lr=args.actor_lr,
weight_decay=args.wd
)
# Create critic
critic = DoubleCritic(
state_dim=args.state_shape,
action_dim=args.action_shape
).to(args.device)
critic_optim = torch.optim.AdamW(
critic.parameters(),
lr=args.critic_lr,
weight_decay=args.wd
)
## Setup logging
time_now = datetime.now().strftime('%b%d-%H%M%S')
log_path = os.path.join(args.logdir, args.log_prefix, "diffusion", time_now)
writer = SummaryWriter(log_path)
writer.add_text("args", str(args))
logger = TensorboardLogger(writer)
# def dist(*logits):
# return Independent(Normal(*logits), 1)
# Define policy
policy = DiffusionOPT(
args.state_shape,
actor,
actor_optim,
args.action_shape,
critic,
critic_optim,
# dist,
args.device,
tau=args.tau,
gamma=args.gamma,
estimation_step=args.n_step,
lr_decay=args.lr_decay,
lr_maxt=args.epoch,
bc_coef=args.bc_coef,
action_space=env.action_space,
exploration_noise = args.exploration_noise,
)
# Load a previous policy if a path is provided
if args.resume_path:
ckpt = torch.load(args.resume_path, map_location=args.device)
policy.load_state_dict(ckpt)
print("Loaded agent from: ", args.resume_path)
# Setup buffer
if args.prioritized_replay:
buffer = PrioritizedVectorReplayBuffer(
args.buffer_size,
buffer_num=len(train_envs),
alpha=args.prior_alpha,
beta=args.prior_beta,
)
else:
buffer = VectorReplayBuffer(
args.buffer_size,
buffer_num=len(train_envs)
)
# Setup collector
train_collector = Collector(policy, train_envs, buffer)
test_collector = Collector(policy, test_envs)
def save_best_fn(policy):
torch.save(policy.state_dict(), os.path.join(log_path, 'policy.pth'))
# Trainer
if not args.watch:
result = offpolicy_trainer(
policy,
train_collector,
test_collector,
args.epoch,
args.step_per_epoch,
args.step_per_collect,
args.test_num,
args.batch_size,
save_best_fn=save_best_fn,
logger=logger,
test_in_train=False
)
pprint.pprint(result)
# Watch the performance
# python main.py --watch --resume-path log/default/diffusion/Jul10-142653/policy.pth
if __name__ == '__main__':
policy.eval()
collector = Collector(policy, env)
result = collector.collect(n_episode=1) #, render=args.render
print(result)
rews, lens = result["rews"], result["lens"]
print(f"Final reward: {rews.mean()}, length: {lens.mean()}")
if __name__ == '__main__':
main(get_args())