-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathautorec.py
executable file
·227 lines (145 loc) · 6.39 KB
/
autorec.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
224
225
226
227
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn import preprocessing
from sklearn.metrics import precision_score
k = 10
epochs = 10
display_step = 10
learning_rate = 0.3
batch_size = 250
train_data = "./train-1m.tsv"
test_data = "./test-1m.tsv"
# Reading dataset
df = pd.read_csv(train_data, sep='\t', names=['user', 'item', 'rating', 'timestamp'], header=None)
df = df.drop('timestamp', axis=1)
num_items = df.item.nunique()
num_users = df.user.nunique()
print("USERS: {} ITEMS: {}".format(num_users, num_items))
# Normalize in [0, 1]
r = df['rating'].values.astype(float)
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(r.reshape(-1,1))
df_normalized = pd.DataFrame(x_scaled)
df['rating'] = df_normalized
# Convert DataFrame in user-item matrix
matrix = df.pivot(index='user', columns='item', values='rating')
matrix.fillna(0, inplace=True)
# Users and items ordered as they are in matrix
users = matrix.index.tolist()
items = matrix.columns.tolist()
matrix = matrix.as_matrix()
print("Matrix shape: {}".format(matrix.shape))
# num_users = matrix.shape[0]
# num_items = matrix.shape[1]
# print("USERS: {} ITEMS: {}".format(num_users, num_items))
# Network Parameters
num_input = num_items # num of items
num_hidden_1 = 10 # 1st layer num features
num_hidden_2 = 5 # 2nd layer num features (the latent dim)
X = tf.placeholder(tf.float64, [None, num_input])
weights = {
'encoder_h1': tf.Variable(tf.random_normal([num_input, num_hidden_1], dtype=tf.float64)),
'encoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_hidden_2], dtype=tf.float64)),
'decoder_h1': tf.Variable(tf.random_normal([num_hidden_2, num_hidden_1], dtype=tf.float64)),
'decoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_input], dtype=tf.float64)),
}
biases = {
'encoder_b1': tf.Variable(tf.random_normal([num_hidden_1], dtype=tf.float64)),
'encoder_b2': tf.Variable(tf.random_normal([num_hidden_2], dtype=tf.float64)),
'decoder_b1': tf.Variable(tf.random_normal([num_hidden_1], dtype=tf.float64)),
'decoder_b2': tf.Variable(tf.random_normal([num_input], dtype=tf.float64)),
}
# Building the encoder
def encoder(x):
# Encoder Hidden layer with sigmoid activation #1
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']), biases['encoder_b1']))
# Encoder Hidden layer with sigmoid activation #2
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']), biases['encoder_b2']))
return layer_2
# Building the decoder
def decoder(x):
# Decoder Hidden layer with sigmoid activation #1
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']), biases['decoder_b1']))
# Decoder Hidden layer with sigmoid activation #2
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']), biases['decoder_b2']))
return layer_2
# Construct model
encoder_op = encoder(X)
decoder_op = decoder(encoder_op)
# Prediction
y_pred = decoder_op
# Targets are the input data.
y_true = X
# Define loss and optimizer, minimize the squared error
loss = tf.losses.mean_squared_error(y_true, y_pred)
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(loss)
predictions = pd.DataFrame()
# Define evaluation metrics
eval_x = tf.placeholder(tf.int32, )
eval_y = tf.placeholder(tf.int32, )
pre, pre_op = tf.metrics.precision(labels=eval_x, predictions=eval_y)
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
local_init = tf.local_variables_initializer()
with tf.Session() as session:
session.run(init)
session.run(local_init)
num_batches = int(matrix.shape[0] / batch_size)
matrix = np.array_split(matrix, num_batches)
for i in range(epochs):
avg_cost = 0
for batch in matrix:
_, l = session.run([optimizer, loss], feed_dict={X: batch})
avg_cost += l
avg_cost /= num_batches
print("Epoch: {} Loss: {}".format(i + 1, avg_cost))
# if i % display_step == 0 or i == 1:
# print('Step %i: Minibatch Loss: %f' % (i, l))
print("Predictions...")
matrix = np.concatenate(matrix, axis=0)
preds = session.run(decoder_op, feed_dict={X: matrix})
# print(matrix)
# print(preds)
predictions = predictions.append(pd.DataFrame(preds))
predictions = predictions.stack().reset_index(name='rating')
predictions.columns = ['user', 'item', 'rating']
predictions['user'] = predictions['user'].map(lambda value: users[value])
predictions['item'] = predictions['item'].map(lambda value: items[value])
# print(predictions)
print("Filtering out items in training set")
keys = ['user', 'item']
i1 = predictions.set_index(keys).index
i2 = df.set_index(keys).index
recs = predictions[~i1.isin(i2)]
recs = recs.sort_values(['user', 'rating'], ascending=[True, False])
recs = recs.groupby('user').head(k)
recs.to_csv('recs.tsv', sep='\t', index=False, header=False)
# creare un vettore dove ci sono per ogni utente i suoi 10 movies
test = pd.read_csv(test_data, sep='\t', names=['user', 'item', 'rating', 'timestamp'], header=None)
test = test.drop('timestamp', axis=1)
test = test.sort_values(['user', 'rating'], ascending=[True, False])
#test = test.groupby('user').head(k) #.reset_index(drop=True)
#test_list = test.as_matrix(columns=['item']).reshape((-1))
#recs_list = recs.groupby('user').head(k).as_matrix(columns=['item']).reshape((-1))
print("Evaluating...")
p = 0.0
for user in users[:10]:
test_list = test[(test.user == user)].head(k).as_matrix(columns=['item']).flatten()
recs_list = recs[(recs.user == user)].head(k).as_matrix(columns=['item']).flatten()
session.run(pre_op, feed_dict={eval_x: test_list, eval_y: recs_list})
#pu = precision_score(test_list, recs_list, average='micro')
#p += pu
# print("Precision for user {}: {}".format(user, pu))
# print("User test: {}".format(test_list))
# print("User recs: {}".format(recs_list))
#p /= len(users)
p = session.run(pre)
print("Precision@{}: {}".format(k, p))
# print("test len: {} - recs len: {}".format(len(test_list), len(recs_list)))
#
# print("test list - type: {}".format(type(test_list)))
# print(test_list)
#
# print("recs list - type: {}".format(type(recs_list)))
# print(recs_list)