-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreplay_buffer.py
218 lines (183 loc) · 10.7 KB
/
replay_buffer.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
import numpy as np
import kornia
import torch
import torch.nn as nn
from algorithms.svea import random_conv
class ReplayBuffer(object):
"""Buffer to store environment transitions."""
def __init__(self, obs_shape, action_shape, capacity, image_pad, device, ted):
self.capacity = capacity
self.device = device
self.aug_trans = nn.Sequential(
nn.ReplicationPad2d(image_pad),
kornia.augmentation.RandomCrop((obs_shape[-1], obs_shape[-1])))
if len(obs_shape)==1:
self.obses = np.empty((capacity, *obs_shape), dtype=np.float32)
self.next_obses = np.empty((capacity, *obs_shape), dtype=np.float32)
else:
self.obses = np.empty((capacity, *obs_shape), dtype=np.uint8)
self.next_obses = np.empty((capacity, *obs_shape), dtype=np.uint8)
self.actions = np.empty((capacity, *action_shape), dtype=np.float32)
self.rewards = np.empty((capacity, 1), dtype=np.float32)
self.not_dones = np.empty((capacity, 1), dtype=np.float32)
self.not_dones_no_max = np.empty((capacity, 1), dtype=np.float32)
self.episode = np.empty((capacity, 1), dtype=np.float32)
self.idx = 0
self.full = False
self.ted = ted
def __len__(self):
return self.capacity if self.full else self.idx
def add(self, obs, action, reward, next_obs, done, done_no_max, episode):
np.copyto(self.obses[self.idx], obs)
np.copyto(self.actions[self.idx], action)
np.copyto(self.rewards[self.idx], reward)
np.copyto(self.next_obses[self.idx], next_obs)
np.copyto(self.not_dones[self.idx], not done)
np.copyto(self.not_dones_no_max[self.idx], not done_no_max)
np.copyto(self.episode[self.idx], episode)
self.idx = (self.idx + 1) % self.capacity
self.full = self.full or self.idx == 0
def sample(self, batch_size):
idxs = np.random.randint(0,
self.capacity if self.full else self.idx,
size=batch_size)
obses = self.obses[idxs]
next_obses = self.next_obses[idxs]
obses = torch.as_tensor(obses, device=self.device).float()
next_obses = torch.as_tensor(next_obses, device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
if self.ted:
idxs_from_same_episode = []
for i in range(batch_size):
# Create non-temporal same episode sample
sample_idx = idxs[i]
sample_episode = self.episode[sample_idx]
try:
all_same_episode_idxs = np.nonzero(self.episode == sample_episode)[0]
idx_to_remove = 1 - np.isin(all_same_episode_idxs, [sample_idx - 1, sample_idx, sample_idx + 1])
x = all_same_episode_idxs * idx_to_remove
reduced_same_episode_idxs = list(x[x != 0])
idx_from_same_episode = np.random.choice(reduced_same_episode_idxs)
except:
# In the rare case there are no other samples from the same episode, we use one from a different episode
idx_from_same_episode = np.random.choice([j for j in range(self.capacity if self.full else self.idx) if j not in [sample_idx - 1, sample_idx, sample_idx + 1]])
idxs_from_same_episode.append(idx_from_same_episode)
same_episode_obs = torch.as_tensor(self.next_obses[idxs_from_same_episode], device=self.device).float()
else:
same_episode_obs = None
return obses, actions, rewards, next_obses, not_dones_no_max, same_episode_obs
def sample_rad(self, batch_size):
idxs = np.random.randint(0,
self.capacity if self.full else self.idx,
size=batch_size)
obses = self.obses[idxs]
next_obses = self.next_obses[idxs]
obses = torch.as_tensor(obses, device=self.device).float()
next_obses = torch.as_tensor(next_obses, device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
obses = self.aug_trans(obses)
next_obses = self.aug_trans(next_obses)
if self.ted:
idxs_from_same_episode = []
for i in range(batch_size):
# Create non-temporal same episode sample
sample_idx = idxs[i]
sample_episode = self.episode[sample_idx]
try:
all_same_episode_idxs = np.nonzero(self.episode == sample_episode)[0]
idx_to_remove = 1 - np.isin(all_same_episode_idxs, [sample_idx - 1, sample_idx, sample_idx + 1])
x = all_same_episode_idxs * idx_to_remove
reduced_same_episode_idxs = list(x[x != 0])
idx_from_same_episode = np.random.choice(reduced_same_episode_idxs)
except:
# In the rare case there are no other samples from the same episode, we use one from a different episode
idx_from_same_episode = np.random.choice([j for j in range(self.capacity if self.full else self.idx) if j not in [sample_idx - 1, sample_idx, sample_idx + 1]])
idxs_from_same_episode.append(idx_from_same_episode)
same_episode_obs = torch.as_tensor(self.next_obses[idxs_from_same_episode], device=self.device).float()
same_episode_obs = self.aug_trans(same_episode_obs)
else:
same_episode_obs = None
return obses, actions, rewards, next_obses, not_dones_no_max, same_episode_obs
def sample_drq(self, batch_size):
idxs = np.random.randint(0,
self.capacity if self.full else self.idx,
size=batch_size)
obses = self.obses[idxs]
next_obses = self.next_obses[idxs]
obses_aug = obses.copy()
next_obses_aug = next_obses.copy()
obses = torch.as_tensor(obses, device=self.device).float()
next_obses = torch.as_tensor(next_obses, device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
obses_aug = torch.as_tensor(obses_aug, device=self.device).float()
next_obses_aug = torch.as_tensor(next_obses_aug, device=self.device).float()
obses = self.aug_trans(obses)
next_obses = self.aug_trans(next_obses)
obses_aug = self.aug_trans(obses_aug)
next_obses_aug = self.aug_trans(next_obses_aug)
if self.ted:
idxs_from_same_episode = []
for i in range(batch_size):
# Create non-temporal same episode sample
sample_idx = idxs[i]
sample_episode = self.episode[sample_idx]
try:
all_same_episode_idxs = np.nonzero(self.episode == sample_episode)[0]
idx_to_remove = 1 - np.isin(all_same_episode_idxs, [sample_idx - 1, sample_idx, sample_idx + 1])
x = all_same_episode_idxs * idx_to_remove
reduced_same_episode_idxs = list(x[x != 0])
idx_from_same_episode = np.random.choice(reduced_same_episode_idxs)
except:
# In the rare case there are no other samples from the same episode, we use one from a different episode
idx_from_same_episode = np.random.choice([j for j in range(self.capacity if self.full else self.idx) if j not in [sample_idx - 1, sample_idx, sample_idx + 1]])
idxs_from_same_episode.append(idx_from_same_episode)
same_episode_obs = torch.as_tensor(self.next_obses[idxs_from_same_episode], device=self.device).float()
same_episode_obs = self.aug_trans(same_episode_obs)
else:
same_episode_obs = None
return obses, actions, rewards, next_obses, not_dones_no_max, obses_aug, next_obses_aug, same_episode_obs
def sample_svea(self, batch_size):
idxs = np.random.randint(0,
self.capacity if self.full else self.idx,
size=batch_size)
obses = self.obses[idxs]
next_obses = self.next_obses[idxs]
obses_aug = obses.copy()
obses = torch.as_tensor(obses, device=self.device).float()
next_obses = torch.as_tensor(next_obses, device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
obses_aug = torch.as_tensor(obses_aug, device=self.device).float()
obses = self.aug_trans(obses)
next_obses = self.aug_trans(next_obses)
obses_aug = random_conv(obses_aug)
if self.ted:
idxs_from_same_episode = []
for i in range(batch_size):
# Create non-temporal same episode sample
sample_idx = idxs[i]
sample_episode = self.episode[sample_idx]
try:
all_same_episode_idxs = np.nonzero(self.episode == sample_episode)[0]
idx_to_remove = 1 - np.isin(all_same_episode_idxs, [sample_idx - 1, sample_idx, sample_idx + 1])
x = all_same_episode_idxs * idx_to_remove
reduced_same_episode_idxs = list(x[x != 0])
idx_from_same_episode = np.random.choice(reduced_same_episode_idxs)
except:
# In the rare case there are no other samples from the same episode, we use one from a different episode
idx_from_same_episode = np.random.choice(
[j for j in range(self.capacity if self.full else self.idx) if
j not in [sample_idx - 1, sample_idx, sample_idx + 1]])
idxs_from_same_episode.append(idx_from_same_episode)
same_episode_obs = torch.as_tensor(self.next_obses[idxs_from_same_episode], device=self.device).float()
same_episode_obs = self.aug_trans(same_episode_obs)
else:
same_episode_obs = None
return obses, actions, rewards, next_obses, not_dones_no_max, obses_aug, same_episode_obs