-
Notifications
You must be signed in to change notification settings - Fork 1
/
score_sparse.py
executable file
·201 lines (173 loc) · 5.16 KB
/
score_sparse.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
import tensorflow as tf
import numpy as np
import math
import scipy.sparse
import random
# read query document pairs
filename = 'query_training_set_00.csv'
a = open(filename,'r')
b = a.readlines()
a.close()
# read compressed sparse representation of queries
c = open('queries_sparse.csv','r')
d = c.readlines()
c.close()
# read compressed sparse representation of documents
h = open('docs_vec9.txt','r')
o = h.readlines()
h.close()
# read compressed sparse representation of corpus
jj = open('corpus_sparse.txt','r')
kk = jj.readline()
jj.close()
# create sparse vector for corpus
ll1 = []
ll2 = []
ll3 = []
mm = kk.split(',')
for i in range(len(mm)):
ll1.append(0)
ll2.append(i)
ll3.append(int(mm[i]))
ll = scipy.sparse.coo_matrix((ll3,(ll1,ll2)), shape=(1,2534163))
# link document ids with document names
aa = open('docs_ids.txt','r')
pp = aa.readlines()
aa.close()
bb = set(pp)
cc = list(bb)
p=len(b)
labels=np.empty(shape=(50,1))
feature_cols=np.empty(shape=(50,7602489))
# create training sparse vectors
for i in range(50):
t = b[i].split('\n')
s = t[0].split(',')
labels[i]= (float(s[2]))
if i%100 == 0:
q = math.floor(i/100)
r1 = []
r2 = []
r3 = []
u = d[q]
v = u.split('\n')
z = v[0].split(',')
j = 0
while(j<len(z)-1):
if (z[j] == '0'):
r1.append(0)
r2.append(int(z[j+1]))
r3.append(int(z[j+2]))
j = j + 3
else:
j = j+1
r = scipy.sparse.coo_matrix((r3,(r1,r2)), shape=(1,2534163))
ee = s[1]+'\n'
ff = cc.index(ee)
gg = o[ff]
hh = gg.split(',')
j = 0
ii1 = []
ii2 = []
ii3 = []
while(j<len(hh)):
if (hh[j] == '0'):
ii1.append(0)
ii2.append(int(hh[j+1]))
ii3.append(int(hh[j+2]))
j = j + 3
else:
j = j + 1
ii = scipy.sparse.coo_matrix((ii3,(ii1,ii2)), shape=(1,2534163))
feature_cols[i] = np.concatenate((np.asarray(ll.todense()),np.asarray(r.todense()),np.asarray(ii.todense())),axis=1)
# release memory
del r
del ii
del cc
del bb
del pp
del mm
del kk
del b
del d
#parameters
input_size = 50
batch_size = 50
hidden_units = 16
dropout = 0
learning_rate = 1e-5
# define tf placeholders
x_train = tf.placeholder(shape=[None,7602489],dtype=tf.float32)
y_train = tf.placeholder(dtype=tf.float32)
# initial weights and biases
W1 = tf.Variable(tf.zeros([7602489,hidden_units]),dtype = tf.float32,name='W1')
b1 = tf.Variable(tf.zeros([]),dtype = tf.float32,name='b1')
# 1st hidden fully connected layer with ReLU
inputs1 = tf.matmul(x_train,W1)+b1
dropout1 = tf.layers.dropout(inputs=inputs1,rate=dropout,name='dropout1')
hidden1 = tf.contrib.layers.fully_connected(dropout1,num_outputs=hidden_units,scope='hidden1')
# output fully connected layer with linear activation
S = tf.contrib.layers.fully_connected(hidden1,num_outputs=1,activation_fn=None,scope='S')
# loss to optimize
loss = tf.losses.mean_squared_error(y_train,S)
# optimize loss
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# for 1 epoch
for epoch in range(1):
permutation = np.random.permutation(input_size)
for k in range(0, input_size, batch_size):
batches = permutation[k:k + batch_size]
batch = [feature_cols[batches], labels[batches]]
_, loss_val = sess.run([train_op, loss], feed_dict={x_train: batch[0], y_train: batch[1]})
if k % 1000 == 0:
print('k = ' + str(k) + ' loss = ' + loss_val.astype('str'))
# read compressed sparse test representations
e = open('sparse_rep2.txt','r')
g = e.readlines()
e.close()
# create test sparse vectors
valid = np.empty(shape=(50,7602489))
for i in range(50):
vv1 =[]
vv2 = []
vv3 = []
qq = g[i].split('\n')
zz = qq[0].split(',')
j=0
while j<len(zz)-1:
if (zz[j] == '0'):
vv1.append(0)
vv2.append(int(zz[j+1]))
vv3.append(int(zz[j+2]))
j=j+3
else:
j=j+1
vv = scipy.sparse.coo_matrix((vv3,(vv1,vv2)), shape=(1,2534163))
yy =[]
for j in range(1):
xx =round( random.random() * len(o)) - 1
while (xx in yy):
xx =round( random.random() * len(o)) - 1
yy.append(xx)
gg = o[xx]
print(xx)
hh = gg.split(',')
j = 0
ii1 = []
ii2 = []
ii3 = []
while(j<len(hh)):
if (hh[j] == '0'):
ii1.append(0)
ii2.append(int(hh[j+1]))
ii3.append(int(hh[j+2]))
j = j + 3
else:
j = j + 1
ii = scipy.sparse.coo_matrix((ii3,(ii1,ii2)), shape=(1,2534163))
valid[i]=(np.concatenate((np.asarray(ll.todense()),np.asarray(vv.todense()),np.asarray(ii.todense())),axis=1))
scores = sess.run([S], feed_dict={x_train: valid})
print(scores)
sess.close()