-
Notifications
You must be signed in to change notification settings - Fork 185
/
CFModel.py
32 lines (24 loc) · 1.41 KB
/
CFModel.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
# A simple implementation of matrix factorization for collaborative filtering expressed as a Keras Sequential model
# Keras uses TensorFlow tensor library as the backend system to do the heavy compiling
import numpy as np
from keras.layers import Embedding, Reshape, Merge
from keras.models import Sequential
class CFModel(Sequential):
# The constructor for the class
def __init__(self, n_users, m_items, k_factors, **kwargs):
# P is the embedding layer that creates an User by latent factors matrix.
# If the intput is a user_id, P returns the latent factor vector for that user.
P = Sequential()
P.add(Embedding(n_users, k_factors, input_length=1))
P.add(Reshape((k_factors,)))
# Q is the embedding layer that creates a Movie by latent factors matrix.
# If the input is a movie_id, Q returns the latent factor vector for that movie.
Q = Sequential()
Q.add(Embedding(m_items, k_factors, input_length=1))
Q.add(Reshape((k_factors,)))
super(CFModel, self).__init__(**kwargs)
# The Merge layer takes the dot product of user and movie latent factor vectors to return the corresponding rating.
self.add(Merge([P, Q], mode='dot', dot_axes=1))
# The rate function to predict user's rating of unrated items
def rate(self, user_id, item_id):
return self.predict([np.array([user_id]), np.array([item_id])])[0][0]