forked from FuCongResearchSquad/GNOLR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kuairand_recall_k.py
More file actions
419 lines (319 loc) · 15.1 KB
/
Copy pathtest_kuairand_recall_k.py
File metadata and controls
419 lines (319 loc) · 15.1 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import sys
import numpy as np
from collections import defaultdict
def load_embeddings(recall_index_path):
embeddings = []
with open(recall_index_path, 'rb') as f:
while True:
try:
emb = np.load(f)
embeddings.append(emb)
except ValueError:
break
return np.concatenate(embeddings, axis=0)
def summary_label(sample_label):
# Sample_label: [user_id, item_id, is_like, is_follow]
print(sample_label.shape)
# Sample_id(user_id/item_id)
sample_id = sample_label[:, [0, 1]].astype(int)
label = sample_label[:, [2, 3]].astype(int)
# SampleID
print("First 5 sample_ids:")
print(sample_id[:5])
# Label
print("\nFirst 5 embedding rows:")
print(label[:5])
def check_consistency_label(sample_label):
sample_label = sample_label.astype(int)
# pair <user_id, item_id> for label consistency check
user_item_pairs = sample_label[:, [0, 1]]
unique_pairs, counts = np.unique(
user_item_pairs, axis=0, return_counts=True)
duplicate_pairs = unique_pairs[counts > 1]
if len(duplicate_pairs) == 0:
print("No duplicate pairs found.")
return
print(f"Found {len(duplicate_pairs)} duplicate pairs.")
# check label consistency
for pair in duplicate_pairs:
indices = np.where((user_item_pairs[:, 0] == pair[0]) & (
user_item_pairs[:, 1] == pair[1]))[0]
labels = sample_label[indices, 2:] # get label
if not np.all(np.array_equal(labels[0], label) for label in labels):
print(f"\nInconsistent labels found for duplicate pair: {pair}")
print("Matching rows:")
print(sample_label[indices])
def summary_data(total_embedding):
# Total Embedding[Duplicate SampleID]
print("Total Shape: ", total_embedding.shape)
# Sample_id(user_id/item_id)
sample_id = total_embedding[:, 0].astype(int)
embedding_values = total_embedding[:, 1:]
# SampleID
print("First 5 sample_ids:")
print(sample_id[:5])
print("Unique Cnt: ", len(np.unique(sample_id))) # Unique Cnt
# Embedding
print("\nFirst 5 embedding rows:")
print(embedding_values[:5])
def check_consistency_data(total_embedding):
# 0. Parser SampleId[item_id/user_id] & Embedding
sample_id = total_embedding[:, 0].astype(int)
embedding_values = total_embedding[:, 1:]
# 1. Duplicated SampleId
unique_ids, counts = np.unique(sample_id, return_counts=True)
duplicate_ids = unique_ids[counts > 1]
print(f"Found {len(duplicate_ids)} duplicate sample_ids.")
# 2. Check Embedding Consistency
inconsistency_flag = False
for dup_id in duplicate_ids:
indices = np.where(sample_id == dup_id)[0]
dup_embeddings = embedding_values[indices]
if not np.all(np.array_equal(dup_embeddings[0], emb) for emb in dup_embeddings):
inconsistency_flag = True
break
if inconsistency_flag:
print("[WARNING]duplicate sample_ids embedding [In-Consistency...]")
else:
print("[INFO]duplicate sample_ids have consistent embeddings.")
def deduplicated_data(total_embedding):
# SampleId(User_id or Item_id) & Embedding
sample_id = total_embedding[:, 0].astype(int)
embedding_values = total_embedding[:, 1:]
# Deduplicate
unique_embeddings = {}
for idx, s_id in enumerate(sample_id):
unique_embeddings[s_id] = embedding_values[idx]
# Sample_id (user_id/item_id)
unique_sample_ids = np.array(list(unique_embeddings.keys()))
# Embedding
unique_embedding_values = np.array(list(unique_embeddings.values()))
print(unique_sample_ids.shape, unique_sample_ids[:5])
print(unique_embedding_values.shape, unique_embedding_values[:5])
return unique_sample_ids, unique_embedding_values
def gen_label_dict(sample_label, reverse=False):
# [user_id, item_id, is_like, is_follow]
sample_label = sample_label.astype(int)
# sample_id [user_id, item_id]
sample_ids = sample_label[:, [0, 1]]
is_like_dict = defaultdict(set)
is_follow_dict = defaultdict(set)
for row in sample_label:
user_id, item_id, is_like_label, is_follow_label = row
if is_like_label == 1:
if reverse == False:
is_like_dict[user_id].add(item_id)
else:
assert reverse == True
is_follow_dict[user_id].add(item_id)
if is_follow_label == 1:
if reverse == False:
is_follow_dict[user_id].add(item_id)
else:
assert reverse == True
is_like_dict[user_id].add(item_id)
return sample_ids, is_like_dict, is_follow_dict
def recall_k(unique_user_id, unique_user_embedding, unique_item_id, unique_item_embedding, is_like_dict, is_follow_dict, K=10):
"""
Calculate the recall@K for each user.
Args:
- unique_user_id: all user_id.
- unique_user_embedding: embedding of each user_id. (shape: [num_users, embedding_dim])
- unique_item_id: all item_id.
- unique_item_embedding: embedding of each item_id. (shape: [num_items, embedding_dim])
- is_like_dict: A dict containing positive sample interactions (is_like). {user_id: {item_id_set}}
- is_follow_dict: A dict containing positive sample interactions (is_follow). {user_id: {item_id_set}}
- K: int (default: 10)
Return:
- is_like_recall_at_k
- is_follow_recall_at_k
"""
recall_is_like_at_k = {}
recall_is_follow_at_k = {}
item_embedding_matrix = np.array(unique_item_embedding)
item_ids = np.array(unique_item_id)
for user_idx, user_id in enumerate(unique_user_id):
# user embedding
user_embedding = np.array(unique_user_embedding[user_idx])
similarities = np.dot(item_embedding_matrix, user_embedding)
sorted_indices = np.argsort(-similarities)
top_k_indices = sorted_indices[:K]
top_k_items = item_ids[top_k_indices]
# is_like per user_id
positive_items_like = is_like_dict.get(user_id, set())
if len(positive_items_like) > 0:
num_positive_in_top_k_like = len(
set(top_k_items) & positive_items_like)
recall_like = num_positive_in_top_k_like / len(positive_items_like)
else:
recall_like = 0.0
# is_follow per user_id
positive_items_follow = is_follow_dict.get(user_id, set())
if len(positive_items_follow) > 0:
num_positive_in_top_k_follow = len(
set(top_k_items) & positive_items_follow)
recall_follow = num_positive_in_top_k_follow / \
len(positive_items_follow)
else:
recall_follow = 0.0
recall_is_like_at_k[user_id] = recall_like
recall_is_follow_at_k[user_id] = recall_follow
# avg per user
avg_recall_like = np.mean(list(recall_is_like_at_k.values()))
avg_recall_follow = np.mean(list(recall_is_follow_at_k.values()))
return avg_recall_like, avg_recall_follow
def recall_k_session(unique_user_id, unique_user_embedding, unique_item_id, unique_item_embedding, sample_dict, is_like_dict, is_follow_dict, Ks=[20, 50, 100, 200, 500]):
"""
Calculate recall@K within inner-session.
"""
recall_is_like_at_ks = {K: {} for K in Ks}
recall_is_follow_at_ks = {K: {} for K in Ks}
item_embedding_matrix = np.array(unique_item_embedding)
item_ids = np.array(unique_item_id)
# Inner-session candidate item_id
user_to_candidate_items = {}
for user_id, item_id in sample_dict:
if user_id not in user_to_candidate_items:
user_to_candidate_items[user_id] = set()
user_to_candidate_items[user_id].add(item_id)
for user_idx, user_id in enumerate(unique_user_id):
# user embedding
user_embedding = np.array(unique_user_embedding[user_idx])
candidate_items_id = user_to_candidate_items.get(user_id, set())
print(
f"[INFO]Index: {user_idx}, User_id: {user_id}, Inner-Session Candidates Items Cnt: {len(candidate_items_id)}")
if not candidate_items_id:
for K in Ks:
recall_is_like_at_ks[K][user_id] = 0.0
recall_is_follow_at_ks[K][user_id] = 0.0
continue
candidates_item_indices = [np.where(item_ids == item_id)[
0][0] for item_id in candidate_items_id]
candidates_item_embedding = item_embedding_matrix[candidates_item_indices]
# cal similarity
similarities = np.dot(candidates_item_embedding, user_embedding)
sorted_indices = np.argsort(-similarities)
candidate_items_array = np.array(list(candidate_items_id))
for K in Ks:
top_k_indices = sorted_indices[:K]
top_k_items = candidate_items_array[top_k_indices]
# is_like per user_id
positive_items_like = is_like_dict.get(user_id, set())
if len(positive_items_like) > 0:
num_positive_in_top_k_like = len(
set(top_k_items) & positive_items_like)
recall_like = num_positive_in_top_k_like / \
len(positive_items_like)
else:
recall_like = 0.0
# is_follow per user_id
positive_items_follow = is_follow_dict.get(user_id, set())
if len(positive_items_follow) > 0:
num_positive_in_top_k_follow = len(
set(top_k_items) & positive_items_follow)
recall_follow = num_positive_in_top_k_follow / \
len(positive_items_follow)
else:
recall_follow = 0.0
recall_is_like_at_ks[K][user_id] = recall_like
recall_is_follow_at_ks[K][user_id] = recall_follow
# avg per user
avg_recall_is_like_at_ks = {K: np.mean(
list(recall_is_like_at_ks[K].values())) for K in Ks}
avg_recall_is_follow_at_ks = {K: np.mean(
list(recall_is_follow_at_ks[K].values())) for K in Ks}
return avg_recall_is_like_at_ks, avg_recall_is_follow_at_ks
def recall_k_split_embedding(unique_user_id, unique_user_embedding, unique_item_id, unique_item_embedding, sample_dict, is_like_dict, is_follow_dict, Ks=[20, 50, 100, 200, 500]):
"""
Use the first 32 dimensions and the last 32 dimensions of user_embedding and item_embedding to calculate recall@K for is_like and is_follow, respectively.
Args:
- user_embedding_array: An array containing user_id and its corresponding embedding. (shape: [num_users, 65])
- item_embedding_array: An array containing item_id and its corresponding embedding. (shape: [num_items, 65])
- sample_label: Arrays containing (user_id, item_id, is_like_label, is_follow_label). (shape: [num_samples, 4])
- Ks (list): Contains multiple K values that need to be calculated.
Return:
- avg_recall_like_at_ks (the first 32 dimensions)
- avg_recall_like_at_ks (last 32 dimensions)
"""
user_embeddings_front = unique_user_embedding[:, :32]
user_embeddings_back = unique_user_embedding[:, 32:]
item_embeddings_front = unique_item_embedding[:, :32]
item_embeddings_back = unique_item_embedding[:, 32:]
# <user_id, item_id> label
sample_user_ids = sample_dict[:, 0].astype(int)
sample_item_ids = sample_dict[:, 1].astype(int)
def compute_recall(user_embeddings, item_embeddings, positive_dict):
"""
Calculate recall@K.
"""
recall_at_ks = {K: {} for K in Ks}
for user_idx, user_id in enumerate(unique_user_id):
user_embedding = user_embeddings[user_idx]
candidate_item_indices = np.where(sample_user_ids == user_id)[0]
candidate_item_ids = sample_item_ids[candidate_item_indices]
print(
f"[INFO]Index: {user_idx}, User_id: {user_id}, Inner-Session Candidates Items Cnt: {len(candidate_item_ids)}")
if len(candidate_item_ids) == 0:
for K in Ks:
recall_at_ks[K][user_id] = 0.0
continue
candidate_embeddings = item_embeddings[
[np.where(unique_item_id == item_id)[0][0]
for item_id in candidate_item_ids]
]
similarities = np.dot(candidate_embeddings, user_embedding)
sorted_indices = np.argsort(-similarities)
candidate_items_sorted = candidate_item_ids[sorted_indices]
for K in Ks:
top_k_items = candidate_items_sorted[:K]
positive_items = positive_dict.get(user_id, set())
if len(positive_items) > 0:
num_positive_in_top_k = len(
set(top_k_items) & positive_items)
recall = num_positive_in_top_k / len(positive_items)
else:
recall = 0.0
recall_at_ks[K][user_id] = recall
avg_recall_at_ks = {K: np.mean(
list(recall_at_ks[K].values())) for K in Ks}
return avg_recall_at_ks
avg_recall_like_at_ks = compute_recall(
user_embeddings_front, item_embeddings_front, is_like_dict)
avg_recall_follow_at_ks = compute_recall(
user_embeddings_back, item_embeddings_back, is_follow_dict)
return avg_recall_like_at_ks, avg_recall_follow_at_ks
if __name__ == '__main__':
recall_user_index_path = sys.argv[1]
recall_item_index_path = sys.argv[2]
label_index_path = sys.argv[3]
user_total_embedding = load_embeddings(recall_user_index_path)
item_total_embedding = load_embeddings(recall_item_index_path)
sample_label = load_embeddings(label_index_path)
# 1. Data Check
# Embedding Check
summary_data(item_total_embedding)
check_consistency_data(item_total_embedding)
# Lable Check
summary_label(sample_label)
check_consistency_label(sample_label)
# 2. Deduplicate user_embedding / item_embedding
unique_user_id, unique_user_embedding = deduplicated_data(
user_total_embedding)
unique_item_id, unique_item_embedding = deduplicated_data(
item_total_embedding)
# Gen sample_id & label
sample_ids, is_like_dict, is_follow_dict = gen_label_dict(
sample_label, reverse=True)
# 3.0 Recall@K
# avg_recall_like, avg_recall_follow = recall_k(unique_user_id, unique_user_embedding, unique_item_id, unique_item_embedding, is_like_dict, is_follow_dict, K)
# 3.1. Recall@K Session for Kuairand
# avg_recall_is_like_at_ks, avg_recall_is_follow_at_ks = recall_k_session(unique_user_id, unique_user_embedding, unique_item_id, unique_item_embedding, sample_ids, is_like_dict, is_follow_dict, [5, 10, 15, 20, 50, 100, 200, 500])
# 3.1. Recall@K Session for NSB
avg_recall_is_like_at_ks, avg_recall_is_follow_at_ks = recall_k_split_embedding(
unique_user_id, unique_user_embedding, unique_item_id, unique_item_embedding, sample_ids, is_like_dict, is_follow_dict, [5, 10, 15, 20, 50, 100, 200, 500])
print("Average Recall@Ks for is_like:")
for K, avg_recall in avg_recall_is_like_at_ks.items():
print(f"Recall@{K}: {avg_recall:.8f}")
print("\nAverage Recall@Ks for is_follow:")
for K, avg_recall in avg_recall_is_follow_at_ks.items():
print(f"Recall@{K}: {avg_recall:.8f}")