forked from sintefneodroid/agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg_agent.py
executable file
·280 lines (203 loc) · 7.32 KB
/
pg_agent.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
279
280
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from neodroid import EnvironmentState
__author__ = 'cnheider'
from itertools import count
import numpy as np
import torch
import torch.nn.functional as F
from torch.distributions import Categorical
from tqdm import tqdm
import utilities as U
from agents.abstract.policy_agent import PolicyAgent
tqdm.monitor_interval = 0
class PGAgent(PolicyAgent):
# region Private
def __defaults__(self) -> None:
self._policy_arch = U.CategoricalMLP
self._accumulated_error = U.to_tensor(0.0, device=self._device)
self._evaluation_function = torch.nn.CrossEntropyLoss()
self._trajectory_trace = U.TrajectoryTraceBuffer()
self._policy_arch_params = U.ConciseArchSpecification(**{
'input_size': None, # Obtain from environment
'hidden_layers':[64, 32, 16],
'output_size': None, # Obtain from environment
'activation': F.relu,
'use_bias': True,
})
self._use_cuda = False
self._discount_factor = 0.99
self._use_batched_updates = False
self._batch_size = 5
self._pg_entropy_reg = 1e-4
self._signal_clipping = False
self._optimiser_learning_rate = 1e-4
self._optimiser_type = torch.optim.Adam
self._optimiser_weight_decay = 1e-5
self._state_type = torch.float
self._signals_tensor_type = torch.float
# endregion
# region Protected
def _sample_model(self, state, **kwargs):
state_tensor = U.to_tensor([state], device=self._device, dtype=self._state_type)
with torch.no_grad():
probs = self._policy(state_tensor)
m = Categorical(probs)
action = m.sample()
return action.item()
def _build(self, **kwargs) -> None:
policy = self._policy_arch(
**(self._policy_arch_params._asdict())
).to(self._device)
self.optimiser = self._optimiser_type(
policy.parameters(),
lr=self._optimiser_learning_rate,
weight_decay=self._optimiser_weight_decay,
)
self._policy = policy
def _optimise_wrt(self, loss, **kwargs):
self.optimiser.zero_grad()
loss.backward()
for params in self._policy.parameters():
params.grad.data.clamp_(-1, 1)
self.optimiser.step()
# endregion
# region Public
def sample_action(self, state, discrete=True, **kwargs):
if discrete:
return self.sample_discrete_action(state)
return self.sample_continuous_action(state)
def sample_discrete_action(self, state):
state_var = U.to_tensor([state], device=self._device, dtype=self._state_type)
probs = self._policy(state_var)
# action = np.argmax(probs)
m = Categorical(probs)
action_sample = m.sample()
action = action_sample.item()
return action, m.log_prob(action_sample), m.entropy()
def sample_continuous_action(self, state):
model_input = U.to_tensor([state], device=self._device, dtype=self._state_type)
with torch.no_grad():
mu, sigma_sq = self._policy(model_input)
mu, sigma_sq = mu[0], sigma_sq[0]
# std = self.sigma.exp().expand_as(mu)
# dist = torch.Normal(mu, std)
# return dist, value
eps = torch.randn(mu.size())
# calculate the probability
action = (mu + sigma_sq.sqrt() * eps).data
prob = U.normal(action, mu, sigma_sq)
entropy = -0.5 * ((
sigma_sq
+ 2
* U.pi_torch(self._device).expand_as(sigma_sq)
).log()
+ 1
)
log_prob = prob.log()
return action, log_prob, entropy
def evaluate(self, **kwargs):
R = 0
policy_loss = []
signals = []
trajectory = self._trajectory_trace.retrieve_trajectory()
t_signal = trajectory.signal
log_probs = trajectory.log_prob
entrp = trajectory.entropy
self._trajectory_trace.clear()
for r in t_signal[::-1]:
R = r + self._discount_factor * R
signals.insert(0, R)
signals = U.to_tensor(signals, device=self._device, dtype=self._signals_tensor_type)
if signals.shape[0] > 1:
stddev = signals.std()
signals = (signals - signals.mean()) / (stddev + self._divide_by_zero_safety)
for log_prob, signal, entropy in zip(log_probs, signals, entrp):
policy_loss.append(-log_prob * signal - self._pg_entropy_reg * entropy)
loss = torch.cat(policy_loss).sum()
return loss
def update(self, *args, **kwargs):
error = self.evaluate()
if error is not None:
if self._use_batched_updates:
self._accumulated_error += error
if self._rollout_i % self._batch_size == 0:
self._optimise_wrt(self._accumulated_error / self._batch_size)
self._accumulated_error = U.to_tensor(0.0, device=self._device)
else:
self._optimise_wrt(error)
def rollout(self, initial_state, environment, render=False, train=True, **kwargs):
if train:
self._rollout_i += 1
episode_signal = 0
episode_length = 0
episode_entropy = 0
if type(initial_state) is EnvironmentState:
state = initial_state.observables
else:
state = initial_state
T = count(1)
T = tqdm(T, f'Rollout #{self._rollout_i}', leave=False)
for t in T:
action, action_log_probs, entropy, *_ = self.sample_action(state)
state, signal, terminated, info = environment.step(action)
if self._signal_clipping:
signal = np.clip(signal, -1.0, 1.0)
episode_signal += signal
episode_entropy += entropy.data.cpu().numpy()
if train:
self._trajectory_trace.add_trace(signal, action_log_probs, entropy)
if render:
environment.render()
if terminated:
episode_length = t
break
if train:
self.update()
avg_entropy = episode_entropy.mean().item()
return episode_signal, episode_length, avg_entropy
def infer(self, env, render=True):
for episode_i in count(1):
print('Episode {}'.format(episode_i))
state = env.reset()
for frame_i in count(1):
action, *_ = self.sample_action(state)
state, reward, terminated, info = env.step(action)
if render:
env.render()
if terminated:
break
def train_episodically(
self,
env,
rollouts=2000,
render=False,
render_frequency=100,
stat_frequency=10,
):
E = range(1, rollouts)
E = tqdm(E, f'Episode: {1}', leave=False)
stats = U.StatisticCollection(stats=('signal', 'duration', 'entropy'))
for episode_i in E:
initial_state = env.reset()
if episode_i % stat_frequency == 0:
U.styled_term_plot_stats_shared_x(stats,
printer=E.write)
E.set_description(f'Episode: {episode_i}, Running signal: {stats.signal.running_value[-1]}, '
f'Running length: {stats.duration.running_value[-1]}')
if render and episode_i % render_frequency == 0:
signal, dur, entrp, *extras = self.rollout(
initial_state, env, render=render
)
else:
signal, dur, entrp, *extras = self.rollout(initial_state, env)
stats.duration.append(dur)
stats.signal.append(signal)
stats.entropy.append(entrp)
if self._end_training:
break
return self._policy, stats
# endregion
if __name__ == '__main__':
import configs.agent_test_configs.test_pg_config as C
U.test_agent_main(PGAgent, C)