-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathtrain_tf1.py
171 lines (145 loc) · 6.05 KB
/
train_tf1.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
import numpy as np
import gym
import os, sys
from tf1_arguments import get_args
from mpi4py import MPI
from subprocess import CalledProcessError
import time
from spinup_utils.logx import setup_logger_kwargs, colorize
from spinup_utils.logx import EpochLogger
from spinup_utils.print_logger import Logger
"""
train the agent, the MPI part code is copy from openai baselines(https://github.com/openai/baselines/blob/master/baselines/her)
but I ignore it~
"""
def obs2state(obs, key_list=['observation', 'desired_goal']):
if type(obs) == dict:
s = np.concatenate(([obs[key] for key in key_list]
))
elif type(obs) == np.ndarray:
s = obs[:]
else:
s = obs[:]
return s
def trainer(net, env, args):
# logger
exp_name = args.exp_name+'_'+args.RL_name+'_'+args.env_name
logger_kwargs = setup_logger_kwargs(exp_name=exp_name,
seed=args.seed,
output_dir=args.output_dir + "/")
logger = EpochLogger(**logger_kwargs)
sys.stdout = Logger(logger_kwargs["output_dir"] + "print.log",
sys.stdout)
logger.save_config(locals(), __file__)
# start trainning
start_time = time.time()
for i in range(args.n_epochs):
for c in range(args.n_cycles):
obs = env.reset()
episode_trans = []
s = obs2state(obs)
ep_reward = 0
real_ep_reward = 0
episode_time = time.time()
success = []
for j in range(args.n_steps):
a = net.get_action(s, noise_scale=args.noise_ps)
if np.random.random() < args.random_eps:
a = np.random.uniform(low=-net.a_bound,
high=net.a_bound,
size=net.act_dim)
a = np.clip(a, -net.a_bound, net.a_bound)
try:
obs_next, r, done, info = env.step(a)
success.append(info["is_success"])
except Exception as e:
success.append(int(done))
s_ = obs2state(obs_next)
# visualization
if args.render and i % 3 == 0 and c % 20 == 0:
env.render()
done = False if j == args.n_steps - 1 else done
if not args.her:
net.store_transition((s, a, r, s_, done))
episode_trans.append([obs, a, r, obs_next, done, info])
s = s_
obs = obs_next
ep_reward += r
real_ep_reward += r
if args.her:
net.save_episode(episode_trans=episode_trans,
reward_func=env.compute_reward,
obs2state=obs2state)
logger.store(EpRet=ep_reward)
logger.store(EpRealRet=real_ep_reward)
for _ in range(40):
outs = net.learn(args.batch_size,
args.base_lr,
args.base_lr * 2,
)
if outs[1] is not None:
logger.store(Q1=outs[1])
logger.store(Q2=outs[2])
if 0.0 < sum(success) < args.n_steps:
print("epoch:", i,
"\tep:", c,
"\tep_rew:", ep_reward,
"\ttime:", np.round(time.time()-episode_time, 3),
'\tdone:', sum(success))
test_ep_reward, logger = net.test_agent(args=args,
env=env,
n=10,
logger=logger,
obs2state=obs2state,
)
logger.store(TestEpRet=test_ep_reward)
logger.log_tabular('Epoch', i)
logger.log_tabular('EpRet', average_only=True)
logger.log_tabular('EpRealRet', average_only=True)
logger.log_tabular('TestEpRet', average_only=True)
logger.log_tabular('Q1', with_min_and_max=True)
logger.log_tabular('Q2', average_only=True)
logger.log_tabular('TestSuccess', average_only=True)
logger.log_tabular('TotalEnvInteracts', i * args.n_cycles * args.n_steps + c * args.n_steps + j + 1)
logger.log_tabular('TotalTime', time.time() - start_time)
logger.dump_tabular()
print(colorize("the experience %s is end" % logger.output_file.name,
'green', bold=True))
net.save_simple_network(logger_kwargs["output_dir"])
def launch(net, args):
env = gym.make(args.env_name)
env.seed(args.seed)
np.random.seed(args.seed)
try:
s_dim = env.observation_space.shape[0]
except:
s_dim = env.observation_space.spaces['observation'].shape[0] + \
env.observation_space.spaces['desired_goal'].shape[0]
act_dim = env.action_space.shape[0]
a_bound = env.action_space.high[0]
net = net(act_dim=act_dim,
obs_dim=s_dim,
a_bound=a_bound,
per_flag=args.per,
her_flag=args.her,
action_l2=args.action_l2,
state_norm=args.state_norm,
gamma=args.gamma,
sess_opt=args.sess_opt,
seed=args.seed,
clip_return=args.clip_return,
)
trainer(net, env, args)
if __name__ == '__main__':
os.environ['OMP_NUM_THREADS'] = '1'
os.environ['MKL_NUM_THREADS'] = '1'
os.environ['IN_MPI'] = '1'
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
# get the params
args = get_args()
from algos.tf1.td3_sp.TD3_per_her import TD3
from algos.tf1.ddpg_sp.DDPG_per_her import DDPG
from algos.tf1.sac_sp.SAC_per_her import SAC
from algos.tf1.sac_auto.sac_auto_per_her import SAC_AUTO
RL_list = [TD3, DDPG, SAC, SAC_AUTO]
[launch(net=net, args=args) for net in RL_list if net.__name__ == args.RL_name]