-
Notifications
You must be signed in to change notification settings - Fork 2
/
BCSH_paper.py
244 lines (208 loc) · 8.55 KB
/
BCSH_paper.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -*- coding: utf-8 -*-
"""
@author: yingwenjie
"""
import scipy
import scipy.io
import numpy as np
from utils import *
from evaluation import *
from supervise import *
def save_arg(B,weight,bits):
[m, n] = weight.shape
"""
得到哈希码后,计算所有位的参数
"""
B_copy = B.copy()
tempB1 = B_copy
tempB0 = -B_copy
tempB1[tempB1 < 0] = 0
tempB0[tempB0 < 0] = 0
PX1_B1 = scipy.sparse.csr_matrix(tempB1) * weight
PX1_B1 = PX1_B1.toarray().astype(float)
ALL1 = np.sum(PX1_B1, 1, keepdims=True)
PX1_B1 = (PX1_B1 + 1) / (ALL1 + n)
PX1_B0 = scipy.sparse.csr_matrix(tempB0) * weight
PX1_B0 = PX1_B0.toarray().astype(float)
ALL0 = np.sum(PX1_B0, 1, keepdims=True)
PX1_B0 = (PX1_B0 + 1) / (ALL0 + n)
logPX1_B1 = np.log2(PX1_B1)
logPX1_B0 = np.log2(PX1_B0)
print(logPX1_B1.shape)
print(logPX1_B0.shape)
B_copy[B_copy < 0] = 0
Bs = B_copy
B_index = []
for i in range(m):
index = 0
for j in range(bits):
index += 2** (bits - j - 1) * Bs[j,i]
B_index.append(index)
B_index = np.array(B_index)
scipy.io.savemat('./data/argfile/arg2.mat',{'B_index':B_index,'logPX1_B1': logPX1_B1,'logPX1_B0': logPX1_B0})
#def BCSH_paper(B, tfidf, label, bits=32, iters=10):
def BCSH_paper(B, tfidf, label, B_test, tfidf_test, label_test, B_dev, tfidf_dev, label_dev, bits=32, iters=100):
weight = tfidf
[m, n] = weight.shape
print(m, n)
weight_test = tfidf_test
weight_dev = tfidf_dev
#B = np.random.randint(0,2,(bits,m)) #随机初始化哈希码B
B[B < 0] = -1
B[B >= 0] = 1
B_test[B_test < 0] = -1
B_test[B_test >= 0] = 1
B_dev[B_dev < 0] = -1
B_dev[B_dev >= 0] = 1
best_loss = float('inf')
for it in range(iters):
print("===============" + str(it) + "================")
alpha3 = 0
for s in range(1):
print(B)
B_grad,loss = train1(B, label, B_test, label_test, B_dev, label_dev)
print(B_grad)
B = B - alpha3 * B_grad.t().numpy()
B[B < 0] = -1
B[B >= 0] = 1
print(B)
#if loss < best_loss:
# best_loss = loss
# save_arg(B,weight,bits)
for r in range(bits):
B_bit = B[r, :].reshape(1, m)
tempB0 = -B[r, :].reshape(1, m)
tempB1 = -tempB0
tempB0[tempB0 < 0] = 0
tempB1[tempB1 < 0] = 0
PX1_B1 = scipy.sparse.csr_matrix(tempB1) * weight
PX1_B1 = PX1_B1.toarray().astype(float)
ALL1 = np.sum(PX1_B1, 1, keepdims=True)
PX1_B1 = (PX1_B1 + 1) / (ALL1 + n)
PX1_B0 = scipy.sparse.csr_matrix(tempB0) * weight
PX1_B0 = PX1_B0.toarray().astype(float)
ALL0 = np.sum(PX1_B0, 1, keepdims=True)
PX1_B0 = (PX1_B0 + 1)/(ALL0 + n)
logPX1_B1 = np.log2(PX1_B1)
logPX1_B0 = np.log2(PX1_B0)
logPB1 = weight * scipy.sparse.csr_matrix(logPX1_B1.transpose())
logPB0 = weight * scipy.sparse.csr_matrix(logPX1_B0.transpose())
logPB1 = logPB1.toarray().transpose()
logPB0 = logPB0.toarray().transpose()
tmp = (logPB1 - logPB0) ### 规范化很重要
tmp[tmp > 32] = 32
PXB1 = np.power(2,tmp)
PXB1 = PXB1 / (1 + PXB1)
Fx = PXB1 * 2 -1
##位平衡约束
alpha1 = 0.5
bit_balance = np.sum(B_bit, axis=1).reshape(1, 1) / m
print(bit_balance[0,0])
Fx -= alpha1 * bit_balance
##位独立约束
alpha2 = 0.1
bit_un = np.dot(B_bit, B.transpose()).reshape(1, bits) / m #* r #逐位学习位约束逐渐加强
for i in range(r, bits):
bit_un[0,i] = 0
bit_un_res = np.dot(bit_un, B).reshape(1, m)
Fx -= alpha2 * bit_un_res
#print(bit_un_res)
#Fx -= alpha3 * B_grad.t().numpy()[r,:]
## 监督梯度
#train1(B,label)
#old_B = B.copy()
#B[r,:] = Fx[0,:]
B[r, Fx[0,:] < 0] = -1
B[r, Fx[0,:] >= 0] = 1
#updateB = sum(sum(B!=old_B))
#print(updateB)
#loss1 = np.trace(np.dot((B_bit - Fx),(B_bit - Fx).transpose()))
#loss2 = np.sum(B_bit,axis=1).reshape(1,bits) / m
#bit_un = np.dot(B, B.transpose()) / m
#loss3 = np.trace(np.dot(bit_un,bit_un.transpose()))
#Loss = loss1 + alpha1 * loss2[0,r] + alpha2 * loss3
#print('Loss=' + str(Loss) + "\t" + "loss1: " + str(loss1) + "\t" + "loss2: " + str(loss2) + "\t" + "loss3: " + str(loss3))
#print('Loss=' + str(Loss) + "\t" + "loss1: " + str(loss1) + "\t" + "loss2: " + str(loss2) + "\t" + "loss3: " + str(loss3))
#eval_retrieval(B.transpose(), label.transpose(), top_n=100)
loss2 = np.sum(B, axis=1)
loss3 = (np.dot(B, B.transpose()))
loss4 = sum(sum(abs(np.dot(B, B.transpose())))) - np.trace(np.dot(B, B.transpose()))
print(logPX1_B1.shape)
print("loss2" + "\t" + str(loss2))
print("loss3" + "\t" + str(loss3))
print("loss4" + "\t" + str(loss4))
"""
# 得到哈希码后,计算所有位的参数
"""
B_copy = B.copy()
tempB1 = B_copy
tempB0 = -B_copy
tempB1[tempB1 < 0] = 0
tempB0[tempB0 < 0] = 0
PX1_B1 = scipy.sparse.csr_matrix(tempB1) * weight
PX1_B1 = PX1_B1.toarray().astype(float)
ALL1 = np.sum(PX1_B1, 1, keepdims=True)
PX1_B1 = (PX1_B1 + 1) / (ALL1 + n)
PX1_B0 = scipy.sparse.csr_matrix(tempB0) * weight
PX1_B0 = PX1_B0.toarray().astype(float)
ALL0 = np.sum(PX1_B0, 1, keepdims=True)
PX1_B0 = (PX1_B0 + 1) / (ALL0 + n)
logPX1_B1 = np.log2(PX1_B1)
logPX1_B0 = np.log2(PX1_B0)
print(logPX1_B1.shape)
print(logPX1_B0.shape)
logPB1 = weight * scipy.sparse.csr_matrix(logPX1_B1.transpose())
logPB0 = weight * scipy.sparse.csr_matrix(logPX1_B0.transpose())
logPB1 = logPB1.toarray().transpose()
logPB0 = logPB0.toarray().transpose()
tmp = (logPB1 - logPB0) ### 规范化很重要
tmp[tmp > 32] = 32
PXB1 = np.power(2, tmp)
PXB1 = PXB1 / (1 + PXB1)
B = PXB1 * 2 -1
B[B < 0] = -1
B[B >= 0] = 1
logPB1 = weight_test * scipy.sparse.csr_matrix(logPX1_B1.transpose())
logPB0 = weight_test * scipy.sparse.csr_matrix(logPX1_B0.transpose())
logPB1 = logPB1.toarray().transpose()
logPB0 = logPB0.toarray().transpose()
tmp = (logPB1 - logPB0) ### 规范化很重要
tmp[tmp > 32] = 32
PXB1 = np.power(2, tmp)
PXB1 = PXB1 / (1 + PXB1)
B_test = PXB1 * 2 -1
B_test[B_test < 0] = -1
B_test[B_test >= 0] = 1
logPB1 = weight_dev * scipy.sparse.csr_matrix(logPX1_B1.transpose())
logPB0 = weight_dev * scipy.sparse.csr_matrix(logPX1_B0.transpose())
logPB1 = logPB1.toarray().transpose()
logPB0 = logPB0.toarray().transpose()
tmp = (logPB1 - logPB0) ### 规范化很重要
tmp[tmp > 32] = 32
PXB1 = np.power(2, tmp)
PXB1 = PXB1 / (1 + PXB1)
B_dev = PXB1 * 2 -1
B_dev[B_dev < 0] = -1
B_dev[B_dev >= 0] = 1
##1->2
logPX1_B1_B0 = (logPX1_B1 - logPX1_B0)
logPX1_B1_B0_sign = np.zeros((logPX1_B1_B0.shape), dtype = int)
logPX1_B1_B0_sign[logPX1_B1_B0 >= 0] = 1
logPX1_B1_B0_sign[logPX1_B1_B0 < 0] = -1
B_num = weight * logPX1_B1_B0_sign.transpose()
B_test_num = weight_test * logPX1_B1_B0_sign.transpose()
B_dev_num = weight_dev * logPX1_B1_B0_sign.transpose()
print("1->2")
print(B_dev_num)
B_grad, loss = train1(B_num.T, label, B_test_num.T, label_test, B_dev_num.T, label_dev)
B[B < 0] = 0
Bs = B
B_index = []
for i in range(m):
index = 0
for j in range(bits):
index += 2** (bits - j - 1) * Bs[j, i]
B_index.append(index)
B_index = np.array(B_index)
scipy.io.savemat('./data/argfile/arg2.mat', {'B_index': B_index, 'logPX1_B1': logPX1_B1, 'logPX1_B0': logPX1_B0})
return Bs