-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_caser.py
366 lines (293 loc) · 13.2 KB
/
train_caser.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import argparse
from time import time
import torch.optim as optim
from torch.autograd import Variable
from algorithms.caser import Caser
from evaluation.evaluation import evaluate_ranking
from data.preprocess import Interactions
from utils.utils import *
def load_data(file_path, delimiter=' '):
'''
Loads the data from a text file and returns a list of (user, item) pairs.
Parameters:
file_path: str, the path to the dataset file.
delimiter: str, the delimiter used in the dataset file.
Returns:
data: list of tuples, the loaded data as (user, item) pairs.
'''
data = []
with open(file_path, 'r') as file:
for line in file:
try:
user, item = map(int, line.strip().split(delimiter)[:2])
data.append((user, item))
except ValueError:
continue # Skip lines that cannot be converted to integer pairs
return data
'''
Loads the data from a text file and returns a list of (user, item) pairs.
Parameters:
file_path: str, the path to the dataset file.
delimiter: str, the delimiter used in the dataset file.
Returns:
data: list of tuples, the loaded data as (user, item) pairs.
'''
with open(file_path, 'r') as file:
data = [tuple(map(int, line.strip().split(delimiter)[:2])) for line in file]
return data
def split_dataset(data, test_ratio=0.2):
'''
Splits the dataset into train and test sets.
'''
shuffle_indices = np.random.permutation(np.arange(len(data)))
test_set_size = int(len(data) * test_ratio)
test_indices = shuffle_indices[:test_set_size]
train_indices = shuffle_indices[test_set_size:]
train_data = [data[i] for i in train_indices]
test_data = [data[i] for i in test_indices]
return train_data, test_data
class Recommender(object):
"""
Contains attributes and methods that needed to train a sequential
recommendation model. Models are trained by many tuples of
(users, sequences, targets, negatives) and negatives are from negative
sampling: for any known tuple of (user, sequence, targets), one or more
items are randomly sampled to act as negatives.
Parameters
----------
n_iter: int,
Number of iterations to run.
batch_size: int,
Minibatch size.
l2: float,
L2 loss penalty, also known as the 'lambda' of l2 regularization.
neg_samples: int,
Number of negative samples to generate for each targets.
If targets=3 and neg_samples=3, then it will sample 9 negatives.
learning_rate: float,
Initial learning rate.
use_cuda: boolean,
Run the model on a GPU or CPU.
model_args: args,
Model-related arguments, like latent dimensions.
"""
def __init__(self,
n_iter=None,
batch_size=None,
l2=None,
neg_samples=None,
learning_rate=None,
use_cuda=False,
model_args=None):
# model related
self._num_items = None
self._num_users = None
self._net = None
self.model_args = model_args
# learning related
self._batch_size = batch_size
self._n_iter = n_iter
self._learning_rate = learning_rate
self._l2 = l2
self._neg_samples = neg_samples
self._device = torch.device("cuda" if use_cuda else "cpu")
# rank evaluation related
self.test_sequence = None
self._candidate = dict()
@property
def _initialized(self):
return self._net is not None
def _initialize(self, interactions):
self._num_items = interactions.num_items
self._num_users = interactions.num_users
self.test_sequence = interactions.test_sequences
self._net = Caser(self._num_users,
self._num_items,
self.model_args).to(self._device)
self._optimizer = optim.Adam(self._net.parameters(),
weight_decay=self._l2,
lr=self._learning_rate)
def fit(self, train, test, verbose=False):
"""
The general training loop to fit the model
Parameters
----------
train: :class:`spotlight.interactions.Interactions`
training instances, also contains test sequences
test: :class:`spotlight.interactions.Interactions`
only contains targets for test sequences
verbose: bool, optional
print the logs
"""
# convert to sequences, targets and users
sequences_np = train.sequences.sequences
targets_np = train.sequences.targets
users_np = train.sequences.user_ids.reshape(-1, 1)
L, T = train.sequences.L, train.sequences.T
n_train = sequences_np.shape[0]
output_str = 'total training instances: %d' % n_train
print(output_str)
if not self._initialized:
self._initialize(train)
start_epoch = 0
for epoch_num in range(start_epoch, self._n_iter):
t1 = time()
# set model to training mode
self._net.train()
users_np, sequences_np, targets_np = shuffle(users_np,
sequences_np,
targets_np)
negatives_np = self._generate_negative_samples(users_np, train, n=self._neg_samples)
# convert numpy arrays to PyTorch tensors and move it to the corresponding devices
users, sequences, targets, negatives = (torch.from_numpy(users_np).long(),
torch.from_numpy(sequences_np).long(),
torch.from_numpy(targets_np).long(),
torch.from_numpy(negatives_np).long())
users, sequences, targets, negatives = (users.to(self._device),
sequences.to(self._device),
targets.to(self._device),
negatives.to(self._device))
epoch_loss = 0.0
for (minibatch_num,
(batch_users,
batch_sequences,
batch_targets,
batch_negatives)) in enumerate(minibatch(users,
sequences,
targets,
negatives,
batch_size=self._batch_size)):
items_to_predict = torch.cat((batch_targets, batch_negatives), 1)
items_prediction = self._net(batch_sequences,
batch_users,
items_to_predict)
(targets_prediction,
negatives_prediction) = torch.split(items_prediction,
[batch_targets.size(1),
batch_negatives.size(1)], dim=1)
self._optimizer.zero_grad()
# compute the binary cross-entropy loss
positive_loss = -torch.mean(
torch.log(torch.sigmoid(targets_prediction)))
negative_loss = -torch.mean(
torch.log(1 - torch.sigmoid(negatives_prediction)))
loss = positive_loss + negative_loss
epoch_loss += loss.item()
loss.backward()
self._optimizer.step()
epoch_loss /= minibatch_num + 1
t2 = time()
if verbose and (epoch_num + 1) % 10 == 0:
output_str = "Epoch %d [%.1f s]\tloss=%.4f [%.1f s]" % (epoch_num + 1,
t2 - t1,
epoch_loss,
time() - t2)
print(output_str)
print("NDGC Accuracy is ",evaluate_ranking(self,test,train,k=10))
def _generate_negative_samples(self, users, interactions, n):
"""
Sample negative from a candidate set of each user. The
candidate set of each user is defined by:
{All Items} \ {Items Rated by User}
Parameters
----------
users: array of np.int64
sequence users
interactions: :class:`spotlight.interactions.Interactions`
training instances, used for generate candidates
n: int
total number of negatives to sample for each sequence
"""
users_ = users.squeeze()
negative_samples = np.zeros((users_.shape[0], n), np.int64)
if not self._candidate:
all_items = np.arange(interactions.num_items - 1) + 1 # 0 for padding
train = interactions.tocsr()
for user, row in enumerate(train):
self._candidate[user] = list(set(all_items) - set(row.indices))
for i, u in enumerate(users_):
for j in range(n):
x = self._candidate[u]
negative_samples[i, j] = x[
np.random.randint(len(x))]
return negative_samples
def predict(self, user_id, item_ids=None):
"""
Make predictions for evaluation: given a user id, it will
first retrieve the test sequence associated with that user
and compute the recommendation scores for items.
Parameters
----------
user_id: int
users id for which prediction scores needed.
item_ids: array, optional
Array containing the item ids for which prediction scores
are desired. If not supplied, predictions for all items
will be computed.
"""
if self.test_sequence is None:
raise ValueError('Missing test sequences, cannot make predictions')
# set model to evaluation model
self._net.eval()
with torch.no_grad():
sequences_np = self.test_sequence.sequences[user_id, :]
sequences_np = np.atleast_2d(sequences_np)
if item_ids is None:
item_ids = np.arange(self._num_items).reshape(-1, 1)
sequences = torch.from_numpy(sequences_np).long()
item_ids = torch.from_numpy(item_ids).long()
user_id = torch.from_numpy(np.array([[user_id]])).long()
user, sequences, items = (user_id.to(self._device),
sequences.to(self._device),
item_ids.to(self._device))
out = self._net(sequences,
user,
items,
for_pred=True)
return out.cpu().numpy().flatten()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# data arguments
parser.add_argument('--train_root', type=str, default='/content/drive/MyDrive/SSC/data/movie.txt')
parser.add_argument('--L', type=int, default=5)
parser.add_argument('--T', type=int, default=2)
# train arguments
parser.add_argument('--n_iter', type=int, default=201)
parser.add_argument('--seed', type=int, default=1234)
parser.add_argument('--batch_size', type=int, default=512)
parser.add_argument('--learning_rate', type=float, default=1e-3)
parser.add_argument('--l2', type=float, default=1e-6)
parser.add_argument('--neg_samples', type=int, default=3)
parser.add_argument('--use_cuda', type=str2bool, default=True)
config = parser.parse_args()
# model dependent arguments
model_parser = argparse.ArgumentParser()
model_parser.add_argument('--d', type=int, default=50)
model_parser.add_argument('--nv', type=int, default=4)
model_parser.add_argument('--nh', type=int, default=16)
model_parser.add_argument('--drop', type=float, default=0.5)
model_parser.add_argument('--ac_conv', type=str, default='relu')
model_parser.add_argument('--ac_fc', type=str, default='relu')
model_config = model_parser.parse_args()
model_config.L = config.L
# set seed
set_seed(config.seed,
cuda=config.use_cuda)
# load dataset
# Assuming 'data' is loaded from a file and is a list of (user, item) pairs
full_data = load_data(config.train_root) # This should be replaced with the actual data loading logic
train_data, test_data = split_dataset(full_data, test_ratio=0.2)
train = Interactions(data=train_data)
train.to_sequence(config.L, config.T)
test = Interactions(data=test_data)
print(config)
print(model_config)
# fit model
model = Recommender(n_iter=config.n_iter,
batch_size=config.batch_size,
learning_rate=config.learning_rate,
l2=config.l2,
neg_samples=config.neg_samples,
model_args=model_config,
use_cuda=config.use_cuda)
model.fit(train, test, verbose=True)