-
Notifications
You must be signed in to change notification settings - Fork 17
/
bare_metal_sac.py
179 lines (132 loc) · 5.1 KB
/
bare_metal_sac.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
import gym
import envs
from wrappers import BoundedActionsEnv, NoisyEnv
from envs.half_cheetah import MagellanHalfCheetahRunningForwardRewardFunction, MagellanHalfCheetahFlippingForwardRewardFunction
import os
from sacred import Experiment
from logger import get_logger
from sac import *
ex = Experiment()
ex.logger = get_logger('bare_metal_sac')
class BareMetalSAC(SAC):
def setup_reward_func(self, reward_func):
self.reward_func = reward_func
def episode(self, env, warm_up=False, train=True):
ep_return, ep_length = 0, 0
done = False
state = env.reset()
while not done:
if warm_up:
action = env.action_space.sample()
else:
action = self(torch.from_numpy(state).unsqueeze(0).float().to(self.device))
action = action.data[0].detach().cpu().numpy()
next_state, reward, done, _ = env.step(action)
if hasattr(self, 'reward_func'):
reward = self.reward_func(state, action, next_state)
ep_return += reward
ep_length += 1
if not done or ep_length == env.spec.max_episode_steps:
mask = 1
else:
mask = 0
self.replay.add(torch.from_numpy(state).unsqueeze(0).float(),
torch.from_numpy(action).unsqueeze(0).float(),
torch.from_numpy(np.array([reward])).float(),
torch.from_numpy(next_state).unsqueeze(0).float(),
torch.from_numpy(np.array([mask])).unsqueeze(0).float())
state = next_state
if train:
if not warm_up:
for _ in range(self.n_updates * ep_length):
self.update()
return ep_return, ep_length
# noinspection PyUnusedLocal
@ex.config
def config():
env_name = "MagellanHalfCheetah-v2"
env_noise_stdev = 0.02
n_steps = int(2e5)
warm_up_steps = 256
eval_freq = 2000
# default SAC parameters
replay_size = int(1e6)
n_hidden = 256
batch_size = 512
n_updates = 1
lr = 1e-3
gamma = 0.99
alpha = 0.2
tau = 0.005
# infra
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
# device = torch.device('cpu')
omp_num_threads = 4
env = gym.make(env_name)
d_state = env.observation_space.shape[0]
d_action = env.action_space.shape[0]
del env
@ex.capture
def get_env(env_name, env_noise_stdev):
env = gym.make(env_name)
env = BoundedActionsEnv(env)
if env_noise_stdev:
env = NoisyEnv(env, stdev=env_noise_stdev)
return env
@ex.capture
def get_agent(reward_func, d_state, d_action, replay_size, batch_size, n_hidden, gamma, alpha, lr, tau, n_updates, device):
agent = BareMetalSAC(d_state=d_state, d_action=d_action, n_hidden=n_hidden,
replay_size=replay_size, batch_size=batch_size,
gamma=gamma, alpha=alpha, lr=lr, tau=tau, n_updates=n_updates)
agent = agent.to(device)
agent.setup_reward_func(reward_func)
return agent
@ex.capture
def evaluate_agent(agent, device, _log):
env = get_env()
env.seed(np.random.randint(2 ** 32 - 1))
env.action_space.seed(np.random.randint(2 ** 32 - 1))
returns = []
for _ in range(20):
ep_return = 0
done = False
state = env.reset()
while not done:
action = agent(torch.from_numpy(state).unsqueeze(0).float().to(device), eval=True)
action = action.data[0].detach().cpu().numpy()
next_state, reward, done, _ = env.step(action)
if hasattr(agent, 'reward_func'):
reward = agent.reward_func(state, action, next_state)
ep_return += reward
state = next_state
returns.append(ep_return)
return np.mean(returns)
@ex.capture
def execute_and_train_agent(agent, env, n_steps, warm_up_steps, eval_freq, _log, _run):
returns = []
step_i = 0
while step_i < n_steps:
ep_return, ep_length = agent.episode(env=env, warm_up=(step_i < warm_up_steps))
step_i += ep_length
returns.append(ep_return)
_log.info(f"step: {step_i}, return: {np.round(ep_return, 2)}\taverage return: {np.round(np.mean(returns[-100:]), 2)}")
if step_i % eval_freq == 0:
eval_return = evaluate_agent(agent=agent)
_log.info(f"step: {step_i}, eval return: {np.round(eval_return, 2)}")
_run.log_scalar("return", eval_return, step_i)
@ex.automain
def main(seed, omp_num_threads):
torch.set_num_threads(omp_num_threads)
os.environ['OMP_NUM_THREADS'] = str(omp_num_threads)
os.environ['MKL_NUM_THREADS'] = str(omp_num_threads)
torch.manual_seed(seed)
np.random.seed(seed)
env = get_env()
env.seed(seed)
agent = get_agent(reward_func=MagellanHalfCheetahFlippingForwardRewardFunction())
try:
execute_and_train_agent(agent=agent, env=env)
except KeyboardInterrupt:
pass
torch.save(agent.state_dict(), 'bare_metal_sac_agent.pt')
return evaluate_agent(agent=agent)