-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEASE.py
167 lines (117 loc) ยท 5.74 KB
/
EASE.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
import time
import os
from tqdm import tqdm
import torch
from scipy import sparse
import numpy as np
import pandas as pd
import bottleneck as bn
from parse_config import ConfigParser
from utils import prepare_device
from utils.ae_util import make_prediction_file, make_inference_data_and_mark, write_submission_file, get_loaders
from model.model import EASE
from data_loader.ae_dataloader import AETrainDataSet, AETestDataSet, ae_data_load, get_labels
# fix random seeds for reproducibility
SEED = 123
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)
def EASE_train_inference(config):
print('========================Data Loading========================')
root_data = config['root_data']
data_dir = config['data_dir']
model_name = config['model_name']
output_path = config['output_path']
n_users = config['n_users']
n_items = config['n_items']
EASE_lambda = config['EASE_lambda']
n_gpu_use = torch.cuda.device_count()
device = torch.device('cuda:0' if n_gpu_use > 0 else 'cpu')
# ํ์ผ์ ์ ์ฅํ ๋๋ ํ ๋ฆฌ ์ค์
if not os.path.exists(output_path):
os.mkdir(output_path)
user_label, item_label = get_labels(data_dir)
raw_data, train_mark = make_inference_data_and_mark(config, root_data, user_label, item_label)
rating_data = pd.read_csv('./data/train/train_ratings.csv').drop(columns=['time'])
rating_data['user'] = rating_data['user'].apply(lambda x: user_label[x])
rating_data['item'] = rating_data['item'].apply(lambda x: item_label[x])
inference_results = np.zeros((31360, 6807))
print('========================Training Start========================')
for i in range(1, 1001):
print(f'================{i}๋ฒ์จฐ ๋ชจ๋ธ ํ์ต ๋ฐ ์ธํผ๋ฐ์ค================')
start_time = time.time()
data_tr, data_te = split_train_test_proportion(rating_data)
data_tr, data_te = ae_data_load(data_tr, data_te)
data_tr, data_te = data_tr.toarray(), data_te.toarray()
model = EASE(EASE_lambda)
model.train(data_tr)
inference_result = model.forward(data_tr)
temp_nonzero = data_tr.nonzero()
inference_result[temp_nonzero]=-np.inf
recall = recall_at_k_batch(inference_result, data_te, 10)
print(f'[================{i}๋ฒ์งธ ๋ชจ๋ธ recall] {recall}================')
print(f'์ฝ {round((time.time() - start_time)/60, 1)}๋ถ ๊ฑธ๋ ธ์ต๋๋ค')
inference_results += inference_result
inference_results[train_mark]=-np.inf
final_10 = bn.argpartition(-inference_results, 10, axis=1)[:, :10] # 10๊ฐ๋ง ๋จ๊ฒจ๋
total_recall_at_k = 'bagging1000_0_8'
# ์์ธก ํ์ผ์ ์ ์ฅํจ
make_prediction_file(output_path, inference_results, config, total_recall_at_k, user_label, item_label)
#์ ์ถ ํ์ผ์ ์ ์ฅํจ
write_submission_file(output_path, final_10, config, total_recall_at_k, user_label, item_label)
def recall_at_k_batch(X_pred, heldout_batch, k=10):
batch_users = X_pred.shape[0]
idx = bn.argpartition(-X_pred, k, axis=1) # ํฐ ์์ k๊ฐ์ ์ธ๋ฑ์ค๊ฐ ์์ ์ค๋๋ก ํด์ ์ธ๋ฑ์ค๋ฅผ ๋ฝ์๋
X_pred_binary = np.zeros_like(X_pred, dtype=bool) # (500, 8607) ๋ชจ์์ 0์ผ๋ก ์ฑ์์ง ๋ฐฐ์ด ๋ง๋ฆ
X_pred_binary[np.arange(batch_users)[:, np.newaxis], idx[:, :k]] = True # k๊ฐ๋ฅผ true๋ก ๋ฐ๊ฟ์ค
X_true_binary = (heldout_batch > 0) # ๋ฐ๊นฅ์ ๋บด๋์ ๋ฐ์ดํฐ์ ๋ํด์๋ง True๊ฐ ๋งค๊ฒจ์ง
tmp = (np.logical_and(X_true_binary, X_pred_binary).sum(axis=1)).astype(np.float32) # ๋ ๋ค and์ธ ๊ฒ์ ๊ณจ๋ผ๋ด์ ๊ฐฏ์๋ฅผ ์ธ์ด์ค
n_hidden = X_true_binary.sum(axis=1)
n_hidden[n_hidden==0] = 1 # 0์ธ ๊ฐ์ ์ถฉ๋ถํ ์์ ์ซ์๋ก ๋ฐ๊ฟ์ค (n_hidden์ด 0์ด๋ฉด ์ด์ฐจํผ ๋ถ์๋ 0)
recall = tmp / np.minimum(k, n_hidden)
recall = recall.mean(axis=0).item()
print(f'recall: {recall}')
return recall
def get_count(tp, id):
playcount_groupbyid = tp[[id]].groupby(id, as_index=False)
count = playcount_groupbyid.size()
return count
def filter_triplets(tp, min_uc=5, min_sc=0):
usercount, itemcount = get_count(tp, 'user'), get_count(tp, 'item')
return tp, usercount, itemcount
def split_train_test_proportion(data, test_prop=0.2):
data_grouped_by_user = data.groupby('user')
tr_list, te_list = list(), list()
print('==================์ ์ ๋ฐ์ดํฐ ์ค๋น==================')
for _, group in tqdm(data_grouped_by_user):
n_items_u = len(group)
idx = np.zeros(n_items_u, dtype='bool')
idx[np.random.choice(n_items_u, size=int(test_prop * n_items_u), replace=False).astype('int64')] = True
tr_list.append(group[np.logical_not(idx)])
te_list.append(group[idx])
data_tr = pd.concat(tr_list)
data_te = pd.concat(te_list)
return data_tr, data_te
def ae_data_load(data_tr, data_te):
n_users = 31360
n_items = 6807
rows_tr, cols_tr = data_tr['user'], data_tr['item']
rows_te, cols_te = data_te['user'], data_te['item']
train_data = sparse.csr_matrix((np.ones_like(rows_tr),
(rows_tr, cols_tr)), dtype='float64', shape=(n_users, n_items))
test_data = sparse.csr_matrix((np.ones_like(rows_te),
(rows_te, cols_te)), dtype='float64', shape=(n_users, n_items))
return train_data, test_data
if __name__ == "__main__":
config = {
"root_data": './data/train/' ,
"data_dir": './data/train/ae_data',
"num_workers": 1,
"model_name": 'EASE',
"output_path": './output/auto_encoder',
"n_users": 31360,
"n_items": 6807,
'EASE_lambda': 400
}
EASE_train_inference(config)