-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_recommender.py
312 lines (302 loc) · 10.6 KB
/
train_recommender.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
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
########################################
# Train deep learning recommender #
########################################
import argparse
import os
import string
from datetime import datetime
import numpy as np
import pandas as pd
import torch
torch.autograd.set_detect_anomaly(True)
import random
import wandb
from data_processors.Dataset import (
CollaborativeFilteringDataWorker,
HitRatioDataset,
UserGameDataseEfficient,
)
from models.collaborative_filtering_recommender import CollabNN, DotProductBias
from tools.train import get_train_test_val_of_dataframe, train_test_validate
from tools.useful_functions import subset_users_having_at_least_n_games
from torch.utils.data import DataLoader
parser = argparse.ArgumentParser(description="Evaluate and train algorithms")
parser.add_argument(
"-m",
"--model",
type=str,
default="CollabNN",
help="Which model architecture to train. You can set CollabNN (Neural Collaborative Filtering) or MF (Matrix Factorization).",
)
parser.add_argument(
"-nw",
"--nworkers",
type=int,
default=1,
help="Number of workers in the Data Loader.",
)
parser.add_argument("-bs", "--batch_size", type=int, default=128)
parser.add_argument(
"-ed",
"--embedding_dim",
type=int,
default=50,
help="Embedding dimension of the latent user and game factors to be learnt.",
)
parser.add_argument("-d", "--device", type=str, default="cpu")
parser.add_argument(
"-ntestu",
"--n_test_users",
type=int,
default=50,
help="How many test users to use during training for the cold start benchmark.",
)
parser.add_argument(
"-negs",
"--n_negative_samples",
type=int,
default=4,
help="Number of negative samples per positive sample.",
)
parser.add_argument("-e", "--epochs", type=int, default=20)
parser.add_argument("-lr", "--learning_rate", type=float, default=0.01)
parser.add_argument("-mom", "--momentum", type=float, default=0.00)
parser.add_argument("-wd", "--weight_decay", type=float, default=0.00)
parser.add_argument(
"-mg",
"--min_games",
type=int,
default=20,
help="Minimum number of games a user need to have to be considered.",
)
parser.add_argument(
"-mp",
"--min_playtime",
type=float,
default=2.0,
help="All games with a playtime smaller than that are set to zero.",
)
parser.add_argument(
"-ncu",
"--num_closest_users",
type=int,
default=13,
help="Number of closest users that will be used to determine the mean user in the cold start setting.",
)
parser.add_argument("-s", "--seed", type=int, default=41)
parser.add_argument(
"-subs",
"--subsample_n_users",
type=int,
default=None,
help="Build whole pipeline on only a subset of users",
)
parser.add_argument("--embedding_path", type=str, default=None)
parser.add_argument(
"--checkpoint", action="store_true", help="Whether to save the best model."
)
parser.add_argument("--no-checkpoint", dest="checkpoint", action="store_false")
parser.add_argument(
"--scheduling", action="store_true", help="Whether to do learning rate scheduling."
)
parser.add_argument("--no-scheduling", dest="scheduling", action="store_false")
parser.add_argument("--wandb", action="store_true")
parser.add_argument(
"--no-wandb",
dest="wandb",
action="store_false",
help="Whether to use weights&biases for experiment tracking",
)
parser.add_argument(
"--logarithm",
action="store_true",
help="Whether to take the logarithm of the playtime data.",
)
parser.add_argument("--no-logarithm", dest="logarithm", action="store_false")
parser.add_argument(
"--binarize", action="store_true", help="Whether to make playtimes binary."
)
parser.add_argument("--no-binarize", dest="binarize", action="store_false")
parser.add_argument("--feed_content_embeddings", action="store_true")
parser.add_argument(
"--no-feed_content_embeddings", dest="feed_content_embeddings", action="store_false"
)
args = parser.parse_args()
base_path = os.path.join(os.getcwd(), "data/training_dataset")
# Load files
game_information = pd.read_parquet(
os.path.join(
base_path,
"subset_game_information_5000_most_played_games_prompts=False.parq",
)
)
user_game_matrix = pd.read_parquet(
os.path.join(
base_path,
"subset_user_game_matrix_5000_most_played_games.parq",
)
)
# Filter out all zero users
mask = (user_game_matrix == 0.0).all(axis=1)
subset_user_game_matrix = user_game_matrix[~mask]
# Take only subset of users into consideration
if args.subsample_n_users is not None:
user_game_matrix = user_game_matrix.sample(
args.subsample_n_users, replace=False
).reset_index(drop=True)
def main(
user_game_matrix=user_game_matrix,
game_information=game_information,
):
# Get datetime and some randomized string for run name
now = datetime.now()
dt_string = now.strftime("%d_%m_%Y_%H_%M")
identifier = "".join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(5)
)
# Subset into users fullfilling criteria
active_users = subset_users_having_at_least_n_games(
args.min_games, args.min_playtime, user_game_matrix
)
# Sample test users
test_users = active_users.sample(
args.n_test_users, replace=False, random_state=args.seed
)
# Sample non test users
non_test_users = active_users.drop(test_users.index.tolist())
test_users = test_users.reset_index(drop=True)
non_test_users = non_test_users.reset_index(drop=True)
# Instantiate wandb if it is used
if args.wandb == True:
wandb.init(
name=f"{dt_string}-{args.model}-{identifier}-binary={args.binarize}-embed={args.feed_content_embeddings}",
project="Gaming",
entity="thesis-groeger",
)
wandb.config.update(vars(args))
# Turn wide user-game matrix into short but tall dataset such that we can process it in batches
cfd = CollaborativeFilteringDataWorker(
non_test_users,
game_information,
save_all=True,
file_storage_path=base_path,
user_game_dataset_name=f"collaborative_filtering_dataset_subsample={args.subsample_n_users}_5000_most_played_games_min_games={args.min_games}-min_playtime{args.min_playtime}-negative_samples={args.n_negative_samples}-n_closest_users={args.num_closest_users}-seed={args.seed}-binarize={args.binarize}-logarithm={args.logarithm}.parq",
binarize=args.binarize,
logarithm=args.logarithm,
)
# Check whether this dataset already exists, if not create one
if cfd.check_load() == False:
# from wide to tall
cfd.transform_dataset()
# from app id to embedding id
cfd.get_mappings()
# add to dataset a embedding id based app id column
cfd.add_mapped_app_id_columns()
else:
cfd.load_datasets()
# Make train, test, val split accoring to 60-20-20 Split
train, validate, test = get_train_test_val_of_dataframe(cfd.user_game_dataset)
(
train_negatives,
validate_negatives,
test_negatives,
) = get_train_test_val_of_dataframe(cfd.user_game_dataset_all_zeros)
print(f"Excerpt of positive training data : \n{train.head()}")
# Instantiate pytorch datasets such that they can be fed into dataloader
train_dset = UserGameDataseEfficient(
train, train_negatives, dataset_name="Training"
)
val_dset = UserGameDataseEfficient(
validate, validate_negatives, dataset_name="Validation"
)
test_dset = UserGameDataseEfficient(test, test_negatives, dataset_name="Test")
# build hit rate dataset by asking the model to rank the top game among 1000 others and
# measure if it is under the top 10
hit_rate_dataset = HitRatioDataset(
test,
cfd.user_game_dataset_all_zeros,
dataset_name="HitRatio",
zero_one_ratio=1000,
)
# instantiate data loaders
train_loader = DataLoader(
train_dset,
batch_size=args.batch_size,
num_workers=args.nworkers,
shuffle=True,
)
val_loader = DataLoader(
val_dset,
batch_size=args.batch_size,
num_workers=args.nworkers,
shuffle=True,
)
test_loader = DataLoader(
test_dset,
batch_size=args.batch_size,
num_workers=args.nworkers,
shuffle=True,
)
# Get total number of users and games to determine the embedding length
n_users = len(user_game_matrix.index.tolist())
n_games = len(user_game_matrix.columns)
print(f"Got {n_users} n_users")
print(f"Got {n_games} n_games")
# Assign content embeddings if selected
if args.feed_content_embeddings == True:
if ".npy" in args.embedding_path:
game_content_embeddings = torch.from_numpy(np.load(args.embedding_path))
elif ".parq" in args.embedding_path:
temp_df = pd.read_parquet(args.embedding_path)
game_content_embeddings = torch.from_numpy(
temp_df[["first_axis", "second_axis", "third_axis"]].to_numpy()
)
elif args.feed_content_embeddings == False:
game_content_embeddings = None
# build models
if args.model == "MF":
print(f"Train Matrix Factorization with {args}")
model = DotProductBias(
n_users=n_users,
n_games=n_games,
embedding_dim=args.embedding_dim,
idx_to_app_id=cfd.idx_to_app_id,
app_id_to_idx=cfd.app_id_to_idx,
reference_dataset=non_test_users,
)
elif args.model == "CollabNN":
print(f"Train ColabNN with {args}")
model = CollabNN(
n_users=n_users,
n_games=n_games,
binary_classification=args.binarize,
embedding_dim=args.embedding_dim,
game_content_embeddings=game_content_embeddings,
idx_to_app_id=cfd.idx_to_app_id,
app_id_to_idx=cfd.app_id_to_idx,
reference_dataset=non_test_users,
)
print(f"Model Architecture: \n{model}")
train_test_validate(
model=model,
train_loader=train_loader,
val_loader=val_loader,
test_loader=test_loader,
hit_rate_dataset=hit_rate_dataset,
n_epochs=args.epochs,
lr=args.learning_rate,
test_users=test_users,
game_information=game_information,
weight_decay=args.weight_decay,
device=args.device,
model_path=f"scripts/app/files/models",
checkpointing=args.checkpoint,
lr_scheduling=args.scheduling,
binary_classification=args.binarize,
use_wandb=args.wandb,
identifier=identifier,
)
if args.wandb == True:
wandb.finish()
if __name__ == "__main__":
main()