-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory_rnn.py
executable file
·217 lines (156 loc) · 8.45 KB
/
memory_rnn.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
import torch.utils.data
import numpy as np
import torch
import os
from config import consts, args
from preprocess import lock_file, release_file
import cv2
import pandas as pd
img_width = args.width
img_height = args.height
interpolation = cv2.INTER_LINEAR # cv2.INTER_AREA #
imread_grayscale = cv2.IMREAD_GRAYSCALE
class MemoryRNN(torch.utils.data.Dataset):
def __init__(self):
super(MemoryRNN, self).__init__()
self.history_length = args.history_length
self.n_steps = args.n_steps
self.seq_length = args.seq_length
self.reward_length = args.seq_length
if args.target == 'tde':
self.seq_length += self.n_steps
else:
self.n_steps = 0
self.burn_in = args.burn_in
self.history_mat = np.expand_dims(np.arange(self.seq_length + self.burn_in), axis=1) + np.arange(self.history_length)
self.history_mat = np.fliplr(self.history_mat)
self.hidden_features = args.hidden_features_rnn
self.seq_overlap = args.seq_overlap
def __len__(self):
return args.n_tot
def __getitem__(self, index):
raise NotImplementedError
def preprocess_trajectory(self, episode_dir, frame, k):
frames = [os.path.join(episode_dir, "%d.png" % (frame + i)) for i in range(-self.history_length + 1, k)]
imgs = np.stack([(cv2.resize(cv2.imread(f0, imread_grayscale).astype(np.float32),
(img_width, img_height), interpolation=interpolation) / 256.) for f0 in frames], axis=0)
return imgs[self.history_mat[:k], :, :]
def collate(batch):
numel = sum([x['h_q'].numel() for x in batch])
storage = batch[0]['h_q'].storage()._new_shared(numel)
out_h_q = batch[0]['h_q'].new(storage)
numel = sum([x['h_beta'].numel() for x in batch])
storage = batch[0]['h_beta'].storage()._new_shared(numel)
out_h_beta = batch[0]['h_beta'].new(storage)
numel = sum([x['s'].numel() for x in batch])
storage = batch[0]['s'].storage()._new_shared(numel)
out_s = batch[0]['s'].new(storage)
numel = sum([x['s_bi'].numel() for x in batch])
storage = batch[0]['s_bi'].storage()._new_shared(numel)
out_s_bi = batch[0]['s_bi'].new(storage)
return {'s': torch.stack([sample['s'] for sample in batch], out=out_s),
's_bi': torch.stack([sample['s_bi'] for sample in batch], out=out_s_bi),
'a': torch.stack([sample['a'] for sample in batch]),
'r': torch.stack([sample['r'] for sample in batch]),
't': torch.stack([sample['t'] for sample in batch]),
'rho_q': torch.stack([sample['rho_q'] for sample in batch]),
'rho_v': torch.stack([sample['rho_v'] for sample in batch]),
'h_beta': torch.stack([sample['h_beta'] for sample in batch], out=out_h_beta),
'h_q': torch.stack([sample['h_q'] for sample in batch], out=out_h_q),
'pi': torch.stack([sample['pi'] for sample in batch]),
'tde': torch.FloatTensor([sample['tde'] for sample in batch])}
class ObservationsRNNMemory(MemoryRNN):
def __init__(self, replay_dir):
super(ObservationsRNNMemory, self).__init__()
self.screen_dir = os.path.join(replay_dir, "explore", "screen")
def __getitem__(self, samples):
# if the first episode is too short s.t. there is no hidden state sample, take the second episode
tde = samples['tde'][self.seq_length + self.burn_in]
if (samples['fr_e'][0] - samples['fr'][0]) <= (self.burn_in + (-samples['fr'][0] % self.seq_overlap)):
# take the second episode
offset = samples['fr_e'][0] - samples['fr'][0]
start = offset + (-samples['fr'][offset] % self.seq_overlap)
samples = samples[start:start + self.burn_in + self.seq_length]
else:
# take the first episode
start = (-samples['fr'][0] % self.seq_overlap)
end = min(samples['fr_e'][0] - samples['fr'][0], (self.burn_in + self.seq_length) + start)
samples = samples[start:end]
prune_length = len(samples)
pad_l = min(self.seq_length + self.burn_in - prune_length, self.burn_in)
pad_r = self.seq_length + self.burn_in - (prune_length + pad_l)
episode_dir = os.path.join(self.screen_dir, str(samples['ep'][0]))
s = self.preprocess_trajectory(episode_dir, samples['fr'][0], prune_length)
h_q = samples['h_q'][0]
h_beta = samples['h_beta'][0]
r = np.pad(samples['r'], [(0, pad_r + pad_l)], 'constant', constant_values=0)
rho_v = np.pad(samples['rho_v'], [(0, pad_r + pad_l)], 'constant', constant_values=0)
rho_q = np.pad(samples['rho_q'], [(0, pad_r + pad_l)], 'constant', constant_values=0)
a = np.pad(samples['a'], [(0, pad_r + pad_l)], 'constant', constant_values=0)
pi = np.pad(np.stack(samples['pi']), [(0, pad_r + pad_l), (0, 0)], 'constant', constant_values=0)
s = np.pad(s, [(0, pad_r + pad_l), (0, 0), (0, 0), (0, 0)], 'constant', constant_values=0)
t = np.pad(samples['t'], [(0, pad_r + pad_l)], 'constant', constant_values=1)
return {'s': torch.from_numpy(s[self.burn_in:]),
'r': torch.from_numpy(r[-self.seq_length:-self.n_steps]),
'rho_q': torch.from_numpy(rho_q[-self.seq_length:-self.n_steps]),
'rho_v': torch.from_numpy(rho_v[self.burn_in:]),
'a': torch.from_numpy(a[self.burn_in:]),
'pi': torch.from_numpy(pi[self.burn_in:]),
'h_q': torch.from_numpy(h_q),
'h_beta': torch.from_numpy(h_beta),
's_bi': torch.from_numpy(s[:self.burn_in]),
't': torch.from_numpy(t[self.burn_in:]),
'tde': tde}
class ObservationsRNNBatchSampler(object):
def __init__(self, replay_dir):
self.batch = args.batch
self.screen_dir = os.path.join(replay_dir, "explore", "screen")
self.trajectory_dir = os.path.join(replay_dir, "explore", "trajectory")
self.list_old_path = os.path.join(replay_dir, "list", "old_explore")
self.replay_updates_interval = args.replay_updates_interval
self.replay_memory_size = args.replay_memory_size
self.readlock = os.path.join(replay_dir, "list", "readlock_explore.npy")
self.rec_type = consts.rec_type
self.priority_alpha = args.priority_alpha
self.epsilon_a = args.epsilon_a
self.tde = np.array([])
def __iter__(self):
traj_old = 0
replay_buffer = np.array([], dtype=self.rec_type)
total_seq_length = args.burn_in + args.seq_length + args.seq_overlap
if args.target == 'tde':
total_seq_length += args.n_steps
sequence = np.arange(total_seq_length)
while True:
# load new memory
fread = lock_file(self.readlock)
traj_sorted = np.load(fread)
fread.seek(0)
np.save(fread, [])
release_file(fread)
if not len(traj_sorted):
continue
replay = np.concatenate(
[np.load(os.path.join(self.trajectory_dir, "%d.npy" % traj)) for traj in traj_sorted], axis=0)
replay_buffer = np.concatenate([replay_buffer, replay], axis=0)
offset = replay_buffer[-self.replay_memory_size]['fr'] - replay_buffer[-self.replay_memory_size]['fr_s'] \
if len(replay_buffer) >= self.replay_memory_size else 0
replay_buffer = replay_buffer[-self.replay_memory_size - offset:]
tde = replay['tde']
self.tde = np.concatenate([self.tde, tde])[-self.replay_memory_size - offset:]
prob = self.tde[:-total_seq_length]
prob = prob / prob.sum()
# save previous traj_old to file
np.save(self.list_old_path, np.array([traj_old]))
traj_old = replay_buffer['traj'][0]
print("Old trajectory: %d" % traj_old)
print("New Sample size is: %d" % len(replay))
len_replay_buffer = len(replay_buffer) - total_seq_length
minibatches = min(self.replay_updates_interval, int(len_replay_buffer / self.batch))
shuffle_indexes = np.random.choice(len_replay_buffer, (minibatches, self.batch), replace=False, p=prob)
print("Explorer:Replay Buffer size is: %d" % len_replay_buffer)
for i in range(minibatches):
samples = shuffle_indexes[i, :, None] + sequence
yield replay_buffer[samples]
def __len__(self):
return np.inf