-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsparse_owlqn.py
More file actions
138 lines (116 loc) · 3.69 KB
/
Copy pathsparse_owlqn.py
File metadata and controls
138 lines (116 loc) · 3.69 KB
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
import numpy as np
from getPath import *
pardir = getparentdir()
from commonLib import *
from adadelta import *
from sklearn import metrics
from commonmethod import *
train_path = pardir+'/data/train.ffm'
test_path = pardir+'/data/test.ffm'
auc_path = pardir+'/res/auc_owl_qn_10'
e = 1e-6
lamda = 0.0001
def reshape_arr(arr):
return np.squeeze(arr).A1
def compfunc(v,lamda):
if v+lamda<0:
return [v+lamda]
elif v-lamda>0:
return [v-lamda]
return 0
def get_pesudo_gradient(realg,w):
# shape = np.shape(realg)
g = np.array([[0.0]]*len(w))
indexs = np.array(list(range(len(w))))
# lesszero_index = indexs[reshape_arr(w<0)]
# morezero_index = indexs[reshape_arr(w>0)]
zero_index = indexs[reshape_arr(w==0)]
less = reshape_arr((w<0))
g[less] = realg[less]-lamda
more = reshape_arr((w>0))
g[more] = realg[more]+lamda
leftindex = zero_index[reshape_arr((realg[zero_index]<-lamda))]
rightindex = zero_index[reshape_arr((realg[zero_index]>lamda))]
g[leftindex] = realg[leftindex]+lamda
g[rightindex] = realg[rightindex]-lamda
g = np.matrix(g)
return g
def get_orthant(w,psudo_g):
orthant = w.copy()
nozeroindex = (w!=0)
zeroindex = (w==0)
nozerosign = np.sign(orthant[nozeroindex])
if len(nozerosign)>0:
orthant[nozeroindex]=nozerosign.A1
zerosign = np.sign(-psudo_g[zeroindex])
if len(zerosign)>0:
orthant[zeroindex]=zerosign.A1
return orthant
def fix_sign(g,sign):
res = np.multiply(g,sign)
g[res<=0]=0.0
return g
def online_owl_qn(w,vecfeatures,labels):
epsilo = 1e-4
n = np.shape(w)[0]
c = 1
k = 0
m = 10
s = []
y = []
batch_size = 100
local_lamda = 0.1
minimum = 1e-10
t0 =np.power(10,4)
ada = Adam(n,alpha=0.01)
iter = 0
lr = 0.01
while k<100:
j = 0
while j+batch_size<=len(labels):
iter+=1
# print("current iter:" +str(iter))
realg = compute_regular_gradients(vecfeatures[j:j+batch_size],labels[j:j+batch_size],w)
pg = get_pesudo_gradient(realg,w)
# pg = realg
if j==0 and k==0:
d = -pg*minimum
d = fix_sign(d,-pg)
templr = ada.getmaxgrad(d,iter)
orth = get_orthant(w,pg)
new_w = w+templr
new_w = fix_sign(new_w,orth)
sk = (new_w - w)
newg = compute_regular_gradients(vecfeatures[j:j+batch_size],labels[j:j+batch_size,:],new_w)
yk = newg-realg+local_lamda*sk
w = new_w.copy()
if len(s)>m:
s.pop(0)
y.pop(0)
s.append(sk)
y.append(yk)
begin = time.time()
d = lbfgs_two_recursion(s,y,pg,d,c=1)
end = time.time()
print_consume_time(begin,end,"two recursion",isprint=0)
# d = fix_sign(d,-pg)
j+=batch_size
if iter%10 == 0:
test(w,test_features,test_labels,auc_path,isl1=1)
k+=1
return w
test_features = 0
test_labels = 0
train_features = 0
train_labels = 0
def train():
if os.path.exists(auc_path):
os.remove(auc_path)
global train_features,train_labels
train_features,train_labels,w = initdata(test_path)
global test_features,test_labels
test_features,test_labels,_ = initdata(test_path)
maxiter = 10
w = online_owl_qn(w,train_features,train_labels)
if __name__=="__main__":
train()