-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
159 lines (132 loc) · 4.79 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
import argparse
import numpy as np
import tensorflow as tf
from controller import Controller
import os
import threading
import time
import random
def delete_file(path):
ls = os.listdir(path)
for i in ls:
c_path = os.path.join(path, i)
if os.path.isdir(c_path):
delete_file(c_path)
os.rmdir(c_path)
else:
os.remove(c_path)
def run_an_exp(env = None,exp_id = 0,epsilon=0):
os.system('python main.py --config=config.%s --exp_id=%d --epsilon=%f' %(env,exp_id,epsilon))
#Set kwargs for hyper-controller training
parser = argparse.ArgumentParser()
parser.add_argument('--env', type=str, default='hopper')
args = parser.parse_args()
env = args.env
STAGE_NUM = {
'hopper': int(10),
'ant': int(10),
'humanoid': int(20),
'hopperbullet': int(20),
'walker2dbullet': int(20),
'halfcheetahbullet':int(10),
}[env]
EPSILON_SCHEDULE = {
'hopper': [0,5,1,0],
'ant': [0,5,1,0],
'humanoid': [0,5,1,0],
'hopperbullet': [0,5,0,0],
'walker2dbullet': [0,5,0,0],
'halfcheetahbullet':[0,5,0,0],
}[env]
DOMAIN = {
'hopper': 'Hopper',
'ant': 'Ant',
'humanoid': 'Humanoid',
'hopperbullet': 'HopperBulletEnv',
'walker2dbullet': 'Walker2DBulletEnv',
'halfcheetahbullet':'HalfCheetahBulletEnv',
}[env]
HYPERPARAMETERS_STES = {
'Hopper': [['model', 'ratio']],
'Ant': [['model', 'ratio']],
'Humanoid': [['model', 'ratio']],
'HopperBulletEnv': [['model', 'policy','ratio']],
'Walker2DBulletEnv': [['model', 'policy','ratio']],
'HalfCheetahBulletEnv': [['model', 'policy','ratio']],
}[DOMAIN]
CONTROLLERS_INIT = {
'Hopper': [True, False, False],
'Ant': [False, False, False],
'Humanoid': [True, False, False],
'HopperBulletEnv': [False],
'Walker2DBulletEnv': [False],
'HalfCheetahBulletEnv': [False],
}[DOMAIN]
EPISODE_PER_STAGE = 10
UPDATE_PER_EPISODE = 30
BATCH_SIZE = 64
#create folder to store the trained model and temp data
model_path = 'saved-models/' + DOMAIN + '/controller0'
buffer_path = 'buffer'
log_path = 'log/' + DOMAIN
if not os.path.exists(model_path):
os.makedirs(model_path)
if not os.path.exists(buffer_path):
os.makedirs(buffer_path)
if not os.path.exists(log_path):
os.makedirs(log_path)
delete_file(model_path)
delete_file(buffer_path)
current_model_path = model_path + '/current'
best_model_path = model_path + '/best'
hyperparameters = HYPERPARAMETERS_STES[0]
controllers_init = CONTROLLERS_INIT[0]
state_dim = 4 + len(hyperparameters)
controller_graph = tf.Graph()
config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)
config.gpu_options.allow_growth = True
controller_session = tf.InteractiveSession(config=config,graph=controller_graph)
action_dim = len(hyperparameters)
action_space = []
for hyperparameeter in hyperparameters:
if (hyperparameeter == 'model'):
action_space.append(2)
else:
action_space.append(3)
controller = Controller(state_dim=state_dim, action_dim=action_dim, action_space=action_space,
hyperparameters=hyperparameters,init = controllers_init,
graph=controller_graph,session=controller_session)
controller.save(path=current_model_path)
best_mbrl_return = -1e9
threads = []
for stage in range(STAGE_NUM):
min_stage, max_stage, max_epsilon, min_epsilon = EPSILON_SCHEDULE
dx = (stage - min_stage) / (max_stage - min_stage)
y = dx * (min_epsilon - max_epsilon) + max_epsilon
epsilon = max(y,0)
for i in range(EPISODE_PER_STAGE):
job = lambda: run_an_exp(env=env,exp_id=stage*EPISODE_PER_STAGE+i,epsilon=epsilon)
t = threading.Thread(target=job)
t.start()
threads.append(t)
time.sleep(random.randint(30,60))
for t in threads:
t.join()
mbrl_returns = []
for i in range(EPISODE_PER_STAGE):
# read data from the buffer
if(os.path.exists('./buffer/states_%d.npy' % (stage*EPISODE_PER_STAGE+i))):
states = np.load('./buffer/states_%d.npy' % (stage*EPISODE_PER_STAGE+i))
actions = np.load('./buffer/actions_%d.npy' % (stage*EPISODE_PER_STAGE+i))
advs = np.load('./buffer/advs_%d.npy' % (stage*EPISODE_PER_STAGE+i))
logps = np.load('./buffer/logps_%d.npy' % (stage*EPISODE_PER_STAGE+i))
controller.store(states, actions, advs, logps)
mbrl_return = np.load('./buffer/mbrl_returns_%d.npy' % (stage*EPISODE_PER_STAGE+i))
mbrl_returns.append(np.mean(mbrl_return))
if(np.mean(mbrl_returns) > best_mbrl_return):
controller.save(path=best_model_path)
best_mbrl_return = np.mean(mbrl_returns)
controller.update(gradient_steps=UPDATE_PER_EPISODE*EPISODE_PER_STAGE,batch_size=BATCH_SIZE)
controller.save(path=current_model_path)
controller.clear()
delete_file(log_path)