Skip to content

Commit 48bb439

Browse files
committed
update 2021.11.21
1 parent 844f99a commit 48bb439

File tree

18 files changed

+704
-207
lines changed

18 files changed

+704
-207
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/reclearn.egg-info/*
66
/build/*
77
*/__pycache__
8+
**/__pycache__

example/m_attrec_demo.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,71 @@
1-
from tensorflow.keras.optimizers import Adam
1+
"""
2+
Created on Nov 20, 2021
3+
train AttRec demo
4+
@author: Ziyao Geng([email protected])
5+
"""
6+
import os
27
from time import time
8+
from tensorflow.keras.optimizers import Adam
9+
310
from reclearn.models.matching import AttRec
411
from reclearn.data.datasets import movielens as ml
512
from reclearn.evaluator import eval_pos_neg
6-
import os
13+
from reclearn.data.feature_column import sparseFeature
714

815
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
916
os.environ['CUDA_VISIBLE_DEVICES'] = '6'
1017

18+
# Hyper parameters
19+
neg_num = 4
20+
embed_dim = 64
21+
seq_len = 200
22+
learning_rate = 0.001
23+
epochs = 20
24+
batch_size = 512
25+
26+
model_params = {
27+
'seq_len': seq_len,
28+
'mode': 'inner',
29+
'w': 0.3,
30+
'loss_name': 'hinge_loss',
31+
'gamma': 0.5,
32+
'embed_reg': 0.
33+
}
34+
35+
k = 10
36+
1137

1238
def main():
13-
epochs = 20
14-
learning_rate = 0.001
15-
batch_size = 512
16-
neg_num = 1
17-
seq_len = 5
18-
k = 10
19-
test_neg_num = 100
2039
file_path = 'data/ml-1m/ratings.dat'
21-
user_num, item_num, train_path, val_path, test_path = ml.load_seq_movielens(file_path=file_path)
22-
train_data = ml.load_seq_ml(train_path, "train", neg_num, seq_len, contain_user=True)
23-
val_data = ml.load_seq_ml(val_path, "val", neg_num, seq_len, contain_user=True)
24-
test_data = ml.load_seq_ml(test_path, "test", test_neg_num, seq_len, contain_user=True)
40+
# TODO: 1. Split Data
41+
train_path, val_path, test_path, meta_path = ml.split_seq_movielens(file_path=file_path)
42+
with open(meta_path) as f:
43+
max_user_num, max_item_num = [int(x) for x in f.readline().strip('\n').split('\t')]
44+
# TODO: 2. Build Feature Columns
2545
fea_cols = {
26-
'user_num': user_num,
27-
'item_num': item_num,
28-
'seq_len': seq_len,
29-
'embed_dim': 64
30-
}
31-
params = {
32-
'fea_cols': fea_cols,
33-
'mode': 'inner',
34-
'loss_name': 'hinge_loss',
35-
'gamma': 0.5,
36-
'w': 0.5,
37-
'embed_reg': 0.
46+
'item': sparseFeature('item', max_item_num + 1, embed_dim),
47+
'user': sparseFeature('user', max_user_num + 1, embed_dim)
3848
}
39-
model = AttRec(**params)
40-
model.summary()
49+
# TODO: 3. Load Data
50+
train_data = ml.load_seq_ml(train_path, "train", seq_len, neg_num, max_item_num, contain_user=True)
51+
val_data = ml.load_seq_ml(val_path, "val", seq_len, neg_num, max_item_num, contain_user=True)
52+
test_data = ml.load_seq_ml(test_path, "test", seq_len, 100, max_item_num, contain_user=True)
53+
# TODO: 4. Build Model
54+
model = AttRec(fea_cols, **model_params)
4155
model.compile(optimizer=Adam(learning_rate=learning_rate))
42-
for epoch in range(1, epochs+1):
56+
# TODO: 5. Fit Model
57+
for epoch in range(1, epochs + 1):
4358
t1 = time()
4459
model.fit(
4560
x=train_data,
46-
validation_data=val_data,
4761
epochs=1,
62+
validation_data=val_data,
4863
batch_size=batch_size
4964
)
50-
eval_dict = eval_pos_neg(model, test_data, batch_size, ["hr", "ndcg"], k)
65+
eval_dict = eval_pos_neg(model, test_data, ['hr', 'mrr', 'ndcg'], k, batch_size)
5166
t2 = time()
52-
print('Iteration %d Fit [%.1f s], Evaluate [%.1f s]: HR = %.4f, NDCG = %.4f, '
53-
% (epoch, t2 - t1, time() - t2, eval_dict['hr'], eval_dict['ndcg']))
67+
print('Iteration %d Fit [%.1f s], Evaluate [%.1f s]: HR = %.4f, MRR = %.4f, NDCG = %.4f'
68+
% (epoch, t2 - t1, time() - t2, eval_dict['hr'], eval_dict['mrr'], eval_dict['ndcg']))
5469

