-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_wals_lib.py
147 lines (106 loc) · 4.37 KB
/
tf_wals_lib.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
import tensorflow as tf
import numpy as np
from mongoengine import connect
from scipy.sparse import coo_matrix
from tensorflow.contrib.factorization import WALSMatrixFactorization as wmf
from tensorflow.contrib.factorization import WALSModel
import json
import random
from models import PosData
from models import WepickDeal
from models import DealW2v
def wals_cate(from_date,to_date,dimension=10,weight=0.5,coef=2.0,n_iter=30):
data_path='wp_'+from_date+'_'+to_date+'_cate.json'
cate_dict=np.load('cate_dict.npy')
user_dict=np.load('user_'+from_date+'_'+to_date+'_for_cate.npy')
num_rows=len(user_dict)
num_cols=len(cate_dict)
with open(data_path,'r') as f:
data=json.load(f)
indices=[]
values=[]
for idx,elem in enumerate(data):
indices+=zip([idx]*len(elem),elem)
values+=[1.0]*len(elem)
with tf.Graph().as_default() as graph1:
sp_mat=tf.SparseTensor(indices,values,[num_rows,num_cols])
model=WALSModel(num_rows,num_cols,dimension,weight,coef,row_weights=None,col_weights=None)
row_factors=model.row_factors[0]
col_factors=model.col_factors[0]
sess=tf.Session(graph=graph1)
row_update_op=model.update_row_factors(sp_mat)[1]
col_update_op=model.update_col_factors(sp_mat)[1]
sess.run(model.initialize_op)
for _ in range(n_iter):
sess.run(model.row_update_prep_gramian_op)
sess.run(model.initialize_row_update_op)
sess.run(row_update_op)
sess.run(model.col_update_prep_gramian_op)
sess.run(model.initialize_col_update_op)
sess.run(col_update_op)
output_row=row_factors.eval(sess).tolist()
output_col=col_factors.eval(sess).tolist()
sess.close()
# temporary mechanism for generated matrice
random.seed()
temp_num=str(random.randrange(100))
user_temp_name='temp_user'+temp_num
item_temp_name='temp_item'+temp_num
with open('../'+user_temp_name+'.json','w') as f:
json.dump(output_row,f)
with open('../'+item_temp_name+'.json','w') as f:
json.dump(output_col,f)
print('files saved')
return dimension, user_temp_name,item_temp_name
def wals(id,from_date,to_date,predict_moment,dimension=30,weight=0.5,coef=2.0,n_iter=30):
data_path='wp_'+from_date+'_'+to_date+'_sparse.json'
deal_dict=np.load('dict_'+from_date+'_'+to_date+'_for_sparse.npy')
user_dict=np.load('user_'+from_date+'_'+to_date+'.npy')
if id not in user_dict:
return -1
else:
user_index=np.where(user_dict==id)[0][0]
num_rows=len(user_dict)
num_cols=len(deal_dict)
connect('wprec',host='mongodb://10.102.61.251:27017')
deals=WepickDeal.objects(pk__gte=predict_moment+' 20',pk__lte=predict_moment+' 99')
deal_slots=[]
deal_ids=[]
predict_input=[]
for elem in deals:
dealid=elem['deal'].id
if dealid in deal_dict:
deal_slots.append(int(elem.id[-2:]))
deal_ids.append(elem['deal'].id)
deal_finder=dict(zip(deal_dict,range(num_cols)))
with open(data_path,'r') as f:
data=json.load(f)
indices=[]
values=[]
for idx,elem in enumerate(data):
indices+=zip([idx]*len(elem),elem)
values+=[1.0]*len(elem)
with tf.Graph().as_default() as graph1:
sp_mat=tf.SparseTensor(indices,values,[num_rows,num_cols])
model=WALSModel(num_rows,num_cols,dimension,weight,coef,row_weights=None,col_weights=None)
row_factors=model.row_factors[0]
col_factors=model.col_factors[0]
sess=tf.Session(graph=graph1)
row_update_op=model.update_row_factors(sp_mat)[1]
col_update_op=model.update_col_factors(sp_mat)[1]
sess.run(model.initialize_op)
for _ in range(n_iter):
sess.run(model.row_update_prep_gramian_op)
sess.run(model.initialize_row_update_op)
sess.run(row_update_op)
sess.run(model.col_update_prep_gramian_op)
sess.run(model.initialize_col_update_op)
sess.run(col_update_op)
output_row=row_factors.eval(sess)
output_col=col_factors.eval(sess)
sess.close()
results=[]
for i in range(len(deal_ids)):
deal_index=deal_finder[deal_ids[i]]
results.append({'id':deal_ids[i],'slot':deal_slots[i],'score':sum(output_row[user_index][:]*output_col[deal_index])})
return results