-
Notifications
You must be signed in to change notification settings - Fork 616
/
Copy pathlastfm.py
201 lines (169 loc) · 6.9 KB
/
lastfm.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
""" An example of using this library to calculate related artists
from the last.fm dataset. More details can be found
at http://www.benfrederickson.com/matrix-factorization/
This code will automatically download a HDF5 version of the dataset from
GitHub when it is first run. The original dataset can also be found at
http://ocelma.net/MusicRecommendationDataset/lastfm-360K.html
"""
import argparse
import codecs
import logging
import time
import numpy as np
import tqdm
from implicit.als import AlternatingLeastSquares
from implicit.approximate_als import (
AnnoyAlternatingLeastSquares,
FaissAlternatingLeastSquares,
NMSLibAlternatingLeastSquares,
)
from implicit.bpr import BayesianPersonalizedRanking
from implicit.datasets.lastfm import get_lastfm
from implicit.lmf import LogisticMatrixFactorization
from implicit.nearest_neighbours import (
BM25Recommender,
CosineRecommender,
TFIDFRecommender,
bm25_weight,
)
# maps command line model argument to class name
MODELS = {
"als": AlternatingLeastSquares,
"nmslib_als": NMSLibAlternatingLeastSquares,
"annoy_als": AnnoyAlternatingLeastSquares,
"faiss_als": FaissAlternatingLeastSquares,
"tfidf": TFIDFRecommender,
"cosine": CosineRecommender,
"bpr": BayesianPersonalizedRanking,
"lmf": LogisticMatrixFactorization,
"bm25": BM25Recommender,
}
def get_model(model_name):
print(f"getting model {model_name}")
model_class = MODELS.get(model_name)
if not model_class:
raise ValueError(f"Unknown Model '{model_name}'")
# some default params
if model_name.endswith("als"):
params = {"factors": 128, "dtype": np.float32}
elif model_name == "bm25":
params = {"K1": 100, "B": 0.5}
elif model_name == "bpr":
params = {"factors": 63}
elif model_name == "lmf":
params = {"factors": 30, "iterations": 40, "regularization": 1.5}
else:
params = {}
return model_class(**params)
def calculate_similar_artists(output_filename, model_name="als"):
"""generates a list of similar artists in lastfm by utilizing the 'similar_items'
api of the models"""
artists, _, plays = get_lastfm()
# create a model from the input data
model = get_model(model_name)
# if we're training an ALS based model, weight input for last.fm
# by bm25
if model_name.endswith("als"):
# lets weight these models by bm25weight.
logging.debug("weighting matrix by bm25_weight")
plays = bm25_weight(plays, K1=100, B=0.8)
# also disable building approximate recommend index
model.approximate_recommend = False
# this is actually disturbingly expensive:
plays = plays.tocsr()
user_plays = plays.T.tocsr()
logging.debug("training model %s", model_name)
start = time.time()
model.fit(user_plays)
logging.debug("trained model '%s' in %0.2fs", model_name, time.time() - start)
# write out similar artists by popularity
start = time.time()
logging.debug("calculating top artists")
user_count = np.ediff1d(plays.indptr)
to_generate = sorted(np.arange(len(artists)), key=lambda x: -user_count[x])
# write out as a TSV of artistid, otherartistid, score
logging.debug("writing similar items")
with tqdm.tqdm(total=len(to_generate)) as progress:
with codecs.open(output_filename, "w", "utf8") as o:
batch_size = 1000
for startidx in range(0, len(to_generate), batch_size):
batch = to_generate[startidx : startidx + batch_size]
ids, scores = model.similar_items(batch, 11)
for i, artistid in enumerate(batch):
artist = artists[artistid]
for other, score in zip(ids[i], scores[i]):
o.write(f"{artist}\t{artists[other]}\t{score}\n")
progress.update(len(batch))
logging.debug("generated similar artists in %0.2fs", time.time() - start)
def calculate_recommendations(output_filename, model_name="als"):
"""Generates artist recommendations for each user in the dataset"""
# train the model based off input params
artists, users, plays = get_lastfm()
# create a model from the input data
model = get_model(model_name)
# if we're training an ALS based model, weight input for last.fm
# by bm25
if model_name.endswith("als"):
# lets weight these models by bm25weight.
logging.debug("weighting matrix by bm25_weight")
plays = bm25_weight(plays, K1=100, B=0.8)
# also disable building approximate recommend index
model.approximate_similar_items = False
# this is actually disturbingly expensive:
plays = plays.tocsr()
user_plays = plays.T.tocsr()
logging.debug("training model %s", model_name)
start = time.time()
model.fit(user_plays)
logging.debug("trained model '%s' in %0.2fs", model_name, time.time() - start)
# generate recommendations for each user and write out to a file
start = time.time()
with tqdm.tqdm(total=len(users)) as progress:
with codecs.open(output_filename, "w", "utf8") as o:
batch_size = 1000
to_generate = np.arange(len(users))
for startidx in range(0, len(to_generate), batch_size):
batch = to_generate[startidx : startidx + batch_size]
ids, scores = model.recommend(
batch, user_plays[batch], filter_already_liked_items=True
)
for i, userid in enumerate(batch):
username = users[userid]
for other, score in zip(ids[i], scores[i]):
o.write(f"{username}\t{artists[other]}\t{score}\n")
progress.update(len(batch))
logging.debug("generated recommendations in %0.2fs", time.time() - start)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generates similar artists on the last.fm dataset"
" or generates personalized recommendations for each user",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--output",
type=str,
default="similar-artists.tsv",
dest="outputfile",
help="output file name",
)
parser.add_argument(
"--model",
type=str,
default="als",
dest="model",
help=f"model to calculate ({'/'.join(MODELS.keys())})",
)
parser.add_argument(
"--recommend",
help="Recommend items for each user rather than calculate similar_items",
action="store_true",
)
parser.add_argument(
"--param", action="append", help="Parameters to pass to the model, formatted as 'KEY=VALUE"
)
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
if args.recommend:
calculate_recommendations(args.outputfile, model_name=args.model)
else:
calculate_similar_artists(args.outputfile, model_name=args.model)