Skip to content

Commit 241f71d

Browse files
committed
Fix a bug
Fix a bug
1 parent 0a63db4 commit 241f71d

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BPR puts a zero mean Gaussian prior on the learnt latent factors (embeddings) wh
1212

1313
The implementation requires python3, pandas and numpy. The dimensionality can be changed in util.py. Currently it uses a dimensionality of 50 and 10 iterations. For larger dimensionality and more number of iterations, it might be useful if there is an access to a cluster of servers or a GPU.
1414

15-
The hit rate @ position 10 is around **71%** (on a 30% test set) (the hit rate was initially 51% but I was able to do some parameter tuning and improve this to 71%) on the MovieLens [2] data set with 600 users, 9000 movies and 100,000 ratings. The data is uploaded to the data folder for convenience.
15+
The hit rate @ position 10 is around **51%** (on a 30% test set) on the MovieLens [2] data set with 600 users, 9000 movies and 100,000 ratings. The data is uploaded to the data folder for convenience.
1616

1717
The MovieLens data sets can be found on the [MovieLens Web Page](https://grouplens.org/datasets/movielens/).
1818

python/bpr.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
1313
@author: shah
1414
"""
15-
from util import m_normal, learning_rate
15+
from util import m_normal, learning_rate, get_lambda
1616
from classes import ret
1717
import random as random
1818
import numpy as np
1919
import math
2020
def bpr_update(users, movies):
2121
count = 0
2222
lr = learning_rate()
23+
lam = get_lambda()
2324
for u1 in users:
2425
u = users[u1]
2526
userid = u.userid
@@ -38,20 +39,20 @@ def bpr_update(users, movies):
3839
diff = Vi - Vj
3940
d = firstterm * diff
4041
derivative = d
41-
Vu = Vu + lr * (derivative + 0.1 * np.linalg.norm(Vu))
42+
Vu = Vu + lr * (derivative + lam * np.linalg.norm(Vu))
4243
users[u1].factor = Vu
4344

4445
# ITEM POSITIVE FACTOR
4546
d = firstterm * Vu
4647
derivative = d
47-
Vi = Vi + lr * (derivative + 0.1 * np.linalg.norm(Vi))
48+
Vi = Vi + lr * (derivative + lam * np.linalg.norm(Vi))
4849
movies[rand_pos].factor = Vi
4950

5051
#ITEM NEGATIVE FACTOR
5152
negvu = -1 * Vu
5253
d = firstterm * negvu
5354
derivative = d
54-
Vj = Vj + lr * (derivative + 0.1 * np.linalg.norm(Vj))
55+
Vj = Vj + lr * (derivative + lam * np.linalg.norm(Vj))
5556
movies[rand_neg].factor = Vj
5657

5758
def calculate_first_term(Vu, Vi, Vj):

python/filereader.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ def read_ratings(filename):
4848
user1.movies_train[movieid] = rating1
4949
users[userid] = user1
5050
trainuserdict[userid] = 1
51-
51+
else:
5252
testcount = testcount + 1
5353
if userid in users.keys():
5454
user1 = users[userid]
5555
user1.movies_test[movieid] = rating1
56-
5756
else:
5857
user1 = user(userid)
5958
user1.factor = random_vector()

python/util.py

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def min_rating():
2020
def learning_rate():
2121
return 1
2222

23+
def get_lambda():
24+
return 0.1
25+
2326
def random_vector():
2427
dim = dimension()
2528
cov_mtx = cov_matrix()

0 commit comments

Comments
 (0)