/
pure_example.py
66 lines (59 loc) · 2.11 KB
/
pure_example.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
import pandas as pd
from libreco.algorithms import LightGCN # pure data, algorithm LightGCN
from libreco.data import DatasetPure, random_split
from libreco.evaluation import evaluate
if __name__ == "__main__":
data = pd.read_csv(
"sample_data/sample_movielens_rating.dat",
sep="::",
names=["user", "item", "label", "time"],
)
# split whole data into three folds for training, evaluating and testing
train_data, eval_data, test_data = random_split(data, multi_ratios=[0.8, 0.1, 0.1])
train_data, data_info = DatasetPure.build_trainset(train_data)
eval_data = DatasetPure.build_evalset(eval_data)
test_data = DatasetPure.build_testset(test_data)
print(data_info) # n_users: 5894, n_items: 3253, data sparsity: 0.4172 %
lightgcn = LightGCN(
task="ranking",
data_info=data_info,
loss_type="bpr",
embed_size=16,
n_epochs=3,
lr=1e-3,
batch_size=2048,
num_neg=1,
device="cuda",
)
# monitor metrics on eval_data during training
lightgcn.fit(
train_data,
neg_sampling=True, # sample negative items for train and eval data
verbose=2,
eval_data=eval_data,
metrics=["loss", "roc_auc", "precision", "recall", "ndcg"],
)
# do final evaluation on test data
print(
"evaluate_result: ",
evaluate(
model=lightgcn,
data=test_data,
neg_sampling=True, # sample negative items for test data
metrics=["loss", "roc_auc", "precision", "recall", "ndcg"],
),
)
# predict preference of user 2211 to item 110
print("prediction: ", lightgcn.predict(user=2211, item=110))
# recommend 7 items for user 2211
print("recommendation: ", lightgcn.recommend_user(user=2211, n_rec=7))
# cold-start prediction
print(
"cold prediction: ",
lightgcn.predict(user="ccc", item="not item", cold_start="average"),
)
# cold-start recommendation
print(
"cold recommendation: ",
lightgcn.recommend_user(user="are we good?", n_rec=7, cold_start="popular"),
)