/
sac_ds_base.py
268 lines (211 loc) · 8.91 KB
/
sac_ds_base.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import logging
import sys
import time
from itertools import chain
from pathlib import Path
from typing import List, Optional, Tuple
import numpy as np
import torch
from torch.utils.tensorboard import SummaryWriter
sys.path.append(str(Path(__file__).resolve().parent.parent))
from algorithm.sac_base import SAC_Base
logger = logging.getLogger('sac.base.ds')
class SAC_DS_Base(SAC_Base):
def __init__(self,
obs_shapes: Tuple,
d_action_size: int,
c_action_size: int,
model_abs_dir: Optional[str],
model,
device: Optional[str] = None,
summary_path: str = 'log',
train_mode: bool = True,
last_ckpt: Optional[str] = None,
seed=None,
write_summary_per_step=1e3,
save_model_per_step=1e5,
ensemble_q_num=2,
ensemble_q_sample=2,
burn_in_step=0,
n_step=1,
use_rnn=False,
tau=0.005,
update_target_per_step=1,
init_log_alpha=-2.3,
use_auto_alpha=True,
learning_rate=3e-4,
gamma=0.99,
v_lambda=0.9,
v_rho=1.,
v_c=1.,
clip_epsilon=0.2,
discrete_dqn_like=False,
use_prediction=False,
transition_kl=0.8,
use_extra_data=True,
use_curiosity=False,
curiosity_strength=1,
use_rnd=False,
rnd_n_sample=10,
use_normalization=False,
noise=0.):
self.obs_shapes = obs_shapes
self.d_action_size = d_action_size
self.c_action_size = c_action_size
self.train_mode = train_mode
self.ensemble_q_num = ensemble_q_num
self.ensemble_q_sample = ensemble_q_sample
self.burn_in_step = burn_in_step
self.n_step = n_step
self.use_rnn = use_rnn
self.write_summary_per_step = int(write_summary_per_step)
self.save_model_per_step = int(save_model_per_step)
self.tau = tau
self.update_target_per_step = update_target_per_step
self.use_auto_alpha = use_auto_alpha
self.gamma = gamma
self.v_lambda = v_lambda
self.v_rho = v_rho
self.v_c = v_c
self.clip_epsilon = clip_epsilon
self.discrete_dqn_like = discrete_dqn_like
self.use_prediction = use_prediction
self.transition_kl = transition_kl
self.use_extra_data = use_extra_data
self.use_curiosity = use_curiosity
self.curiosity_strength = curiosity_strength
self.use_rnd = use_rnd
self.rnd_n_sample = rnd_n_sample
self.use_normalization = use_normalization
self.use_priority = False
self.use_n_step_is = True
self.noise = noise
self.device = device
if device is None:
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
if seed is not None:
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
self.summary_writer = None
if model_abs_dir:
summary_path = Path(model_abs_dir).joinpath(summary_path)
self.summary_writer = SummaryWriter(str(summary_path))
self._build_model(model, init_log_alpha, learning_rate)
self._init_or_restore(model_abs_dir, int(last_ckpt) if last_ckpt is not None else None)
def _random_action(self, action):
batch = action.shape[0]
d_action = action[..., :self.d_action_size]
c_action = action[..., self.d_action_size:]
if self.d_action_size:
action_random = np.eye(self.d_action_size)[np.random.randint(0, self.d_action_size, size=batch)]
cond = np.random.rand(batch) < self.noise
d_action[cond] = action_random[cond]
if self.c_action_size:
c_action = np.tanh(np.arctanh(c_action) + np.random.randn(batch, self.c_action_size) * self.noise)
return np.concatenate([d_action, c_action], axis=-1).astype(np.float32)
def choose_action(self, obs_list):
action = super().choose_action(obs_list)
return self._random_action(action)
def choose_rnn_action(self, obs_list, pre_action, rnn_state):
action, next_rnn_state = super().choose_rnn_action(obs_list, pre_action, rnn_state)
return self._random_action(action), next_rnn_state
def get_policy_variables(self, get_numpy=True):
"""
For learner to send variables to actors
"""
variables = chain(self.model_rep.parameters(), self.model_policy.parameters())
if get_numpy:
return [v.detach().cpu().numpy() for v in variables]
else:
return variables
def update_policy_variables(self, t_variables: List[np.ndarray]):
"""
For actor to update its own network from learner
"""
variables = self.get_policy_variables(get_numpy=False)
for v, t_v in zip(variables, t_variables):
v.data.copy_(torch.from_numpy(t_v).to(self.device))
def get_nn_variables(self, get_numpy=True):
"""
For learner to send variables to evolver
"""
variables = chain(self.model_rep.parameters(),
self.model_policy.parameters(),
[self.log_d_alpha, self.log_c_alpha])
for model_q in self.model_q_list:
variables = chain(variables,
model_q.parameters())
if self.use_prediction:
variables = chain(variables,
self.model_transition.parameters(),
self.model_reward.parameters(),
self.model_observation.parameters())
if self.use_curiosity:
variables = chain(variables,
self.model_forward.parameters())
if self.use_rnd:
variables = chain(variables,
self.model_rnd.parameters(),
self.model_target_rnd.parameters())
if get_numpy:
return [v.detach().cpu().numpy() for v in variables]
else:
return variables
def update_nn_variables(self, t_variables: List[np.ndarray]):
"""
Update own network from evolver selection
"""
variables = self.get_nn_variables(get_numpy=False)
for v, t_v in zip(variables, t_variables):
v.data.copy_(torch.from_numpy(t_v).to(self.device))
self._update_target_variables()
def get_all_variables(self, get_numpy=True):
variables = self.get_nn_variables(get_numpy=False)
variables = chain(variables, self.model_target_rep.parameters())
for model_target_q in self.model_target_q_list:
variables = chain(variables, model_target_q.parameters())
if self.use_normalization:
variables = chain(variables,
[self.normalizer_step],
self.running_means,
self.running_variances)
if get_numpy:
return [v.detach().cpu().numpy() for v in variables]
else:
return variables
def update_all_variables(self, t_variables: List[np.ndarray]):
if any([np.isnan(v.sum()) for v in t_variables]):
return False
variables = self.get_all_variables(get_numpy=False)
for v, t_v in zip(variables, t_variables):
v.data.copy_(torch.from_numpy(t_v).to(self.device))
return True
def train(self,
n_obses_list,
n_actions,
n_rewards,
next_obs_list,
n_dones,
n_mu_probs,
rnn_state=None):
n_obses_list = [torch.from_numpy(t).to(self.device) for t in n_obses_list]
n_actions = torch.from_numpy(n_actions).to(self.device)
n_rewards = torch.from_numpy(n_rewards).to(self.device)
next_obs_list = [torch.from_numpy(t).to(self.device) for t in next_obs_list]
n_dones = torch.from_numpy(n_dones).to(self.device)
n_mu_probs = torch.from_numpy(n_mu_probs).to(self.device)
if self.use_rnn:
rnn_state = torch.from_numpy(rnn_state).to(self.device)
self._train(n_obses_list=n_obses_list,
n_actions=n_actions,
n_rewards=n_rewards,
next_obs_list=next_obs_list,
n_dones=n_dones,
n_mu_probs=n_mu_probs,
priority_is=None,
initial_rnn_state=rnn_state if self.use_rnn else None)
step = self.global_step.item()
if step % self.save_model_per_step == 0:
self.save_model()
self._increase_global_step()
return step + 1