-
Notifications
You must be signed in to change notification settings - Fork 3
/
tensorflow_svd++_OLD.py
164 lines (122 loc) · 5.11 KB
/
tensorflow_svd++_OLD.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
import tensorflow as tf
from tensorflow_utils import *
import numpy as np
import pandas as pd
import os
import sys
from time import clock
from data_processing import *
from utils import *
import gc
submit = False
model_name = 'tensorflow_svd++'
ordering = 'um'
# useful links:
# https://github.com/aymericdamien/TensorFlow-Examples
# https://github.com/songgc/TF-recomm
# http://surprise.readthedocs.io/en/stable/matrix_factorization.html
# used techniques from this tensorflow implementation of SVD++:
# https://github.com/WindQAQ/tf-recsys/blob/master/tfcf/models/svdpp.py
def SVDpp(n_samples, n_u, n_m, mean, lf=100, reg=0.02, learning_rate=0.005):
i = tf.placeholder(tf.int32, shape=[None])
j = tf.placeholder(tf.int32, shape=[None])
r = tf.placeholder(tf.float32, shape=[None])
mpu_lookup = tf.sparse_placeholder(tf.int64)
batch = tf.shape(i)[0]
inits = tf.random_normal_initializer(mean=0.0, stddev=0.1)
regs = tf.contrib.layers.l2_regularizer(scale=reg)
mu = tf.constant([mean])
b_u = tf.get_variable('user_bias', shape=[n_u], initializer=inits, regularizer=regs)
b_m = tf.get_variable('movie_bias', shape=[n_m], initializer=inits, regularizer=regs)
p_u = tf.get_variable('user_embedding', shape=[n_u, lf], initializer=inits, regularizer=regs)
q_m = tf.get_variable('movie_embedding', shape=[n_m, lf], initializer=inits, regularizer=regs)
y_m = tf.get_variable('implicit_movie_embedding', shape=[n_m, lf], initializer=inits, regularizer=regs)
slice = tf.nn.embedding_lookup
I_u_slice = tf.nn.embedding_lookup_sparse(y_m, mpu_lookup, None, combiner='sqrtn')
# prediction is u + b_u + b_i + q_m . (p_u + sum_(j in I_u) (y_j) / |I_u|^0.5)
r_pred = tf.tile(mu, [batch]) \
+ slice(b_u, i) \
+ slice(b_m, j) \
+ tf.reduce_sum(tf.multiply(slice(p_u, i) + I_u_slice, slice(q_m, j)), 1)
reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
train_loss = tf.reduce_sum(tf.pow(r_pred-r,2)) + sum(reg_losses)
se = tf.reduce_sum(tf.pow(r_pred-r,2))
model = tf.train.GradientDescentOptimizer(learning_rate).minimize(train_loss)
return i, j, r, mpu_lookup, se, r_pred, model
print('Loading data...')
# NOTE: this model breaks on datasets where some users have no ratings, e.g. 'val'
# -- datasets that work: 'probe', 'train'
train_dataset='probe'
col_types = {'User Number': np.int32, 'Movie Number': np.int16, 'Rating': np.int8}
df = pd.read_csv(os.path.join('data', 'um_' + train_dataset + '.csv'), dtype=col_types)
row = df['User Number'].values - 1
col = df['Movie Number'].values - 1
val = df['Rating'].values
n_samples = len(val)
n_users = 1 + np.max(row)
n_movies = 1 + np.max(col)
order = np.random.permutation(n_samples)
df_val = pd.read_csv(os.path.join('data', 'um_probe.csv'))
row_val = df_val['User Number'].values - 1
col_val = df_val['Movie Number'].values - 1
val_val = df_val['Rating'].values
n_samples_val = len(val_val)
gc.collect()
print('Initializing model...')
batch = 10000
epochs = 20
i, j, r, mpu_lookup, se, pred, model = \
SVDpp(n_samples, n_users, n_movies, np.mean(val), lf=100, reg=0.02, learning_rate=5e-3)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print('Model size: %d bytes' % sess.graph_def.ByteSize())
print('Training model...')
for e in range(epochs):
sq_errs = []
sq_errs_val = []
start = clock()
for prog in range(1+int(n_samples // batch)):
a = prog * batch
b = (1 + prog) * batch
mpu = movies_per_user(row, col, row[order[a:b]])
feed_dict={
i: row[order[a:b]],
j: col[order[a:b]],
r: val[order[a:b]],
mpu_lookup: (mpu['indices'], mpu['values'], mpu['dense_shape'])
}
_, c = f_time(sess.run, [model, se], feed_dict=feed_dict)
sq_errs.append(c)
for prog in range(1+int(n_samples_val // batch)):
a = prog * batch
b = (1 + prog) * batch
mpu = movies_per_user(row, col, row_val[a:b])
feed_dict={
i: row_val[a:b],
j: col_val[a:b],
r: val_val[a:b],
mpu_lookup: (mpu['indices'], mpu['values'], mpu['dense_shape'])
}
c = sess.run(se, feed_dict=feed_dict)
sq_errs_val.append(c)
end = clock()
train_rmse = np.sqrt(np.sum(sq_errs)/n_samples)
val_rmse = np.sqrt(np.sum(sq_errs_val)/n_samples_val)
t = end - start
print('Epoch %d\t\tTrain RMSE = %.4f\tVal RMSE = %.4f\t\tTime = %.4f' % (e, train_rmse, val_rmse, t))
'''
if submit:
print('Saving submission...')
df_qual = pd.read_csv(os.path.join('data', 'mu_qual.csv'))
row_qual = df_qual['User Number'].values - 1
col_qual = df_qual['Movie Number'].values - 1
n_samples_qual = len(row_qual)
predictions = []
for prog, p in enumerate(range(1+ int(n_samples_qual // batch))):
l = prog * batch
r = (prog + 1) * batch
pr = sess.run(pred, feed_dict={i: row_qual[l:r], j: col_qual[l:r]})
predictions += list(pr)
save_submission(model_name, predictions, ordering)
'''