-
Notifications
You must be signed in to change notification settings - Fork 2
/
Convprob.py
214 lines (166 loc) · 6.96 KB
/
Convprob.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
import numpy as np
import time
import CPD.standard_ALS3 as stnd_ALS
import CPD.common_kernels as ck
import sys
import os
import argparse
from pathlib import Path
from os.path import dirname, join
import argparse
import arg_defs as arg_defs
import csv
from CPD.standard_ALS import CP_DTALS_Optimizer
from CPD.NLS import CP_fastNLS_Optimizer
from CPD.common_kernels import get_residual_sp, get_residual
parent_dir = dirname(__file__)
results_dir = join(parent_dir, 'results')
def convprob(tenpy,tensor,s,f_R,l_R,num_iter,num_gen,method,csv_writer=None,num_init = 10,Regu= 0.01,args=None):
conv = []
orig_Regu = Regu
decrease= True
increase=False
lower = args.lower
upper = args.upper
varying = args.varying
varying_fact = args.varying_fact
conv_tol = args.conv_tol
for R in range(f_R,l_R+1):
for k in range(num_gen):
if tensor == 'negrandom':
a = np.random.uniform(low = -1, high = 1, size= (s,R))
b = np.random.uniform(low = -1, high = 1, size= (s,R))
c = np.random.uniform(low = -1, high = 1, size= (s,R))
elif tensor == 'randn':
a = np.random.normal(size = (s,R))
b = np.random.normal(size = (s,R))
c = np.random.normal(size = (s,R))
else:
a = tenpy.random((s,R))
b = tenpy.random((s,R))
c = tenpy.random((s,R))
T = tenpy.einsum('ia,ja,ka->ijk', a,b,c)
converged = 0
for j in range(num_init):
total_iters = 0
t_all = 0.0
A = tenpy.random((s,R))
P = A.copy()
B = tenpy.random((s,R))
Q = B.copy()
C = tenpy.random((s,R))
N = C.copy()
X = [A,B,C]
Regu = orig_Regu
args.maxiter = 4*s*R
optimizer_list = {
'DT': CP_DTALS_Optimizer(tenpy,T,X,args),
'NLS': CP_fastNLS_Optimizer(tenpy,T,X,args)}
optimizer = optimizer_list[method]
prev_res = ck.get_residual3(tenpy,T,X[0],X[1],X[2])
#print('Residual is',prev_res)
start = time.time()
for i in range(num_iter):
if method == 'NLS':
[X,iters] = optimizer.step(Regu)
else:
delta = optimizer.step(Regu)
res = ck.get_residual3(tenpy,T,X[0],X[1],X[2])
if method == "NLS" :
if varying:
if Regu < lower:
increase=True
decrease=False
if Regu > upper:
decrease= True
increase=False
if increase:
Regu = Regu*varying_fact
elif decrease:
Regu = Regu/varying_fact
if method == 'DT':
if varying:
if i%100 == 0:
lower = lower/2
upper = upper/2
if Regu < lower:
increase=True
decrease=False
if Regu > upper:
decrease= True
increase=False
if increase:
Regu = Regu*varying_fact
elif decrease:
Regu = Regu/varying_fact
if res< conv_tol:
converged= 1
break
if abs(prev_res - res)< 1e-07:
break
if method == 'NLS':
if optimizer.g_norm < 1e-15:
tenpy.printf('Method converged due to gradient tolerance in',i,'iterations')
break
if optimizer.g_norm > 1e+15:
tenpy.printf('Method converged due to gradient tolerance in upper direction',i,'iterations')
break
prev_res = res
end = time.time()
res = ck.get_residual3(tenpy,T,X[0],X[1],X[2])
#print('Residual after convergence is',res)
t_all+= end - start
if converged:
break
if tenpy.is_master_proc():
# write to csv file
if csv_writer is not None:
if method != 'NLS':
csv_writer.writerow([ R, k, i,t_all, res, converged])
else:
csv_writer.writerow([ R, k,iters,t_all, res, converged])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
arg_defs.add_nls_arguments(parser)
arg_defs.add_probability_arguments(parser)
args, _ = parser.parse_known_args()
# Set up CSV logging
csv_path = join(results_dir, 'NewConvprob'+arg_defs.get_prob_file_prefix(args)+'.csv')
is_new_log = not Path(csv_path).exists()
csv_file = open(csv_path, 'a')#, newline='')
csv_writer = csv.writer(
csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
s = args.s
order = args.order
R = args.R
r = args.r
nls_tol = args.nls_tol
grad_tol = args.grad_tol
cg_tol = args.cg_tol
num=args.num
num_iter = args.num_iter
tensor = args.tensor
tlib = args.tlib
Regu = args.regularization
num_gen = args.num_gen
num_init = args.num_init
f_R = args.f_R
l_R=args.l_R
diag = args.diag
num = args.num
Arm = args.arm
c = args.c
tau = args.tau
arm_iters=args.arm_iters
if tlib == "numpy":
import backend.numpy_ext as tenpy
elif tlib == "ctf":
import backend.ctf_ext as tenpy
if tenpy.is_master_proc():
# print the arguments
for arg in vars(args) :
print( arg+':', getattr(args, arg))
# initialize the csv file
if is_new_log:
csv_writer.writerow(['R','problem','iterations', 'time', 'residual','converged'])
convprob(tenpy,tensor,s,f_R,l_R,num_iter,num_gen,args.probmethod,csv_writer,num_init,Regu,args)