forked from songgc/TF-recomm
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsvd_query.py
94 lines (83 loc) · 3.4 KB
/
svd_query.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
import time
from collections import deque
import numpy as np
import tensorflow as tf
from six import next
from tensorflow.core.framework import summary_pb2
from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file
import socket
import sys
import dataio
import ops
np.random.seed(13575)
BATCH_SIZE = 1000
USER_NUM = 6040
ITEM_NUM = 3952
DIM = 15
EPOCH_MAX = 100
DEVICE = "/cpu:0"
def clip(x):
return np.clip(x, 1.0, 5.0)
def make_scalar_summary(name, val):
return summary_pb2.Summary(value=[summary_pb2.Summary.Value(tag=name, simple_value=val)])
def get_movies():
df = dataio.read_movies("/tmp/movielens/ml-1m/movies.dat", sep="::")
rows = len(df)
return df, rows
if __name__ == '__main__':
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph("tfrecomm.meta")
new_saver.restore(sess, tf.train.latest_checkpoint('./'))
sess.run(tf.global_variables_initializer())
print ("Get Movies Data")
moviefile,rows = get_movies()
graph = tf.get_default_graph()
infer = graph.get_tensor_by_name("svd_inference:0")
user_batch = graph.get_tensor_by_name("id_user:0")
item_batch = graph.get_tensor_by_name("id_item:0")
movies=list(range(len(moviefile)))
infer = graph.get_tensor_by_name("svd_inference:0")
users=[1]
feed_dict={user_batch: users,item_batch: movies}
pred_batch = sess.run(infer, feed_dict)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('0.0.0.0', 81)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
sock.listen(1)
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()
try:
print >>sys.stderr, 'connection from', client_address
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print >>sys.stderr, 'received "%s"' % data
if data:
del users[:]
try:
user = int(data)
except:
break
users.append(int(data))
print (users)
pred_batch = sess.run(infer, feed_dict={user_batch: users,item_batch: movies})
moviesrecomm=list(zip(movies,pred_batch))
smovies=sorted (moviesrecomm,key=lambda x:x[1],reverse=True)
topmovies= smovies[0:10]
print (topmovies)
for item in topmovies:
itopmovie=item[0]
recommendedmovie=moviefile["title"][itopmovie]
recommendedtags=moviefile["tags"][itopmovie]
#print >>sys.stderr, 'sending data back to the client'
connection.sendall(recommendedmovie+":"+recommendedtags+"\n")
#print >>sys.stderr, 'Sent data'
else:
print >>sys.stderr, 'no more data from', client_address
break
finally:
connection.close()