forked from mojishoki/Coagent-Networks-Revisited
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppo_coagent.py
278 lines (219 loc) · 9.61 KB
/
ppo_coagent.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
269
270
271
272
273
274
275
276
277
278
import gym
import numpy as np
import torch
import torch.nn as nn
class LinearNN(nn.Module):
def __init__(self, input_dim, output_dim, hidden_dim = 8):
super().__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, output_dim)
self.last_x = torch.tensor([], dtype = torch.float32)
self.last_y = None
def forward(self, x):
if not self.training and torch.equal(self.last_x, x.cpu().float()):
return self.last_y
if not self.training:
self.last_x = x.cpu().float()
self.last_y = self.fc2(torch.relu(self.fc1(x.float())))
return self.last_y
else: # not training in which case no need to update last observations
return self.fc2(torch.relu(self.fc1(x.float())))
def initialize_zero(self, basenoise = .0):
self.fc1.weight.data.fill_(basenoise)
self.fc2.weight.data.fill_(basenoise)
def initialize_xavier(self):
torch.nn.init.xavier_uniform_(self.fc1.weight)
torch.nn.init.xavier_uniform_(self.fc2.weight)
class PPOCoAgent:
def reset(self):
self.states = []
self.actions = []
self.action_probs = []
self.impprobs = []
self.act_time = []
self.returns = []
self.vs = []
self.advs = []
def ppo_advret(self, rewards):
eps_len = len(rewards)
for act_time in self.act_time:
self.returns.append( np.sum( np.array([self.discount**i for i in range(eps_len-act_time)]) * np.array(rewards[act_time:])) )
next_value = 0
tmp_adv = 0
act_time_ext = self.act_time + [eps_len]
self.advs =[0.] * len(self.states)
for i in reversed(range(len(self.states))):
if i<len(self.states)-1:
next_value = self.vs[i+1]
len_act_time = act_time_ext[i+1]-act_time_ext[i]
rewards_i = np.sum( np.array([self.discount**k for k in range(len_act_time)]) * np.array(rewards[act_time_ext[i]:act_time_ext[i+1]]))
tmp_td_error = rewards_i + self.discount**(len_act_time) * next_value - self.vs[i]
tmp_adv = tmp_adv * self.eta * self.discount + tmp_td_error
self.advs[i] = tmp_adv
def states_actions_to_prob(self, paths_to_root):
raise NotImplementedError
def ppo_update(self, paths_to_root):
self.zero_grad()
new_action_logprobs, new_vs, new_action_entropies = self.states_actions_to_prob(paths_to_root)
if new_action_logprobs != []:
self.train()
surrogate = 0.
ratio = (new_action_logprobs - torch.log(torch.tensor(self.action_probs)).to(self.device)).exp()
ratio_clamped = torch.clamp(ratio, 1 - self.epsilon, 1 + self.epsilon)
self.advs = torch.tensor(self.advs).to(self.device)
Lsur = self.advs * ratio
Lsur_clamped = self.advs * ratio_clamped
Lsur_clipped = torch.min(Lsur, Lsur_clamped)
value_loss=(new_vs - torch.tensor(self.returns).to(self.device))**2
surrogate = torch.mean(-Lsur_clipped + self.c1 * value_loss - self.beta * new_action_entropies)
surrogate.backward()
torch.nn.utils.clip_grad_norm_(self.pi.parameters(), self.clip)
self.step()
self.eval()
def update(self, rewards, paths_to_root):
self.ppo_advret(rewards)
for _ in range(self.SGD_epoch):
self.ppo_update(paths_to_root)
class SigmoidTermination(PPOCoAgent):
def __init__(self, rng, observation_space, discount, eta, hidden_dim, SGD_epoch, epsilon, c1, beta, clip, lr, temp, device):
assert(isinstance(observation_space, gym.spaces.Box))
self.rng = rng
self.discount = discount
self.eta = eta
self.SGD_epoch = SGD_epoch
self.epsilon = epsilon
self.c1 = c1
self.beta = beta
self.clip = clip
self.temp = temp
self.device = device
self.pi = LinearNN(np.prod(observation_space.shape), 1, hidden_dim).to(device)
self.pi.initialize_zero(1e-3)
self.optim = torch.optim.Adam(self.pi.parameters(), lr=lr)
self.pi.eval()
def train(self):
self.pi.train()
def eval(self):
self.pi.eval()
def zero_grad(self):
self.optim.zero_grad()
def step(self):
self.optim.step()
def pmf(self, observation):
if self.pi.training:
return torch.sigmoid(self.pi(observation)/self.temp)
else:
with torch.no_grad():
return torch.sigmoid(self.pi(observation)/self.temp).item()
def sample(self, observation, time):
p = self.pmf(observation)
idx = int(self.rng.uniform() < p)
prob = p if idx == 1 else 1 - p
self.states.append(observation)
self.actions.append(idx)
self.action_probs.append(prob)
self.act_time.append(time)
return idx, prob
def value(self, observation, path_to_root_from_o):
v = 0
prod = 1.
for p in path_to_root_from_o:
v += prod * (1- p.termination.pmf(observation))*p.policy.value(observation)
prod *= p.termination.pmf(observation)
return v
def states_actions_to_prob(self, paths_to_root):
new_action_logprobs = []
new_action_entropies = []
new_vs = []
if self.states != []:
self.train()
for j,state in enumerate(self.states):
o_termination_prob = self.pmf(state)
new_action_entropies.append(-((1-o_termination_prob) * torch.log(1-o_termination_prob+1e-8) + o_termination_prob*torch.log(o_termination_prob+1e-8)))
new_action_logprobs.append(torch.log(o_termination_prob if self.actions[j] else 1-o_termination_prob))
new_action_entropies = torch.stack(new_action_entropies)
new_action_logprobs = torch.stack(new_action_logprobs)
for t,act_time in enumerate(self.act_time):
path_to_root_act_time = paths_to_root[act_time-1]
for oo_lev, oo in enumerate(path_to_root_act_time):
if oo.termination is self:
new_vs.append( self.value(self.states[t], path_to_root_act_time[oo_lev:]) )
break
new_vs = torch.stack(new_vs)
self.eval()
return new_action_logprobs, new_vs, new_action_entropies
class SoftmaxAC(PPOCoAgent):
def __init__(self, rng, observation_space, nactions, discount, eta, hidden_dim, SGD_epoch, epsilon, c1, beta, clip, lr_actor, lr_critic, temp, device):
assert(isinstance(observation_space, gym.spaces.Box))
self.rng = rng
self.nactions = nactions
self.discount = discount
self.eta = eta
self.SGD_epoch = SGD_epoch
self.epsilon = epsilon
self.c1 = c1
self.beta = beta
self.clip = clip
self.temp = temp
self.device = device
self.pi = LinearNN(np.prod(observation_space.shape), self.nactions, hidden_dim).to(device)
self.pi.initialize_zero()
# self.pi.initialize_xavier()
self.v = LinearNN(np.prod(observation_space.shape), 1, hidden_dim).to(device)
self.v.initialize_zero()
# self.v.initialize_xavier()
self.pi_optim = torch.optim.Adam(self.pi.parameters(), lr=lr_actor)
self.v_optim = torch.optim.Adam(self.v.parameters(), lr=lr_critic)
self.pi.eval()
self.v.eval()
def train(self):
self.pi.train()
self.v.train()
def eval(self):
self.pi.eval()
self.v.eval()
def zero_grad(self):
self.pi_optim.zero_grad()
self.v_optim.zero_grad()
def step(self):
self.pi_optim.step()
self.v_optim.step()
def pmf(self, observation):
if self.pi.training:
return torch.softmax(self.pi(observation)/self.temp, dim=0)
else:
with torch.no_grad():
return torch.softmax(self.pi(observation)/self.temp, dim=0).cpu().numpy()
def sample(self, observation, time):
p = self.pmf(observation)
idx = int(self.rng.choice(self.nactions, p=p))
prob = p[idx]
self.impprobs.append(1.) # this will end up storing the probability of reaching here from the last primitive action
self.states.append(observation)
self.actions.append(idx)
self.action_probs.append(prob)
self.act_time.append(time)
self.vs.append(self.value(observation))
return idx, prob
def value(self, observation, action=None):
if action is None:
if self.v.training:
return self.v(observation)
else:
with torch.no_grad():
return self.v(observation).item()
def states_actions_to_prob(self, paths_to_root=None):
new_action_logprobs = []
new_action_entropies = []
new_vs = []
if self.states != []:
self.train()
for j,state in enumerate(self.states):
action_probs = self.pmf(state)
new_action_entropies.append( -torch.sum(action_probs *torch.log(action_probs )) )
new_action_logprobs.append( torch.log(self.pmf(state)[self.actions[j]]) )
new_action_entropies = torch.stack(new_action_entropies)
new_action_logprobs = torch.stack(new_action_logprobs)
new_vs = torch.stack([self.value(state) for state in self.states])
self.eval()
return new_action_logprobs, new_vs, new_action_entropies