2017-03-07 23:42:44 -08:00
|
|
|
import numpy as np
|
|
|
|
|
2017-05-16 14:12:18 -07:00
|
|
|
|
2017-03-07 23:42:44 -08:00
|
|
|
def flatten(weights, start=0, stop=2):
|
2017-07-13 14:53:57 -07:00
|
|
|
"""This methods reshapes all values in a dictionary.
|
2017-03-07 23:42:44 -08:00
|
|
|
|
2017-07-13 14:53:57 -07:00
|
|
|
The indices from start to stop will be flattened into a single index.
|
2017-03-07 23:42:44 -08:00
|
|
|
|
2017-07-13 14:53:57 -07:00
|
|
|
Args:
|
|
|
|
weights: A dictionary mapping keys to numpy arrays.
|
|
|
|
start: The starting index.
|
|
|
|
stop: The ending index.
|
|
|
|
"""
|
|
|
|
for key, val in weights.items():
|
2018-07-19 15:30:36 -07:00
|
|
|
new_shape = val.shape[0:start] + (-1, ) + val.shape[stop:]
|
2017-07-13 14:53:57 -07:00
|
|
|
weights[key] = val.reshape(new_shape)
|
|
|
|
return weights
|
2017-03-07 23:42:44 -08:00
|
|
|
|
2017-05-16 14:12:18 -07:00
|
|
|
|
2017-03-07 23:42:44 -08:00
|
|
|
def concatenate(weights_list):
|
2017-07-13 14:53:57 -07:00
|
|
|
keys = weights_list[0].keys()
|
|
|
|
result = {}
|
|
|
|
for key in keys:
|
|
|
|
result[key] = np.concatenate([l[key] for l in weights_list])
|
|
|
|
return result
|
2017-03-07 23:42:44 -08:00
|
|
|
|
2017-05-16 14:12:18 -07:00
|
|
|
|
2017-03-07 23:42:44 -08:00
|
|
|
def shuffle(trajectory):
|
2017-12-14 01:08:23 -08:00
|
|
|
permutation = np.random.permutation(trajectory["actions"].shape[0])
|
2017-07-13 14:53:57 -07:00
|
|
|
for key, val in trajectory.items():
|
|
|
|
trajectory[key] = val[permutation]
|
|
|
|
return trajectory
|