5570

5671
main()

example/m_caser_demo.py

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,72 @@
1-
from tensorflow.keras.optimizers import Adam
1+
"""
2+
Created on Nov 20, 2021
3+
train Caser demo
4+
@author: Ziyao Geng([email protected])
5+
"""
6+
import os
27
from time import time
8+
from tensorflow.keras.optimizers import Adam
9+
310
from reclearn.models.matching import Caser
411
from reclearn.data.datasets import movielens as ml
512
from reclearn.evaluator import eval_pos_neg
6-
import os
13+
from reclearn.data.feature_column import sparseFeature
714

815
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
916
os.environ['CUDA_VISIBLE_DEVICES'] = '6'
1017

18+
# Hyper parameters
19+
neg_num = 4
20+
embed_dim = 64
21+
seq_len = 200
22+
learning_rate = 0.001
23+
epochs = 20
24+
batch_size = 512
25+
26+
model_params = {
27+
'seq_len': seq_len,
28+
'hor_n': 8,
29+
'hor_h': 2,
30+
'ver_n': 4,
31+
'dnn_dropout': 0.2,
32+
'loss_name': 'binary_entropy_loss',
33+
'embed_reg': 0.
34+
}
35+
36+
k = 10
37+
1138

1239
def main():
13-
epochs = 30
14-
learning_rate = 0.001
15-
batch_size = 512
16-
neg_num = 1
17-
seq_len = 200
18-
k = 10
19-
test_neg_num = 100
2040
file_path = 'data/ml-1m/ratings.dat'
21-
user_num, item_num, train_path, val_path, test_path = ml.load_seq_movielens(file_path=file_path)
22-
train_data = ml.load_seq_ml(train_path, "train", neg_num, seq_len, contain_user=True)
23-
val_data = ml.load_seq_ml(val_path, "val", neg_num, seq_len, contain_user=True)
24-
test_data = ml.load_seq_ml(test_path, "test", test_neg_num, seq_len, contain_user=True)
41+
# TODO: 1. Split Data
42+
train_path, val_path, test_path, meta_path = ml.split_seq_movielens(file_path=file_path)
43+
with open(meta_path) as f:
44+
max_user_num, max_item_num = [int(x) for x in f.readline().strip('\n').split('\t')]
45+
# TODO: 2. Build Feature Columns
2546
fea_cols = {
26-
'user_num': user_num,
27-
'item_num': item_num,
28-
'seq_len': seq_len,
29-
'embed_dim': 50
30-
}
31-
params = {
32-
'fea_cols': fea_cols,
33-
'hor_n': 8,
34-
'hor_h': 2,
35-
'ver_n': 4,
36-
'dnn_dropout': 0.2,
37-
'loss_name': 'binary_entropy_loss',
38-
'gamma': 0.5,
39-
'embed_reg': 0.
47+
'user': sparseFeature('user', max_user_num + 1, embed_dim),
48+
'item': sparseFeature('item', max_item_num + 1, embed_dim)
4049
}
41-
model = Caser(**params)
42-
model.summary()
50+
# TODO: 3. Load Data
51+
train_data = ml.load_seq_ml(train_path, "train", seq_len, neg_num, max_item_num, contain_user=True)
52+
val_data = ml.load_seq_ml(val_path, "val", seq_len, neg_num, max_item_num, contain_user=True)
53+
test_data = ml.load_seq_ml(test_path, "test", seq_len, 100, max_item_num, contain_user=True)
54+
# TODO: 4. Build Model
55+
model = Caser(fea_cols, **model_params)
4356
model.compile(optimizer=Adam(learning_rate=learning_rate))
44-
for epoch in range(1, epochs+1):
57+
# TODO: 5. Fit Model
58+
for epoch in range(1, epochs + 1):
4559
t1 = time()
4660
model.fit(
4761
x=train_data,
48-
validation_data=val_data,
4962
epochs=1,
63+
validation_data=val_data,
5064
batch_size=batch_size
5165
)
52-
eval_dict = eval_pos_neg(model, test_data, batch_size, ["hr", "ndcg"], k)
66+
eval_dict = eval_pos_neg(model, test_data, ['hr', 'mrr', 'ndcg'], k, batch_size)
5367
t2 = time()
54-
print('Iteration %d Fit [%.1f s], Evaluate [%.1f s]: HR = %.4f, NDCG = %.4f, '
55-
% (epoch, t2 - t1, time() - t2, eval_dict['hr'], eval_dict['ndcg']))
68+
print('Iteration %d Fit [%.1f s], Evaluate [%.1f s]: HR = %.4f, MRR = %.4f, NDCG = %.4f'
69+
% (epoch, t2 - t1, time() - t2, eval_dict['hr'], eval_dict['mrr'], eval_dict['ndcg']))
5670

