-
Notifications
You must be signed in to change notification settings - Fork 113
/
utils.py
executable file
·276 lines (225 loc) · 8.22 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import numpy as np
import torch
from .pandas_backend import pd
# helper function similar to pandas.Series.rolling
def rolling_window(a, window):
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
def get_irsu(batch):
items_t, ratings_t, sizes_t, users_t = (
batch["items"],
batch["ratings"],
batch["sizes"],
batch["users"],
)
return items_t, ratings_t, sizes_t, users_t
def batch_no_embeddings(batch, frame_size, *args, **kwargs):
"""
Embed Batch: discrete state discrete action
"""
items_t, ratings_t, sizes_t, users_t = get_irsu(batch)
b_size = ratings_t.size(0)
items = items_t[:, :-1]
next_items = items_t[:, 1:]
ratings = ratings_t[:, :-1]
next_ratings = ratings_t[:, 1:]
action = items_t[:, -1]
reward = ratings_t[:, -1]
done = torch.zeros(b_size)
done[torch.cumsum(sizes_t - frame_size, dim=0) - 1] = 1
batch = {
"items": items,
"next_items": next_items,
ratings: "ratings",
"next_ratings": next_ratings,
"action": action,
"reward": reward,
"done": done,
"meta": {"users": users_t, "sizes": sizes_t},
}
return batch
def batch_tensor_embeddings(batch, item_embeddings_tensor, frame_size, *args, **kwargs):
"""
Embed Batch: continuous state continuous action
"""
items_t, ratings_t, sizes_t, users_t = get_irsu(batch)
items_emb = item_embeddings_tensor[items_t.long()]
b_size = ratings_t.size(0)
items = items_emb[:, :-1, :].view(b_size, -1)
next_items = items_emb[:, 1:, :].view(b_size, -1)
ratings = ratings_t[:, :-1]
next_ratings = ratings_t[:, 1:]
state = torch.cat([items, ratings], 1)
next_state = torch.cat([next_items, next_ratings], 1)
action = items_emb[:, -1, :]
reward = ratings_t[:, -1]
done = torch.zeros(b_size)
done[torch.cumsum(sizes_t - frame_size, dim=0) - 1] = 1
batch = {
"state": state,
"action": action,
"reward": reward,
"next_state": next_state,
"done": done,
"meta": {"users": users_t, "sizes": sizes_t},
}
return batch
def batch_contstate_discaction(
batch, item_embeddings_tensor, frame_size, num_items, *args, **kwargs
):
"""
Embed Batch: continuous state discrete action
"""
items_t, ratings_t, sizes_t, users_t = get_irsu(batch)
items_emb = item_embeddings_tensor[items_t.long()]
b_size = ratings_t.size(0)
items = items_emb[:, :-1, :].view(b_size, -1)
next_items = items_emb[:, 1:, :].view(b_size, -1)
ratings = ratings_t[:, :-1]
next_ratings = ratings_t[:, 1:]
state = torch.cat([items, ratings], 1)
next_state = torch.cat([next_items, next_ratings], 1)
action = items_t[:, -1]
reward = ratings_t[:, -1]
done = torch.zeros(b_size)
done[torch.cumsum(sizes_t - frame_size, dim=0) - 1] = 1
one_hot_action = torch.zeros(b_size, num_items)
one_hot_action.scatter_(1, action.view(-1, 1), 1)
batch = {
"state": state,
"action": one_hot_action,
"reward": reward,
"next_state": next_state,
"done": done,
"meta": {"users": users_t, "sizes": sizes_t},
}
return batch
# pads stuff to work with lstms
def padder(x):
items_t = []
ratings_t = []
sizes_t = []
users_t = []
for i in range(len(x)):
items_t.append(torch.tensor(x[i]["items"]))
ratings_t.append(torch.tensor(x[i]["rates"]))
sizes_t.append(x[i]["sizes"])
users_t.append(x[i]["users"])
items_t = torch.nn.utils.rnn.pad_sequence(items_t, batch_first=True).long()
ratings_t = torch.nn.utils.rnn.pad_sequence(ratings_t, batch_first=True).float()
sizes_t = torch.tensor(sizes_t).float()
return {"items": items_t, "ratings": ratings_t, "sizes": sizes_t, "users": users_t}
def sort_users_itemwise(user_dict, users):
return (
pd.get()
.Series(dict([(i, user_dict[i]["items"].shape[0]) for i in users]))
.sort_values(ascending=False)
.index
)
def prepare_batch_dynamic_size(batch, item_embeddings_tensor, embed_batch=None):
item_idx, ratings_t, sizes_t, users_t = get_irsu(batch)
item_t = item_embeddings_tensor[item_idx]
batch = {"items": item_t, "users": users_t, "ratings": ratings_t, "sizes": sizes_t}
return batch
# Main function that is used as torch.DataLoader->collate_fn
# CollateFn docs:
# https://pytorch.org/docs/stable/data.html#working-with-collate-fn
def prepare_batch_static_size(
batch, item_embeddings_tensor, frame_size=10, embed_batch=batch_tensor_embeddings
):
item_t, ratings_t, sizes_t, users_t = [], [], [], []
for i in range(len(batch)):
item_t.append(batch[i]["items"])
ratings_t.append(batch[i]["rates"])
sizes_t.append(batch[i]["sizes"])
users_t.append(batch[i]["users"])
item_t = np.concatenate([rolling_window(i, frame_size + 1) for i in item_t], 0)
ratings_t = np.concatenate(
[rolling_window(i, frame_size + 1) for i in ratings_t], 0
)
item_t = torch.tensor(item_t)
users_t = torch.tensor(users_t)
ratings_t = torch.tensor(ratings_t).float()
sizes_t = torch.tensor(sizes_t)
batch = {"items": item_t, "users": users_t, "ratings": ratings_t, "sizes": sizes_t}
return embed_batch(
batch=batch,
item_embeddings_tensor=item_embeddings_tensor,
frame_size=frame_size,
)
# Usually in data sets there item index is inconsistent (if you plot it doesn't look like a line)
# This function makes the index linear, allows for better compression of the data
# And also makes use of tensor[tensor] semantics
# items_embeddings_key_dict:arg - item embeddings by key
# include_zero:arg - whether to include items_embeddings_id_dict[0] = [0, 0, 0, ..., 0] (128)
# sometimes needed for rnn padding, by default True
# returns:
# items_embeddings_tensor - items_embeddings_dict compressed into tensor
# key_to_id - dict key -> index
# id_to_key - dict index -> key
def make_items_tensor(items_embeddings_key_dict):
keys = list(sorted(items_embeddings_key_dict.keys()))
key_to_id = dict(zip(keys, range(len(keys))))
id_to_key = dict(zip(range(len(keys)), keys))
items_embeddings_id_dict = {}
for k in items_embeddings_key_dict.keys():
items_embeddings_id_dict[key_to_id[k]] = items_embeddings_key_dict[k]
items_embeddings_tensor = torch.stack(
[items_embeddings_id_dict[i] for i in range(len(items_embeddings_id_dict))]
)
return items_embeddings_tensor, key_to_id, id_to_key
class ReplayBuffer:
def __init__(self, buffer_size, layout):
self.buffer = None
self.idx = 0
self.size = buffer_size
self.layout = layout
self.meta = {"step": []}
self.flush()
def flush(self):
# state, action, reward, next_state
del self.buffer
self.buffer = [torch.zeros(i) for i in self.layout]
self.idx = 0
self.meta["step"] = []
def append(self, batch):
state, action, reward, next_state, step = (
batch["state"],
batch["action"],
batch["reward"],
batch["next_state"],
batch["step"],
)
self.meta["step"].append(step)
lower = self.idx
upper = state.size(0) + lower
self.buffer[0][lower:upper] = state
self.buffer[1][lower:upper] = action
self.buffer[2][lower:upper] = reward
self.buffer[3][lower:upper] = next_state
self.idx = upper
def get(self):
state, action, reward, next_state = self.buffer
batch = {
"state": state,
"action": action,
"reward": reward,
"next_state": next_state,
"meta": self.meta,
}
return batch
def len(self):
return self.idx
def get_base_batch(batch, device=torch.device("cuda"), done=True):
b = [
batch["state"],
batch["action"],
batch["reward"].unsqueeze(1),
batch["next_state"],
]
if done:
b.append(batch["done"].unsqueeze(1))
else:
batch.append(torch.zeros_like(batch["reward"]))
return [i.to(device) for i in b]