-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwo_tower_SVD_NN_Recommender.py
223 lines (182 loc) · 8.2 KB
/
Two_tower_SVD_NN_Recommender.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import pandas as pd
import tensorflow as tf
import numpy as np
from surprise import Dataset
from surprise import Reader
from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder, LabelEncoder
from sklearn.model_selection import train_test_split
from pandas import read_csv
# for neural networks
import keras
from keras import backend as K
from keras.regularizers import l2
from keras import backend as K
from keras import initializers
from keras.models import Sequential, Model
from keras.layers.core import Dense, Lambda, Activation
from keras.layers import Embedding, Input, Dense, merge, Reshape, concatenate, Flatten, Dropout, BatchNormalization, Add,ActivityRegularization
from keras.constraints import maxnorm
from keras.optimizers import Adagrad, Adam, SGD, RMSprop
from time import time
from tensorflow.keras import initializers
import keras.utils.vis_utils
from keras.utils.vis_utils import plot_model
from keras.layers.merge import dot
from keras.utils import to_categorical
import time
# Ignore the warnings
tf.get_logger().setLevel('ERROR')
start = time.time()
# import data
df_movies = pd.read_csv('movies.csv', usecols=['movieId', 'title', 'genres'],
dtype={'movieId': 'int32', 'title': 'str', 'genres': 'str'})
df_ratings = pd.read_csv('ratings.csv', usecols=['userId', 'movieId', 'rating'],
dtype={'userId': 'int32', 'movieId': 'int32', 'rating': 'float32'})
def root_mean_squared_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true)))
def recall_m(y_true, y_pred):
true_positives = K.sum(K.round(1-K.clip(abs((y_true - y_pred))/5+0.3, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision_m(y_true, y_pred):
true_positives = K.sum(K.round(1-K.clip(abs((y_true - y_pred))/5+0.3, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
# df_movies.set_index('movieId')
# create categories for unique movieIds
# add a new column for category id
df_ratings.insert(2, "movieId_cat", (df_ratings.movieId.astype('category').cat.codes.values), True)
df_ratings.userId = df_ratings.userId.astype('category').cat.codes.values
users_movies = df_ratings.pivot(index='userId', columns='movieId', values='rating').fillna(0)
# create a map dataframe for movieIds
d = {'movieId_cat': df_ratings.movieId_cat.unique(), 'movieId': df_ratings.movieId.unique()}
df_movieId_map = pd.DataFrame(d)
# delete the non-sequential column of movieIds
df_ratings.drop('movieId_cat', axis=1, inplace=True)
P_nap = users_movies.copy()
reader = Reader(rating_scale=(0.5, 5))
data_2 = Dataset.load_from_df(df_ratings, reader)
trainset = data_2.build_full_trainset()
testset = trainset.build_anti_testset(fill=0)
trainset_list = trainset.build_testset()
trainset_df = pd.DataFrame(trainset_list)
trainset_df.columns = ['userId', 'movieId', 'rating']
testset_df = pd.DataFrame(testset)
testset_df.columns = ['userId', 'movieId', 'rating']
# create categories for unique movieIds
trainset_df.userId = trainset_df.userId.astype('category').cat.codes.values
trainset_df.movieId = trainset_df.movieId.astype('category').cat.codes.values
testset_df.userId = testset_df.userId.astype('category').cat.codes.values
testset_df.movieId = testset_df.movieId.astype('category').cat.codes.values
users = trainset_df.userId.unique()
movies = trainset_df.movieId.unique()
# train = df_ratings.copy()
split = np.random.rand(len(trainset_df)) < 0.8
train = trainset_df[split]
valid = trainset_df[~split]
n_movies = len(df_ratings['movieId'].unique())
n_users = len(df_ratings['userId'].unique())
# Input variables
user_input = Input(shape=(1,), dtype='int64', name='user_input')
item_input = Input(shape=(1,), dtype='int64', name='item_input')
Embedding_User = Embedding(input_dim=n_users, output_dim=64, name='user_embedding')
Embedding_Item = Embedding(input_dim=n_movies, output_dim=64, name='item_embedding')
# Crucial to flatten an embedding vector!
user_latent = Flatten()(Embedding_User(user_input))
user_latent = Dropout(0.4)(user_latent)
item_latent = Flatten()(Embedding_Item(item_input))
item_latent = Dropout(0.4)(item_latent)
# user tower
u_vec_lat = Flatten()(user_latent)
u_vec_lat = Dropout(0.4)(u_vec_lat)
#u_vec_lat = BatchNormalization()(u_vec_lat)
nn_u_vec = Dense(64,activation='relu')(u_vec_lat)
nn_u_vec= BatchNormalization()(nn_u_vec)
nn_u_vec = Dropout(0.4)(nn_u_vec)
nn_u_vec = Dense(32,activation='relu')(nn_u_vec)
nn_u_vec= BatchNormalization()(nn_u_vec)
nn_u_vec = Dropout(0.4)(nn_u_vec)
nn_u_vec = Dense(16,activation='relu')(nn_u_vec)
nn_u_vec= BatchNormalization()(nn_u_vec)
nn_u_vec = Dropout(0.4)(nn_u_vec)
nn_u_vec = Dense(8,activation='relu')(nn_u_vec)
nn_u_vec= BatchNormalization()(nn_u_vec)
nn_u_vec = Dropout(0.4)(nn_u_vec)
# item tower
i_vec_lat = Flatten()(item_latent)
i_vec_lat = Dropout(0.4)(i_vec_lat)
#i_vec_lat = BatchNormalization()(i_vec_lat)
nn_i_vec = Dense(64,activation='relu')(i_vec_lat)
nn_i_vec = BatchNormalization()(nn_i_vec)
nn_i_vec = Dropout(0.4)(nn_i_vec)
nn_i_vec = Dense(32,activation='relu')(nn_i_vec)
nn_i_vec= BatchNormalization()(nn_i_vec)
nn_i_vec = Dropout(0.4)(nn_i_vec)
nn_i_vec = Dense(16,activation='relu')(nn_i_vec)
nn_i_vec= BatchNormalization()(nn_i_vec)
nn_i_vec = Dropout(0.4)(nn_i_vec)
nn_i_vec = Dense(8,activation='relu')(nn_i_vec)
nn_i_vec= BatchNormalization()(nn_i_vec)
nn_i_vec = Dropout(0.4)(nn_i_vec)
sim = dot([nn_u_vec, nn_i_vec], name='Simalarity-Dot-Product', axes=1)
nn_inp = Dense(128, activation='relu')(sim)
#nn_inp = BatchNormalization()(nn_inp)
nn_inp = Dropout(0.4)(nn_inp)
prediction = Dense(1, activation='relu')(nn_inp)
# build model
model = keras.models.Model([user_input, item_input], prediction)
model.compile(loss=root_mean_squared_error, optimizer=Adam(lr=1e-4),metrics=[precision_m,'acc',recall_m])
batch_size = 128
epochs = 10
History = model.fit([train.userId, train.movieId], train.rating, batch_size=batch_size,
epochs=epochs, validation_data=([valid.userId, valid.movieId], valid.rating),
verbose=1)
end_1 = time.time()
print(end_1-start)
predictions = model.predict([testset_df.userId, testset_df.movieId])
# convert it to dataframe
df = pd.DataFrame(predictions)
df.columns = ['ratings']
# assign the new predicted ratings
testset_df_new = testset_df.copy()
testset_df_new = testset_df_new.assign(rating=df['ratings'])
# re create the new filled user-item matrix only with predicted values
users_movies_predicted = testset_df_new.pivot(index='movieId', columns='userId', values='rating').fillna(0)
new = users_movies_predicted.copy()
end_2 = time.time()
print(end_2-start)
plot_model(model, to_file="SVD_NN_recommender.png", show_shapes=True, show_layer_names=True)
# evaluate the model
from pylab import rcParams
rcParams['figure.figsize'] = 10, 5
import matplotlib.pyplot as plt
plt.plot(History.history['loss'], 'g')
plt.plot(History.history['val_loss'], 'b')
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.grid(True)
plt.show()
def show(target_user_id):
movies_ids_cat = new.nlargest(10, [target_user_id])
movieId = np.empty(10, dtype=int)
recommendations = np.empty([10,3],dtype=object)
k = 0
for i in range(len(movies_ids_cat)):
for j in range(len(df_movieId_map)):
if movies_ids_cat.index[i] == df_movieId_map.movieId_cat[j]:
movieId[k] = df_movieId_map.movieId[j]
k = k + 1
for k in range(10):
for l in range(len(df_movies)):
if df_movies.movieId[l] == movieId[k]:
recommendations[k][0] = df_movies['title'][l]
recommendations[k][1] = df_movies['genres'][l]
recommendations[k][2] = df_movies['movieId'][l]
#print(df_movies['title'][l])
#print(df_movies['genres'][l])
return recommendations
df_movieId_map = df_movieId_map.sort_values(by=['movieId_cat'])