forked from bradleypallen/keras-movielens-cf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFModel.py
47 lines (40 loc) · 1.79 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# CFModel.py
#
# A simple implementation of matrix factorization for collaborative filtering
# expressed as a Keras Sequential model. This code is based on the approach
# outlined in [Alkahest](http://www.fenris.org/)'s blog post
# [Collaborative Filtering in Keras](http://www.fenris.org/2016/03/07/collaborative-filtering-in-keras).
#
# License: MIT. See the LICENSE file for the copyright notice.
#
import numpy as np
from keras.layers import Embedding, Reshape, Merge, Dropout, Dense
from keras.models import Sequential
class CFModel(Sequential):
def __init__(self, n_users, m_items, k_factors, **kwargs):
P = Sequential()
P.add(Embedding(n_users, k_factors, input_length=1))
P.add(Reshape((k_factors,)))
Q = Sequential()
Q.add(Embedding(m_items, k_factors, input_length=1))
Q.add(Reshape((k_factors,)))
super(CFModel, self).__init__(**kwargs)
self.add(Merge([P, Q], mode='dot', dot_axes=1))
def rate(self, user_id, item_id):
return self.predict([np.array([user_id]), np.array([item_id])])[0][0]
class DeepModel(Sequential):
def __init__(self, n_users, m_items, k_factors, p_dropout=0.1, **kwargs):
P = Sequential()
P.add(Embedding(n_users, k_factors, input_length=1))
P.add(Reshape((k_factors,)))
Q = Sequential()
Q.add(Embedding(m_items, k_factors, input_length=1))
Q.add(Reshape((k_factors,)))
super(DeepModel, self).__init__(**kwargs)
self.add(Merge([P, Q], mode='concat'))
self.add(Dropout(p_dropout))
self.add(Dense(k_factors, activation='relu'))
self.add(Dropout(p_dropout))
self.add(Dense(1, activation='linear'))
def rate(self, user_id, item_id):
return self.predict([np.array([user_id]), np.array([item_id])])[0][0]