-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate.py
More file actions
35 lines (25 loc) · 818 Bytes
/
Copy pathevaluate.py
File metadata and controls
35 lines (25 loc) · 818 Bytes
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
import numpy as np
import torch
def hit(gt_item, pred_items):
if gt_item in pred_items:
return 1
return 0
def ndcg(gt_item, pred_items):
if gt_item in pred_items:
index = pred_items.index(gt_item)
return np.reciprocal(np.log2(index+2))
return 0
def metrics(model, test_loader, top_k):
HR, NDCG = [], []
for user, item_i, item_j in test_loader:
user = user.cuda()
item_i = item_i.cuda()
item_j = item_j.cuda() # not useful when testing
prediction_i, prediction_j = model(user, item_i, item_j)
_, indices = torch.topk(prediction_i, top_k)
recommends = torch.take(
item_i, indices).cpu().numpy().tolist()
gt_item = item_i[0].item()
HR.append(hit(gt_item, recommends))
NDCG.append(ndcg(gt_item, recommends))
return np.mean(HR), np.mean(NDCG)