-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuresvd.py
169 lines (131 loc) · 5.6 KB
/
puresvd.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
from sparsesvd import sparsesvd
from scipy.sparse import load_npz, vstack, csr_matrix
import numpy as np
from typing import Tuple, List, Dict
from concurrent.futures import ProcessPoolExecutor
from utils.constants import N_RECS, INPUT_FILE, INFO_ROW
from utils.tools import coalesce, read_json, load_pickle, pop_empty
import time
class PureSVDModel:
NAME = 'PureSVD'
def __init__(
self,
h: int,
use_test: bool,
train_path: str,
test_path: str,
trackmap_path: str
):
"""
Initialization of the PureSVD model.
:param h: Dimension of the latent space.
:param use_test: Boolean parameter to use the test matrix or not.
:param train_path: Path where sparse train matrix is stored.
:param test_path: Path where sparse test matrix is stored.
:param trackmap_path: Path where the map from track URIs to columns in the matrix is stored in.
"""
self.h = h
self.use_test = use_test
self.train_path = train_path
self.test_path = test_path
self.trackmap_path = trackmap_path
self.Utest, self.S, self.V = None, None, None
def factorize(self, U_path: str, S_path: str, V_path: str, verbose: bool):
"""
Applies matrix factorization to obtain U, S and V matrices.
:param U_path: Path to store U matrix (for test playlists).
:param S_path: Path to store S matrix.
:param V_path: Path to store V matrix.
"""
if verbose:
print('Factorizing sparse matrices...')
# load Rtrain and Rtest
Rtrain = load_npz(self.train_path)
Rtest = load_npz(self.test_path)
# create R matrix using Rtest or not
m_test = Rtest.shape[0]
R = vstack([Rtrain, Rtest]).tocsc() if self.use_test else Rtrain.tocsc()
del Rtrain, Rtest
# R ~ [m, n]
# U ~ [m, h]
# S ~ [h, h]
# V ~ [n, h]
Ut, S, Vt = sparsesvd(R, self.h)
# now obtain Utest ~ [m_test, h]
if self.use_test:
Utest = Ut.T[-m_test:]
del Ut
else:
# project vectors
Rtest = load_npz(self.test_path)
Utest = Rtest @ Vt.T
del Rtest
self.Utest = Utest
self.S = S
self.V = Vt.T
if U_path:
np.save(U_path, Utest)
if S_path:
np.save(S_path, S)
if V_path:
np.save(V_path, self.V)
def recommend(self, submit_path: str, batch_size: int, num_threads: int, verbose: bool):
"""
Compute recommendations.
:param submit_path: Path to store the submissions.
:param batch_size: Batch size to distribute test matrix rows.
:param num_threads: Number of threads to parallelize the dot product.
:param verbose: Boolean parameter to display the trace.
"""
# compute most popular tracks and remove Rtrain and Rtest matrices
Rtrain = load_npz(self.train_path)
popular = np.copy(np.asarray(-(Rtrain.sum(axis=0))).argsort().ravel()).tolist()[:N_RECS]
del Rtrain
# read test file, trackmap (track_uri -> col) and pidmap (pid -> row)
test = read_json(INPUT_FILE)
trackmap = load_pickle(self.trackmap_path)
pidmap = load_pickle(self.test_path.replace('.npz', '.pickle'))
pidmap = {row: pid for pid, row in pidmap.items()} # invert pidmap
# remove empty playlists from the test set
test_empty = pop_empty(test)
test = {pid: list(map(trackmap.get, tracks)) for pid, tracks in test.items()}
with ProcessPoolExecutor(max_workers=num_threads) as pool:
futures = list()
for i in range(0, len(test), batch_size):
futures.append(
pool.submit(recommend, i, batch_size, self.Utest, self.S, self.V, test, pidmap, popular, verbose)
)
playlists = futures.pop(0).result()
for _ in range(len(futures)):
playlists |= futures.pop(0).result()
# convert trackmap from id -> track_uri
trackmap = {value: key for key, value in trackmap.items()}
# write results in the submission
with open(submit_path, 'w', encoding='utf8') as file:
file.write(INFO_ROW + '\n')
for pid, tracks in playlists.items():
file.write(f'{pid},' + ','.join(list(map(trackmap.get, tracks))) + '\n')
for pid in test_empty:
file.write(f'{pid},' + ','.join(list(map(trackmap.get, popular))) + '\n')
def recommend(i: int, batch_size: int,
Utest: np.ndarray, S: np.ndarray, V: np.ndarray, test: Dict[int, List[int]],
pidmap: Dict[int, int], popular: np.ndarray, verbose: bool):
playlists = dict()
if verbose:
print(f'Computing recommendation for playlist {i}/{Utest.shape[0]}')
u = Utest[i:(i + batch_size)]
slice = u @ (np.diag(S) @ V.T)
slice[np.isclose(slice, 0, atol=1e-8)] = 0
slice = csr_matrix(slice, dtype=np.float32)
for j in range(i, i + u.shape[0]):
pid = pidmap.pop(j)
included = test.pop(pid)
ratings = slice.getrow(j - i)
ratings[:, included] = 0
ratings.eliminate_zeros()
cols, values = ratings.indices, ratings.data
playlists[pid] = cols[(-values).argsort().tolist()[:N_RECS]].tolist()
if len(playlists[pid]) < N_RECS:
news = (np.setdiff1d(popular, np.array(playlists[pid]+included)))[:(N_RECS-len(playlists[pid]))]
playlists[pid] += news.tolist()
return playlists