-
Notifications
You must be signed in to change notification settings - Fork 67
/
utils.py
190 lines (142 loc) · 5.43 KB
/
utils.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
"""Utility functions."""
import os
import h5py
import numpy as np
import torch
from torch.utils import data
from torch import nn
import matplotlib.pyplot as plt
EPS = 1e-17
def weights_init(m):
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight)
nn.init.zeros_(m.bias)
def save_dict_h5py(array_dict, fname):
"""Save dictionary containing numpy arrays to h5py file."""
# Ensure directory exists
directory = os.path.dirname(fname)
if not os.path.exists(directory):
os.makedirs(directory)
with h5py.File(fname, 'w') as hf:
for key in array_dict.keys():
hf.create_dataset(key, data=array_dict[key])
def load_dict_h5py(fname):
"""Restore dictionary containing numpy arrays from h5py file."""
array_dict = dict()
with h5py.File(fname, 'r') as hf:
for key in hf.keys():
array_dict[key] = hf[key][:]
return array_dict
def save_list_dict_h5py(array_dict, fname):
"""Save list of dictionaries containing numpy arrays to h5py file."""
# Ensure directory exists
directory = os.path.dirname(fname)
if not os.path.exists(directory):
os.makedirs(directory)
with h5py.File(fname, 'w') as hf:
for i in range(len(array_dict)):
grp = hf.create_group(str(i))
for key in array_dict[i].keys():
grp.create_dataset(key, data=array_dict[i][key])
def load_list_dict_h5py(fname):
"""Restore list of dictionaries containing numpy arrays from h5py file."""
array_dict = list()
with h5py.File(fname, 'r') as hf:
for i, grp in enumerate(hf.keys()):
array_dict.append(dict())
for key in hf[grp].keys():
array_dict[i][key] = hf[grp][key][:]
return array_dict
def get_colors(cmap='Set1', num_colors=9):
"""Get color array from matplotlib colormap."""
cm = plt.get_cmap(cmap)
colors = []
for i in range(num_colors):
colors.append((cm(1. * i / num_colors)))
return colors
def pairwise_distance_matrix(x, y):
num_samples = x.size(0)
dim = x.size(1)
x = x.unsqueeze(1).expand(num_samples, num_samples, dim)
y = y.unsqueeze(0).expand(num_samples, num_samples, dim)
return torch.pow(x - y, 2).sum(2)
def get_act_fn(act_fn):
if act_fn == 'relu':
return nn.ReLU()
elif act_fn == 'leaky_relu':
return nn.LeakyReLU()
elif act_fn == 'elu':
return nn.ELU()
elif act_fn == 'sigmoid':
return nn.Sigmoid()
elif act_fn == 'softplus':
return nn.Softplus()
else:
raise ValueError('Invalid argument for `act_fn`.')
def to_one_hot(indices, max_index):
"""Get one-hot encoding of index tensors."""
zeros = torch.zeros(
indices.size()[0], max_index, dtype=torch.float32,
device=indices.device)
return zeros.scatter_(1, indices.unsqueeze(1), 1)
def to_float(np_array):
"""Convert numpy array to float32."""
return np.array(np_array, dtype=np.float32)
def unsorted_segment_sum(tensor, segment_ids, num_segments):
"""Custom PyTorch op to replicate TensorFlow's `unsorted_segment_sum`."""
result_shape = (num_segments, tensor.size(1))
result = tensor.new_full(result_shape, 0) # Init empty result tensor.
segment_ids = segment_ids.unsqueeze(-1).expand(-1, tensor.size(1))
result.scatter_add_(0, segment_ids, tensor)
return result
class StateTransitionsDataset(data.Dataset):
"""Create dataset of (o_t, a_t, o_{t+1}) transitions from replay buffer."""
def __init__(self, hdf5_file):
"""
Args:
hdf5_file (string): Path to the hdf5 file that contains experience
buffer
"""
self.experience_buffer = load_list_dict_h5py(hdf5_file)
# Build table for conversion between linear idx -> episode/step idx
self.idx2episode = list()
step = 0
for ep in range(len(self.experience_buffer)):
num_steps = len(self.experience_buffer[ep]['action'])
idx_tuple = [(ep, idx) for idx in range(num_steps)]
self.idx2episode.extend(idx_tuple)
step += num_steps
self.num_steps = step
def __len__(self):
return self.num_steps
def __getitem__(self, idx):
ep, step = self.idx2episode[idx]
obs = to_float(self.experience_buffer[ep]['obs'][step])
action = self.experience_buffer[ep]['action'][step]
next_obs = to_float(self.experience_buffer[ep]['next_obs'][step])
return obs, action, next_obs
class PathDataset(data.Dataset):
"""Create dataset of {(o_t, a_t)}_{t=1:N} paths from replay buffer.
"""
def __init__(self, hdf5_file, path_length=5):
"""
Args:
hdf5_file (string): Path to the hdf5 file that contains experience
buffer
"""
self.experience_buffer = load_list_dict_h5py(hdf5_file)
self.path_length = path_length
def __len__(self):
return len(self.experience_buffer)
def __getitem__(self, idx):
observations = []
actions = []
for i in range(self.path_length):
obs = to_float(self.experience_buffer[idx]['obs'][i])
action = self.experience_buffer[idx]['action'][i]
observations.append(obs)
actions.append(action)
obs = to_float(
self.experience_buffer[idx]['next_obs'][self.path_length - 1])
observations.append(obs)
return observations, actions