5771

5872
main()

example/m_fissa_demo.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Created on Nov 21, 2021
3+
train FISSA demo
4+
@author: Ziyao Geng([email protected])
5+
"""
6+
import os
7+
from time import time
8+
from tensorflow.keras.optimizers import Adam
9+
10+
from reclearn.models.matching import FISSA
11+
from reclearn.data.datasets import movielens as ml
12+
from reclearn.evaluator import eval_pos_neg
13+
from reclearn.data.feature_column import sparseFeature
14+
15+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
16+
os.environ['CUDA_VISIBLE_DEVICES'] = '6'
17+
18+
# Hyper parameters
19+
neg_num = 4
20+
embed_dim = 64
21+
seq_len = 200
22+
learning_rate = 0.001
23+
epochs = 20
24+
batch_size = 512
25+
26+
model_params = {
27+
'seq_len': seq_len,
28+
'blocks': 2,
29+
'num_heads': 1,
30+
'ffn_hidden_unit': 64,
31+
'dnn_dropout': 0.2,
32+
'layer_norm_eps': 1e-6,
33+
'loss_name': 'binary_entropy_loss',
34+
'embed_reg': 0.
35+
}
36+
37+
k = 10
38+
39+
40+
def main():
41+
file_path = 'data/ml-1m/ratings.dat'
42+
# TODO: 1. Split Data
43+
train_path, val_path, test_path, meta_path = ml.split_seq_movielens(file_path=file_path)
44+
with open(meta_path) as f:
45+
_, max_item_num = [int(x) for x in f.readline().strip('\n').split('\t')]
46+
# TODO: 2. Build Feature Columns
47+
fea_cols = {
48+
'item': sparseFeature('item', max_item_num + 1, embed_dim)
49+
}
50+
# TODO: 3. Load Data
51+
train_data = ml.load_seq_ml(train_path, "train", seq_len, neg_num, max_item_num, contain_user=True)
52+
val_data = ml.load_seq_ml(val_path, "val", seq_len, neg_num, max_item_num, contain_user=True)
53+
test_data = ml.load_seq_ml(test_path, "test", seq_len, 100, max_item_num, contain_user=True)
54+
# TODO: 4. Build Model
55+
model = FISSA(fea_cols, **model_params)
56+
model.compile(optimizer=Adam(learning_rate=learning_rate))
57+
# TODO: 5. Fit Model
58+
for epoch in range(1, epochs + 1):
59+
t1 = time()
60+
model.fit(
61+
x=train_data,
62+
epochs=1,
63+
validation_data=val_data,
64+
batch_size=batch_size
65+
)
66+
eval_dict = eval_pos_neg(model, test_data, ['hr', 'mrr', 'ndcg'], k, batch_size)
67+
t2 = time()
68+
print('Iteration %d Fit [%.1f s], Evaluate [%.1f s]: HR = %.4f, MRR = %.4f, NDCG = %.4f'
69+
% (epoch, t2 - t1, time() - t2, eval_dict['hr'], eval_dict['mrr'], eval_dict['ndcg']))
70+
71+
72+
main()

example/m_gru4rec_demo.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Created on Nov 20, 2021
3+
train GRU4Rec demo
4+
@author: Ziyao Geng([email protected])
5+
"""
6+
import os
7+
from time import time
8+
from tensorflow.keras.optimizers import Adam
9+
10+
from reclearn.models.matching import GRU4Rec
11+
from reclearn.data.datasets import movielens as ml
12+
from reclearn.evaluator import eval_pos_neg
13+
from reclearn.data.feature_column import sparseFeature
14+
15+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
16+
os.environ['CUDA_VISIBLE_DEVICES'] = '6'
17+
18+
# Hyper parameters
19+
neg_num = 4
20+
embed_dim = 64
21+
seq_len = 200
22+
learning_rate = 0.001
23+
epochs = 20
24+
batch_size = 512
25+
26+
model_params = {
27+
'seq_len': seq_len,
28+
'gru_layers': 2,
29+
'gru_unit': 128,
30+
'gru_activation': 'tanh',
31+
'dnn_dropout': 0.5,
32+
'loss_name': 'bpr_loss',
33+
'embed_reg': 0.
34+
}
35+
36+
k = 10
37+
38+
39+
def main():
40+
file_path = 'data/ml-1m/ratings.dat'
41+
# TODO: 1. Split Data
42+
train_path, val_path, test_path, meta_path = ml.split_seq_movielens(file_path=file_path)
43+
with open(meta_path) as f:
44+
_, max_item_num = [int(x) for x in f.readline().strip('\n').split('\t')]
45+
# TODO: 2. Build Feature Columns
46+
fea_cols = {
47+
'item': sparseFeature('item', max_item_num + 1, embed_dim)
48+
}
49+
# TODO: 3. Load Data
50+
train_data = ml.load_seq_ml(train_path, "train", seq_len, neg_num, max_item_num)
51+
val_data = ml.load_seq_ml(val_path, "val", seq_len, neg_num, max_item_num)
52+
test_data = ml.load_seq_ml(test_path, "test", seq_len, 100, max_item_num)
53+
# TODO: 4. Build Model
54+
model = GRU4Rec(fea_cols, **model_params)
55+
model.compile(optimizer=Adam(learning_rate=learning_rate))
56+
# TODO: 5. Fit Model
57+
for epoch in range(1, epochs + 1):
58+
t1 = time()
59+
model.fit(
60+
x=train_data,
61+
epochs=1,
62+
validation_data=val_data,
63+
batch_size=batch_size
64+
)
65+
eval_dict = eval_pos_neg(model, test_data, ['hr', 'mrr', 'ndcg'], k, batch_size)
66+
t2 = time()
67+
print('Iteration %d Fit [%.1f s], Evaluate [%.1f s]: HR = %.4f, MRR = %.4f, NDCG = %.4f'
68+
% (epoch, t2 - t1, time() - t2, eval_dict['hr'], eval_dict['mrr'], eval_dict['ndcg']))
69+
70+
71+
main()

example/m_ncf_demo.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Created on Nov 19, 2021
3-
train BPR demo
3+
train NCF demo
44
@author: Ziyao Geng([email protected])
55
"""
66
import os
@@ -23,12 +23,12 @@
2323
batch_size = 512
2424

2525
model_params = {
26-
'hidden_units': [256, 128, 64],
27-
'activation': 'relu',
28-
'dnn_dropout': 0.5,
29-
'is_batch_norm': False,
30-
'loss_name': 'binary_entropy_loss',
31-
'gamma': 0.3
26+
'hidden_units': [256, 128, 64],
27+
'activation': 'relu',
28+
'dnn_dropout': 0.5,
29+
'is_batch_norm': False,
30+
'loss_name': 'binary_entropy_loss',
31+
'gamma': 0.3
3232
}
3333

3434
k = 10

0 commit comments

Comments
 (0)