-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
260 lines (202 loc) · 9.24 KB
/
dataset.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
from abc import ABC, abstractmethod
from typing import Dict, Tuple, List
import os
import numpy as np
import torch
class AbstractReplayBuffer(ABC):
@abstractmethod
def add(self,
time_step: int):
pass
@abstractmethod
def __next__(self):
pass
@abstractmethod
def __len__(self) -> int:
pass
class NumpyReplayBuffer(AbstractReplayBuffer):
def __init__(self) -> None:
super().__init__()
self.mean: float = 0
self.std: float = 1
self.data: Dict[str, np.ndarray] = None
@property
def size(self):
return self.data["states"].shape[0]
def sample(self,
batch_size: int) -> Dict[str, np.ndarray]:
indexes = np.random.randint(size=batch_size, low=0, high=self.size)
return {
"states": self.data["observations"][indexes],
"actions": self.data["actions"][indexes],
"rewards": self.data["rewards"][indexes],
"next_states": self.data["next_observations"][indexes],
"terminals": self.data["terminals"][indexes]
}
def get_moments(self, modality: str, eps: float = 1e-3) -> Tuple[np.ndarray, np.ndarray]:
mean = self.data[modality].mean(0)
std = self.data[modality].std(0)
return mean, std + eps
def from_d4rl(self,
dataset: Dict[str, np.ndarray],
normalize: bool = False) -> None:
buffer = dataset
if normalize:
self.mean, self.std = self.compute_mean_std(buffer["observations"])
buffer["observations"] = self.normalize_states(buffer["observations"], self.mean, self.std)
buffer["next_observations"] = self.normalize_states(buffer["next_observations"], self.mean, self.std)
self.data = buffer
print("d4rl dataset has been downloader to ReplayBuffer")
@staticmethod
def compute_mean_std(states: np.ndarray,
eps: float = 1e-3) -> Tuple[np.ndarray, np.ndarray]:
mean = states.mean(0)
std = states.std(0) + eps
return mean, std
@staticmethod
def normalize_states(state: np.ndarray,
mean: np.ndarray,
std: np.ndarray) -> np.ndarray:
return (state - mean) / std
def from_json(self, json_file: str):
import json
if not json_file.endswith('.json'):
json_file = json_file + '.json'
json_file = os.path.join("json_datasets", json_file)
output = dict()
with open(json_file) as f:
dataset = json.load(f)
for k, v in dataset.items():
v = np.array(v)
if k != "terminals":
v = v.astype(np.float32)
output[k] = v
self.from_d4rl(output)
class ReplayBuffer:
def __init__(self,
state_dim: int,
action_dim: int,
n_step: int = 3,
buffer_size: int = 1000000) -> None:
self.n_step = n_step
self.state_dim = state_dim
self.action_dim = action_dim
self.buffer_size = buffer_size
self.pointer = 0
self.size = 0
device = "cpu"
self.device = device
self.states = torch.zeros((buffer_size, state_dim), dtype=torch.float32, device=device)
self.actions = torch.zeros((buffer_size, action_dim), dtype=torch.float32, device=device)
self.rewards = torch.zeros((buffer_size, 1), dtype=torch.float32, device=device)
self.next_states = torch.zeros((buffer_size, state_dim), dtype=torch.float32, device=device)
self.dones = torch.zeros((buffer_size, 1), dtype=torch.float32, device=device)
# i/o order: state, action, reward, next_state, next_action, done
def from_json(self, json_file: str):
import json
if not json_file.endswith('.json'):
json_file = json_file + '.json'
json_file = os.path.join("json_datasets", json_file)
output = dict()
with open(json_file) as f:
dataset = json.load(f)
for k, v in dataset.items():
v = np.array(v)
if k != "terminals":
v = v.astype(np.float32)
output[k] = v
self.from_d4rl(output)
def get_moments(self) -> Tuple[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]:
state_mean, state_std = self.states.mean(dim=0), self.states.std(dim=0)
action_mean, action_std = self.actions.mean(dim=0), self.actions.std(dim=0)
return (state_mean, state_std), (action_mean, action_std)
@staticmethod
def to_tensor(data: np.ndarray, device=None) -> torch.Tensor:
if device is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
return torch.tensor(data, dtype=torch.float32, device=device)
def sample(self, batch_size: int) -> Tuple[torch.Tensor]:
indexes = np.random.randint(0, self.size, size=batch_size)
return (
self.states[indexes],
self.actions[indexes],
self.rewards[indexes],
self.next_states[indexes],
self.actions[indexes + self.n_step],
self.dones[indexes]
)
def from_d4rl(self, dataset):
if self.size:
print("Warning: loading data into non-empty buffer")
n_transitions = dataset["observations"].shape[0]
if n_transitions < self.buffer_size:
self.states[:n_transitions] = self.to_tensor(dataset["observations"][-n_transitions:], self.device)
self.actions[:n_transitions] = self.to_tensor(dataset["actions"][-n_transitions:], self.device)
self.next_states[:n_transitions] = self.to_tensor(dataset["next_observations"][-n_transitions:], self.device)
self.rewards[:n_transitions] = self.to_tensor(dataset["rewards"][-n_transitions:].reshape(-1, 1), self.device)
self.dones[:n_transitions] = self.to_tensor(dataset["terminals"][-n_transitions:].reshape(-1, 1), self.device)
else:
self.buffer_size = n_transitions
self.states = self.to_tensor(dataset["observations"][-n_transitions:], self.device)
self.actions = self.to_tensor(dataset["actions"][-n_transitions:])
self.next_states = self.to_tensor(dataset["next_observations"][-n_transitions:], self.device)
self.rewards = self.to_tensor(dataset["rewards"][-n_transitions:].reshape(-1, 1), self.device)
self.dones = self.to_tensor(dataset["terminals"][-n_transitions:].reshape(-1, 1), self.device)
self.size = n_transitions
self.pointer = n_transitions % self.buffer_size
def from_d4rl_finetune(self, dataset):
raise NotImplementedError()
def normalize_states(self, eps=1e-3):
mean = self.states.mean(0, keepdim=True)
std = self.states.std(0, keepdim=True) + eps
self.states = (self.states - mean) / std
self.next_states = (self.next_states - mean) / std
return mean, std
def clip(self, eps=1e-5):
self.actions = torch.clip(self.actions, - 1 + eps, 1 - eps)
def add_transition(self,
state: torch.Tensor,
action: torch.Tensor,
reward: torch.Tensor,
next_state: torch.Tensor,
done: torch.Tensor):
if not isinstance(state, torch.Tensor):
state = self.to_tensor(state, self.device)
action = self.to_tensor(action, self.device)
reward = self.to_tensor(reward, self.device)
next_state = self.to_tensor(next_state, self.device)
done = self.to_tensor(done, self.device)
self.states[self.pointer] = state
self.actions[self.pointer] = action
self.rewards[self.pointer] = reward
self.next_states[self.pointer] = next_state
self.dones[self.pointer] = done
self.pointer = (self.pointer + 1) % self.buffer_size
self.size = min(self.size + 1, self.buffer_size)
def add_batch(self,
states: List[torch.Tensor],
actions: List[torch.Tensor],
rewards: List[torch.Tensor],
next_states: List[torch.Tensor],
dones: List[torch.Tensor]):
for state, action, reward, next_state, done in zip(states, actions, rewards, next_states, dones):
self.add_transition(state, action, reward, next_state, done)
@staticmethod
def dataset_stats(dataset):
episode_returns = []
returns = 0
episode_length = 0
for reward, done in zip(dataset["rewards"], dataset["terminals"]):
if done:
episode_returns.append(returns)
returns = 0
episode_length = 0
else:
episode_length += 1
returns += reward
if episode_length == 1000:
episode_returns.append(returns)
returns = 0
episode_length = 0
episode_returns = np.array(episode_returns)
return episode_returns.mean(), episode_returns.std()