-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathmf.py
146 lines (117 loc) · 3.59 KB
/
mf.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
# https://udemy.com/recommender-systems
# https://deeplearningcourses.com/recommender-systems
from __future__ import print_function, division
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from datetime import datetime
# load in the data
import os
if not os.path.exists('user2movie.json') or \
not os.path.exists('movie2user.json') or \
not os.path.exists('usermovie2rating.json') or \
not os.path.exists('usermovie2rating_test.json'):
import preprocess2dict
with open('user2movie.json', 'rb') as f:
user2movie = pickle.load(f)
with open('movie2user.json', 'rb') as f:
movie2user = pickle.load(f)
with open('usermovie2rating.json', 'rb') as f:
usermovie2rating = pickle.load(f)
with open('usermovie2rating_test.json', 'rb') as f:
usermovie2rating_test = pickle.load(f)
N = np.max(list(user2movie.keys())) + 1
# the test set may contain movies the train set doesn't have data on
m1 = np.max(list(movie2user.keys()))
m2 = np.max([m for (u, m), r in usermovie2rating_test.items()])
M = max(m1, m2) + 1
print("N:", N, "M:", M)
# initialize variables
K = 10 # latent dimensionality
W = np.random.randn(N, K)
b = np.zeros(N)
U = np.random.randn(M, K)
c = np.zeros(M)
mu = np.mean(list(usermovie2rating.values()))
# prediction[i,j] = W[i].dot(U[j]) + b[i] + c.T[j] + mu
def get_loss(d):
# d: (user_id, movie_id) -> rating
N = float(len(d))
sse = 0
for k, r in d.items():
i, j = k
p = W[i].dot(U[j]) + b[i] + c[j] + mu
sse += (p - r)*(p - r)
return sse / N
# train the parameters
epochs = 25
reg =20. # regularization penalty
train_losses = []
test_losses = []
for epoch in range(epochs):
print("epoch:", epoch)
epoch_start = datetime.now()
# perform updates
# update W and b
t0 = datetime.now()
for i in range(N):
# for W
matrix = np.eye(K) * reg
vector = np.zeros(K)
# for b
bi = 0
for j in user2movie[i]:
r = usermovie2rating[(i,j)]
matrix += np.outer(U[j], U[j])
vector += (r - b[i] - c[j] - mu)*U[j]
bi += (r - W[i].dot(U[j]) - c[j] - mu)
# set the updates
W[i] = np.linalg.solve(matrix, vector)
b[i] = bi / (len(user2movie[i]) + reg)
if i % (N//10) == 0:
print("i:", i, "N:", N)
print("updated W and b:", datetime.now() - t0)
# update U and c
t0 = datetime.now()
for j in range(M):
# for U
matrix = np.eye(K) * reg
vector = np.zeros(K)
# for c
cj = 0
try:
for i in movie2user[j]:
r = usermovie2rating[(i,j)]
matrix += np.outer(W[i], W[i])
vector += (r - b[i] - c[j] - mu)*W[i]
cj += (r - W[i].dot(U[j]) - b[i] - mu)
# set the updates
U[j] = np.linalg.solve(matrix, vector)
c[j] = cj / (len(movie2user[j]) + reg)
if j % (M//10) == 0:
print("j:", j, "M:", M)
except KeyError:
# possible not to have any ratings for a movie
pass
print("updated U and c:", datetime.now() - t0)
print("epoch duration:", datetime.now() - epoch_start)
# store train loss
t0 = datetime.now()
train_losses.append(get_loss(usermovie2rating))
# store test loss
test_losses.append(get_loss(usermovie2rating_test))
print("calculate cost:", datetime.now() - t0)
print("train loss:", train_losses[-1])
print("test loss:", test_losses[-1])
print("train losses:", train_losses)
print("test losses:", test_losses)
# plot losses
plt.plot(train_losses, label="train loss")
plt.plot(test_losses, label="test loss")
plt.legend()
plt.show()