-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_and_wrappers.py
393 lines (336 loc) · 14.2 KB
/
utils_and_wrappers.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import gym
import numpy as np
import torch
from matplotlib import pyplot
torch.set_default_dtype(torch.float64)
def generate_seeds3D(policy_layers, seed_type, channels, activations = None, environment=None):
layers = len(policy_layers)
max_dim = 0
for i, layer in enumerate(policy_layers):
if max(layer['in_dim'],layer['out_dim']) > max_dim:
max_dim = max(layer['in_dim'],layer['out_dim'])
if i == layers - 1: # last layer
out_dim = layer['out_dim']
if seed_type == 'randomU':
s = torch.rand(1, channels, layers, max_dim, max_dim) # batch size, channels, y_dim, x_dim
elif seed_type == 'randomU2':
s = 2*torch.rand(1, channels,layers, max_dim, max_dim) - 1
elif seed_type == 'randomU3':
s = 2*torch.rand(1, channels,layers,max_dim, max_dim) - 1
s /= 2
elif seed_type == 'randomU4':
s = torch.rand(1, channels,layers, max_dim, max_dim)
s /= 10
elif seed_type == 'randomU5':
s = 2*torch.rand(1, channels,layers, max_dim, max_dim) - 1
s /= 10
elif seed_type == 'randomN':
s = torch.randn(1, channels,layers, max_dim, max_dim)
elif seed_type == 'ones':
s = torch.ones(1, channels, layers,max_dim, max_dim)
elif seed_type == 'zeros':
s = torch.zeros(1, channels, layers,max_dim, max_dim)
elif seed_type == 'single_seed':
s = torch.zeros(1, channels, layers,max_dim, max_dim)
if 'Lander' in environment:
s[:, :, :layers-1, max_dim//2, max_dim//2] = 1.0 # lunar lander
elif 'Ant' in environment:
s[:, :, :, max_dim//2, max_dim//2] = 1.0 # ant
elif seed_type == 'single_seed_big':
s = torch.zeros(1, channels, layers,max_dim, max_dim)
s[:, :, :, max_dim//2-1:max_dim//2+1, max_dim//2-1:max_dim//2+1] = 1.0
elif seed_type == 'two_seeds':
s = torch.zeros(1, channels, layers,max_dim, max_dim)
s[:, :, :, max_dim//2, max_dim//3] = 1.0
s[:, :, :, max_dim//2, max_dim - max_dim//3] = 1.0
elif seed_type == 'three_seeds':
s = torch.zeros(1, channels, layers,max_dim, max_dim)
s[:, :, :, max_dim//2, max_dim//4] = 1.0
s[:, :, :, max_dim//2, max_dim//2] = 1.0
s[:, :, :, max_dim//2, max_dim - max_dim//4] = 1.0
elif seed_type == '1single_seed':
s = torch.zeros(1, channels, layers,max_dim, max_dim)
# s[:, 0, :, :, :] = 1.0
s[:, 0, :, max_dim//2, max_dim//2] = 1.0
elif seed_type == 'four_seeds':
s = torch.zeros(1, channels, layers,max_dim, max_dim)
s[:, :,:, max_dim - max_dim//4, max_dim - max_dim//3] = 1.0
s[:, :,:, max_dim//4, max_dim - max_dim//3] = 1.0
s[:, :,:, max_dim - max_dim//4, max_dim//3] = 1.0
s[:, :,:, max_dim//4, max_dim//3] = 1.0
elif seed_type == '-ones':
s = -1.0*torch.ones(1, channels, layers, max_dim, max_dim)
elif seed_type == 'activations':
s = torch.zeros(1, channels, layers,max_dim, max_dim)
n = activations.shape[0]
s[:, :, : , max_dim//2, max_dim//2 - n//2 : max_dim//2 + n//2] = torch.tensor(activations)
else:
raise AssertionError
s[:,:,-1,out_dim:,:] = 0.0
return s # batch, channels, depth (num layers), ydim, xdim
def policy_layers_parameters(p):
policy_layers = []
for name, layer in p.named_modules():
if isinstance(layer, torch.nn.Linear):
l = {
'layer' : name,
'in_dim' : layer.in_features,
'out_dim': layer.out_features,
'bias' : layer.bias
}
policy_layers.append(l)
elif isinstance(layer, torch.nn.Conv2d):
l = {
'layer' : name,
'in_dim' : layer.in_channels,
'out_dim': layer.out_channels,
'kernel_size' : layer.kernel_size,
'stride' : layer.stride,
'bias' : layer.bias
}
policy_layers.append(l)
elif isinstance(layer, torch.nn.Conv3d):
l = {
'layer' : name,
'in_dim' : layer.in_channels,
'out_dim': layer.out_channels,
'kernel_size' : layer.kernel_size,
'stride' : layer.stride,
'bias' : layer.bias
}
policy_layers.append(l)
return policy_layers
def plots_rewads_save(id_, rewards_mean, rewards_best):
pyplot.plot(rewards_mean, label="Mean")
pyplot.plot(rewards_best, label="Best")
pyplot.xlabel('Generation', fontsize=12)
pyplot.ylabel('Negative reward', fontsize=12)
pyplot.legend(loc="best", prop={'size': 14})
pyplot.savefig('saved_models' + "/"+ id_ + '/rewards_neg.pdf', dpi=300)
pyplot.clf()
pyplot.plot(-1*rewards_mean, label="Mean")
pyplot.plot(-1*rewards_best, label="Best")
pyplot.xlabel('Generation', fontsize=12)
pyplot.ylabel('Reward', fontsize=12)
pyplot.legend(loc="best", prop={'size': 14})
pyplot.savefig('saved_models' + "/"+ id_ + '/rewards.pdf', dpi=300)
pyplot.clf()
pyplot.close()
def visuvisualise_plastic_weights(p, axes, seed, rows, camera, camera2):
for i, ax in enumerate(axes.flat):
im = ax.imshow(seed[0 , i%rows])
ax.axis('off')
camera.snap()
_1, _2, _3 = list(p.parameters()) #MLP
ws = [_1, _2, _3]
ws = [w.detach().numpy() for w in ws]
ws[0] = ws[0]
ws[1] = ws[1].T
ws[2] = ws[2].T
layer_names = ['FC layer 1', 'FC layer 2', 'FC layer 3']
for i, ax in enumerate(axes.flat):
im = ax.imshow(ws[i])
ax.set_yticks([])
ax.set_xticks([])
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.set_xlabel(layer_names[i], fontsize=14)
camera2.snap()
def visualise_plastic_weights_plastic_inFitness(axes , camera, camera2):
animation = camera.animate()
animation.save('animation.mp4', dpi=300)
animation2 = camera2.animate()
#% start: automatic generated code from pylustrator
pyplot.figure(2).ax_dict = {ax.get_label(): ax for ax in pyplot.figure(2).axes}
import matplotlib as mpl
pyplot.figure(2).axes[0].set_position([0.154127, 0.110000, 0.072187, 0.770000])
pyplot.figure(2).axes[1].xaxis.labelpad = 10.720000
pyplot.figure(2).axes[2].set_position([0.817358, 0.110000, 0.036094, 0.770000])
pyplot.figure(2).ax_dict = {ax.get_label(): ax for ax in pyplot.figure(2).axes}
pyplot.figure(2).axes[1].set_position([0.366029, 0.278873, 0.299191, 0.398921])
#% end: automatic generated code from pylustrator
animation2.save('animation_2.mp4', dpi=300)
pyplot.show()
def visualise_plastic_weights_NonPlastic_inFitness(p, axes, axes2, fig2, camera, camera2):
_1, _2, _3 = list(p.parameters()) #MLP
ws = [_1, _2, _3]
ws = [w.detach().numpy() for w in ws]
ws[0] = ws[0]
ws[1] = ws[1].T
ws[2] = ws[2].T
layer_names = ['FC layer 1', 'FC layer 2', 'FC layer 3']
for i, ax in enumerate(axes2.flat):
im = ax.imshow(ws[i])
ax.set_yticks([])
ax.set_xticks([])
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.set_xlabel(layer_names[i], fontsize=12)
fig2.colorbar(im, ax=axes2.flat, orientation="horizontal", pad=0.2)
pyplot.figure(1).ax_dict = {ax.get_label(): ax for ax in pyplot.figure(1).axes}
import matplotlib as mpl
pyplot.figure(1).axes[0].set_position([0.077188, 0.192000, 0.132839, 0.708477])
pyplot.figure(1).axes[2].set_position([0.793879, 0.192000, 0.066420, 0.708477])
pyplot.figure(1).ax_dict = {ax.get_label(): ax for ax in pyplot.figure(1).axes}
pyplot.figure(1).ax_dict["<colorbar>"].set_position([0.177500, 0.058000, 0.653125, 0.043542])
pyplot.figure(1).ax_dict = {ax.get_label(): ax for ax in pyplot.figure(1).axes}
pyplot.figure(1).axes[1].set_position([0.316654, 0.270333, 0.385441, 0.513922])
pyplot.show()
def plot_index_weights(fc):
fig, ax = pyplot.subplots()
im = ax.imshow(fc.weight)
# Loop over data dimensions and create text annotations.
for i in range(fc.weight.shape[0]):
for j in range(fc.weight.shape[0]):
text = ax.text(j, i, str(i)+','+str(j),
ha="center", va="center", color="w", fontsize='xx-small')
ax.set_title("Weight fc")
fig.tight_layout()
pyplot.show()
# https://github.com/openai/baselines/blob/master/baselines/common/atari_wrappers.py
# https://stable-baselines3.readthedocs.io/en/master/_modules/stable_baselines3/common/atari_wrappers.html
class ScaledFloatFrame(gym.ObservationWrapper):
def __init__(self, env):
gym.ObservationWrapper.__init__(self, env)
self.observation_space = gym.spaces.Box(low=0, high=1, shape=env.observation_space.shape, dtype=np.float64)
def observation(self, observation):
# This undoes the memory optimization, use with smaller replay buffers only.
return np.array(observation).astype(np.float64) / 255.0
class FireEpisodicLifeEnv(gym.Wrapper):
def __init__(self, env):
"""Make end-of-life == end-of-episode, but only reset on true game over.
Done by DeepMind for the DQN and co. since it helps value estimation.
"""
gym.Wrapper.__init__(self, env)
self.lives = 0
def step(self, action):
obs, reward, done, info = self.env.step(action)
# Check current lives, make loss of life terminal then update lives to handle bonus lives
lives = self.env.unwrapped.ale.lives()
if self.lives > lives > 0:
# for Qbert sometimes we stay in lives == 0 condtion for a few frames
# so its important to keep lives > 0, so that we only reset once
# the environment advertises done.
# done = True
obs, _, done, _ = self.env.step(1)
if done:
self.env.reset()
obs, _, done, _ = self.env.step(2)
if done:
self.env.reset()
self.lives = lives
return obs, reward, done, info
def reset(self, **kwargs):
self.env.reset(**kwargs)
obs, _, done, _ = self.env.step(1)
if done:
self.env.reset(**kwargs)
obs, _, done, _ = self.env.step(2)
if done:
self.env.reset(**kwargs)
return obs
def calc_fan(weight_shape):
"""
Source: https://github.com/ddbourgin/numpy-ml/
Compute the fan-in and fan-out for a weight matrix/volume.
Parameters
----------
weight_shape : tuple
The dimensions of the weight matrix/volume. The final 2 entries must be
`in_ch`, `out_ch`.
Returns
-------
fan_in : int
The number of input units in the weight tensor
fan_out : int
The number of output units in the weight tensor
"""
if len(weight_shape) == 2:
fan_in, fan_out = weight_shape
elif len(weight_shape) in [3, 4]:
in_ch, out_ch = weight_shape[-2:]
kernel_size = np.prod(weight_shape[:-2])
fan_in, fan_out = in_ch * kernel_size, out_ch * kernel_size
else:
raise ValueError("Unrecognized weight dimension: {}".format(weight_shape))
return fan_in, fan_out
def xavier_glorot_uniform(weight_shape, gain=1.0):
"""
Source: https://github.com/ddbourgin/numpy-ml/
Initialize network weights `W` using the Glorot uniform initialization
strategy.
For tanh
-----
The Glorot uniform initialization strategy initializes weights using draws
from ``Uniform(-b, b)`` where:
.. math::
b = \\text{gain} \sqrt{\\frac{6}{\\text{fan_in} + \\text{fan_out}}}
The motivation for Glorot uniform initialization is to choose weights to
ensure that the variance of the layer outputs are approximately equal to
the variance of its inputs.
This initialization strategy was primarily developed for deep networks with
tanh and logistic sigmoid nonlinearities.
Parameters
----------
weight_shape : tuple
The dimensions of the weight matrix/volume.
Returns
-------
W : :py:class:`ndarray <numpy.ndarray>` of shape `weight_shape`
The initialized weights.
"""
fan_in, fan_out = calc_fan(weight_shape)
b = gain * np.sqrt(6 / (fan_in + fan_out))
return np.squeeze(np.random.uniform(-b, b, size=weight_shape))
def kaiming_he_uniform(weight_shape):
"""
Source: https://github.com/ddbourgin/numpy-ml/
Initializes network weights `W` with using the He uniform initialization
strategy.
For relu
-----
The He uniform initializations trategy initializes thew eights in `W` using
draws from Uniform(-b, b) where
.. math::
b = \sqrt{\\frac{6}{\\text{fan_in}}}
Developed for deep networks with ReLU nonlinearities.
Parameters
----------
weight_shape : tuple
The dimensions of the weight matrix/volume.
Returns
-------
W : :py:class:`ndarray <numpy.ndarray>` of shape `weight_shape`
The initialized weights.
"""
fan_in, fan_out = calc_fan(weight_shape)
b = np.sqrt(6 / fan_in)
return np.squeeze(np.random.uniform(-b, b, size=weight_shape))
def dimensions_env(environment):
"""
Look up observation and action space dimension
"""
from gym.spaces import Discrete, Box
env = gym.make(environment)
if len(env.observation_space.shape) == 3: # Pixel-based environment
pixel_env = True
input_dim = 32*32*3
elif len(env.observation_space.shape) == 1: # State-based environment
pixel_env = False
input_dim = env.observation_space.shape[0]
elif isinstance(env.observation_space, Discrete):
pixel_env = False
input_dim = env.observation_space.n
else:
raise ValueError('Observation space not supported')
if isinstance(env.action_space, Box):
action_dim = env.action_space.shape[0]
elif isinstance(env.action_space, Discrete):
action_dim = env.action_space.n
else:
raise ValueError('Action space not supported')
return input_dim, action_dim, pixel_env