gegd 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gegd/__init__.py +0 -0
- gegd/optimizer/AF_PSO.py +251 -0
- gegd/optimizer/AF_STE.py +217 -0
- gegd/optimizer/GEGD.py +893 -0
- gegd/optimizer/TF.py +200 -0
- gegd/optimizer/__init__.py +0 -0
- gegd/parameter_processing/__init__.py +0 -0
- gegd/parameter_processing/density_transforms.py +177 -0
- gegd/parameter_processing/symmetry_operations.py +224 -0
- gegd-0.0.1.dist-info/LICENSE +674 -0
- gegd-0.0.1.dist-info/METADATA +25 -0
- gegd-0.0.1.dist-info/RECORD +14 -0
- gegd-0.0.1.dist-info/WHEEL +5 -0
- gegd-0.0.1.dist-info/top_level.txt +1 -0
gegd/__init__.py
ADDED
|
File without changes
|
gegd/optimizer/AF_PSO.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import gegd.parameter_processing.density_transforms as dtf
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
from mpi4py import MPI
|
|
6
|
+
comm = MPI.COMM_WORLD
|
|
7
|
+
|
|
8
|
+
class optimizer:
|
|
9
|
+
def __init__(self,
|
|
10
|
+
Nx,
|
|
11
|
+
Ny,
|
|
12
|
+
Nswarm,
|
|
13
|
+
symmetry,
|
|
14
|
+
periodic,
|
|
15
|
+
padding,
|
|
16
|
+
maxiter,
|
|
17
|
+
high_fidelity_setting,
|
|
18
|
+
brush_size,
|
|
19
|
+
upsample_ratio=1,
|
|
20
|
+
beta_proj=8,
|
|
21
|
+
brush_shape='circle',
|
|
22
|
+
cost_obj=None,
|
|
23
|
+
Nthreads=1,
|
|
24
|
+
):
|
|
25
|
+
|
|
26
|
+
self.Nx = Nx
|
|
27
|
+
self.Ny = Ny
|
|
28
|
+
self.Nswarm = Nswarm
|
|
29
|
+
self.symmetry = symmetry
|
|
30
|
+
self.periodic = periodic
|
|
31
|
+
self.padding = padding
|
|
32
|
+
self.maxiter = maxiter
|
|
33
|
+
self.beta_proj = beta_proj
|
|
34
|
+
self.brush_size = brush_size
|
|
35
|
+
self.brush_shape = brush_shape
|
|
36
|
+
self.upsample_ratio = upsample_ratio
|
|
37
|
+
self.sigma_filter = brush_size/2/np.sqrt(2)
|
|
38
|
+
self.high_fidelity_setting = high_fidelity_setting
|
|
39
|
+
self.cost_obj = cost_obj
|
|
40
|
+
self.Nthreads = Nthreads
|
|
41
|
+
|
|
42
|
+
# Get Number of Independent Parameters
|
|
43
|
+
if symmetry == 0:
|
|
44
|
+
self.Ndim = Nx*Ny
|
|
45
|
+
|
|
46
|
+
elif symmetry == 1:
|
|
47
|
+
self.Ndim = int(np.floor(Nx/2 + 0.5)*Ny)
|
|
48
|
+
|
|
49
|
+
elif symmetry == 2:
|
|
50
|
+
self.Ndim = int(np.floor(Nx/2 + 0.5)*np.floor(Ny/2 + 0.5))
|
|
51
|
+
|
|
52
|
+
elif symmetry == 4:
|
|
53
|
+
self.Ndim = int(np.floor(Nx/2 + 0.5)*(np.floor(Nx/2 + 0.5) + 1)/2)
|
|
54
|
+
|
|
55
|
+
def get_loss_swarm(self, x):
|
|
56
|
+
# Get Brush Binarized Densities ------------------------------------------------------------
|
|
57
|
+
x_bin = dtf.binarize(x, self.symmetry, self.periodic, self.Nx, self.Ny, self.brush_size, self.brush_shape, self.beta_proj, self.sigma_filter,
|
|
58
|
+
upsample_ratio=self.upsample_ratio, padding=self.padding, Nthreads=self.Nthreads)
|
|
59
|
+
|
|
60
|
+
# Sample Modified Cost Function --------------------------------------------------------------
|
|
61
|
+
self.cost_obj.set_accuracy(self.high_fidelity_setting)
|
|
62
|
+
|
|
63
|
+
f_swarm = np.zeros(self.Nswarm)
|
|
64
|
+
for n in range(self.Nswarm):
|
|
65
|
+
f_swarm[n] = self.cost_obj.get_cost(x_bin[n,:], get_grad=False)
|
|
66
|
+
|
|
67
|
+
return f_swarm, x_bin
|
|
68
|
+
|
|
69
|
+
def PSO(self,
|
|
70
|
+
lb,
|
|
71
|
+
ub,
|
|
72
|
+
coeff_cognitive=1.49,
|
|
73
|
+
coeff_social=1.49,
|
|
74
|
+
coeff_inertia=0.9,
|
|
75
|
+
velocity=None,
|
|
76
|
+
x=None,
|
|
77
|
+
pbest_loss=None,
|
|
78
|
+
pbest_x=None,
|
|
79
|
+
pbest_x_bin=None,
|
|
80
|
+
):
|
|
81
|
+
|
|
82
|
+
vmax = ub - lb
|
|
83
|
+
if velocity is None and x is None:
|
|
84
|
+
#Latin Hypercube Particle Initialization
|
|
85
|
+
increment = (ub - lb)/self.Nswarm
|
|
86
|
+
|
|
87
|
+
x = np.zeros((self.Nswarm, self.Ndim))
|
|
88
|
+
velocity = np.zeros((self.Nswarm, self.Ndim))
|
|
89
|
+
for nd in range(self.Ndim):
|
|
90
|
+
swarm_ind = np.random.permutation(self.Nswarm).reshape(self.Nswarm)
|
|
91
|
+
if lb[nd] == ub[nd]:
|
|
92
|
+
x[:,nd] = lb[nd]*np.ones(self.Nswarm)
|
|
93
|
+
velocity[:,nd] = np.zeros(self.Nswarm)
|
|
94
|
+
else:
|
|
95
|
+
x[:,nd] = lb[nd] + (swarm_ind + 1)*increment[nd] - increment[nd]*np.random.rand(self.Nswarm)
|
|
96
|
+
velocity[:,nd] = -vmax[nd] + 2*vmax[nd]*np.random.rand(self.Nswarm)
|
|
97
|
+
|
|
98
|
+
if pbest_loss is None and pbest_x is None:
|
|
99
|
+
# Initialize pbest
|
|
100
|
+
pbest_loss = np.zeros(self.Nswarm)
|
|
101
|
+
pbest_x = np.zeros((self.Nswarm, self.Ndim))
|
|
102
|
+
|
|
103
|
+
while True:
|
|
104
|
+
t1 = time.time()
|
|
105
|
+
if self.n_iter > self.maxiter:
|
|
106
|
+
break
|
|
107
|
+
|
|
108
|
+
loss_swarm, x_bin = self.get_loss_swarm(x)
|
|
109
|
+
|
|
110
|
+
#Particle Best
|
|
111
|
+
if self.n_iter == 0:
|
|
112
|
+
pbest_loss = loss_swarm.copy()
|
|
113
|
+
pbest_x = x.copy()
|
|
114
|
+
pbest_x_bin = x_bin.copy()
|
|
115
|
+
else:
|
|
116
|
+
pbest_mask = loss_swarm < pbest_loss
|
|
117
|
+
pbest_loss[pbest_mask] = loss_swarm[pbest_mask]
|
|
118
|
+
pbest_x[pbest_mask,:] = x[pbest_mask,:]
|
|
119
|
+
pbest_x_bin[pbest_mask,:] = x_bin[pbest_mask,:]
|
|
120
|
+
|
|
121
|
+
#Global Best
|
|
122
|
+
gbest_ind = np.argmin(pbest_loss)
|
|
123
|
+
if self.gbest_x_hist is None:
|
|
124
|
+
self.gbest_loss_hist = np.append(self.gbest_loss_hist, pbest_loss[gbest_ind])
|
|
125
|
+
self.gbest_x_hist = pbest_x[gbest_ind,:].copy()
|
|
126
|
+
self.gbest_x_binary_hist = pbest_x_bin[gbest_ind,:].copy()
|
|
127
|
+
else:
|
|
128
|
+
if self.gbest_loss_hist[-1] > pbest_loss[gbest_ind]:
|
|
129
|
+
self.gbest_loss_hist = np.append(self.gbest_loss_hist, pbest_loss[gbest_ind])
|
|
130
|
+
self.gbest_x_hist = np.vstack((self.gbest_x_hist, pbest_x[gbest_ind,:]))
|
|
131
|
+
self.gbest_x_binary_hist = np.vstack((self.gbest_x_binary_hist, pbest_x_bin[gbest_ind,:]))
|
|
132
|
+
else:
|
|
133
|
+
self.gbest_loss_hist = np.append(self.gbest_loss_hist, self.gbest_loss_hist[-1])
|
|
134
|
+
if self.gbest_x_hist.ndim == 1:
|
|
135
|
+
self.gbest_x_hist = np.vstack((self.gbest_x_hist, self.gbest_x_hist))
|
|
136
|
+
self.gbest_x_binary_hist = np.vstack((self.gbest_x_binary_hist, self.gbest_x_binary_hist))
|
|
137
|
+
else:
|
|
138
|
+
self.gbest_x_hist = np.vstack((self.gbest_x_hist, self.gbest_x_hist[-1,:]))
|
|
139
|
+
self.gbest_x_binary_hist = np.vstack((self.gbest_x_binary_hist, self.gbest_x_binary_hist[-1,:]))
|
|
140
|
+
|
|
141
|
+
t2 = time.time()
|
|
142
|
+
t_rem = (t2 - t1)*(self.maxiter - self.n_iter + 1)/3600
|
|
143
|
+
|
|
144
|
+
if comm.rank == 0:
|
|
145
|
+
print(' | %12d | %12.5f | %12.5f | %12.5f | %5.2f |' %(self.n_iter, np.mean(pbest_loss), np.std(pbest_loss), self.gbest_loss_hist[-1], t_rem), flush=True)
|
|
146
|
+
self.save_data(coeff_inertia=coeff_inertia,
|
|
147
|
+
velocity=velocity,
|
|
148
|
+
x=x,
|
|
149
|
+
pbest_loss=pbest_loss,
|
|
150
|
+
pbest_x=pbest_x,
|
|
151
|
+
pbest_x_bin=pbest_x_bin)
|
|
152
|
+
|
|
153
|
+
#Parameter Updates
|
|
154
|
+
if self.gbest_loss_hist.size >= 5 and self.gbest_loss_hist[-1] == self.gbest_loss_hist[-5]:
|
|
155
|
+
coeff_inertia *= 0.95
|
|
156
|
+
|
|
157
|
+
#Velocity Updates
|
|
158
|
+
if self.gbest_x_hist.ndim == 1:
|
|
159
|
+
velocity = coeff_inertia*velocity + coeff_cognitive*np.random.rand()*(pbest_x - x) + coeff_social*np.random.rand()*(self.gbest_x_hist[np.newaxis,:] - x)
|
|
160
|
+
else:
|
|
161
|
+
velocity = coeff_inertia*velocity + coeff_cognitive*np.random.rand()*(pbest_x - x) + coeff_social*np.random.rand()*(self.gbest_x_hist[-1,:][np.newaxis,:] - x)
|
|
162
|
+
|
|
163
|
+
vmax_violation = np.abs(velocity) - vmax[np.newaxis,:]
|
|
164
|
+
vmax_violation_mask = np.sum(np.abs(velocity) > vmax[np.newaxis,:], axis=1).astype(bool)
|
|
165
|
+
vmax_violation_dim = np.argmax(vmax_violation, axis=1)[vmax_violation_mask]
|
|
166
|
+
velocity[vmax_violation_mask,:] *= (vmax[vmax_violation_dim]/np.max(np.abs(velocity[vmax_violation_mask,:]), axis=1))[:,np.newaxis]
|
|
167
|
+
|
|
168
|
+
#Craziness
|
|
169
|
+
N_cr = np.random.permutation(self.Nswarm)[:int(self.Nswarm/10)]
|
|
170
|
+
if np.random.rand() < 0.22:
|
|
171
|
+
for t in range(int(self.Nswarm/10)):
|
|
172
|
+
velocity[N_cr[t],:] = -vmax + vmax*np.random.rand(self.Ndim)
|
|
173
|
+
|
|
174
|
+
#Position Updates
|
|
175
|
+
x += velocity
|
|
176
|
+
|
|
177
|
+
lb_violation_mask = x < lb[np.newaxis,:]
|
|
178
|
+
ub_violation_mask = x > ub[np.newaxis,:]
|
|
179
|
+
x_lb = lb[np.newaxis,:] + (lb[np.newaxis,:] - x)
|
|
180
|
+
x_ub = ub[np.newaxis,:] - (x - ub[np.newaxis,:])
|
|
181
|
+
x[lb_violation_mask] = x_lb[lb_violation_mask]
|
|
182
|
+
x[ub_violation_mask] = x_ub[ub_violation_mask]
|
|
183
|
+
|
|
184
|
+
self.n_iter += 1
|
|
185
|
+
|
|
186
|
+
def run(self, n_seed, output_filename, coeff_cognitive=1.49, coeff_social=1.49, coeff_inertia=0.9, load_data=False):
|
|
187
|
+
if comm.rank == 0:
|
|
188
|
+
print('### Particle Swarm Optimization (seed = ' + str(n_seed) + ')\n', flush=True)
|
|
189
|
+
|
|
190
|
+
np.random.seed(n_seed)
|
|
191
|
+
self.output_filename = output_filename
|
|
192
|
+
|
|
193
|
+
lb = -np.ones(self.Ndim)
|
|
194
|
+
ub = np.ones(self.Ndim)
|
|
195
|
+
|
|
196
|
+
if load_data:
|
|
197
|
+
data_file1 = output_filename + "_AF_PSO_results.npz"
|
|
198
|
+
data_file2 = output_filename + "_AF_PSO_density_hist.npz"
|
|
199
|
+
|
|
200
|
+
with np.load(data_file1) as data:
|
|
201
|
+
self.n_iter = data['n_iter']
|
|
202
|
+
self.gbest_loss_hist = data['gbest_loss_hist'][:self.n_iter]
|
|
203
|
+
|
|
204
|
+
coeff_inertia = data['coeff_inertia']
|
|
205
|
+
pbest_loss = data['pbest_loss']
|
|
206
|
+
|
|
207
|
+
with np.load(data_file2) as data:
|
|
208
|
+
self.gbest_x_hist = data['gbest_x_hist'][:self.n_iter,:]
|
|
209
|
+
self.gbest_x_binary_hist = data['gbest_x_binary_hist'][:self.n_iter,:]
|
|
210
|
+
|
|
211
|
+
velocity = data['velocity']
|
|
212
|
+
x = data['x']
|
|
213
|
+
pbest_x = data['pbest_x']
|
|
214
|
+
pbest_x_bin = data['pbest_x_bin']
|
|
215
|
+
|
|
216
|
+
else:
|
|
217
|
+
self.gbest_loss_hist = np.zeros(0)
|
|
218
|
+
self.gbest_x_hist = None
|
|
219
|
+
self.gbest_x_binary_hist = None
|
|
220
|
+
|
|
221
|
+
velocity = None
|
|
222
|
+
x = None
|
|
223
|
+
pbest_loss = None
|
|
224
|
+
pbest_x = None
|
|
225
|
+
pbest_x_bin = None
|
|
226
|
+
|
|
227
|
+
self.n_iter = 0
|
|
228
|
+
|
|
229
|
+
if comm.rank == 0:
|
|
230
|
+
print(' | Iteration | PBest Avg. | PBest Stdev. | GBest Cost | t_rem(hr) |', flush=True)
|
|
231
|
+
|
|
232
|
+
self.PSO(lb, ub, coeff_inertia=coeff_inertia,
|
|
233
|
+
velocity=velocity, x=x, pbest_loss=pbest_loss, pbest_x=pbest_x, pbest_x_bin=pbest_x_bin)
|
|
234
|
+
|
|
235
|
+
self.save_data()
|
|
236
|
+
|
|
237
|
+
def save_data(self, coeff_inertia=0, velocity=0, x=0, pbest_loss=0, pbest_x=0, pbest_x_bin=0):
|
|
238
|
+
if comm.rank == 0:
|
|
239
|
+
np.savez(output_filename + "_AF_PSO_results",
|
|
240
|
+
gbest_loss_hist=self.gbest_loss_hist,
|
|
241
|
+
n_iter=self.n_iter,
|
|
242
|
+
coeff_inertia=coeff_inertia,
|
|
243
|
+
pbest_loss=pbest_loss)
|
|
244
|
+
|
|
245
|
+
np.savez(output_filename + "_AF_PSO_density_hist",
|
|
246
|
+
gbest_x_hist=self.gbest_x_hist,
|
|
247
|
+
gbest_x_binary_hist=self.gbest_x_binary_hist,
|
|
248
|
+
velocity=velocity,
|
|
249
|
+
x=x,
|
|
250
|
+
pbest_x=pbest_x,
|
|
251
|
+
pbest_x_bin=pbest_x_bin)
|
gegd/optimizer/AF_STE.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import gegd.parameter_processing.density_transforms as dtf
|
|
3
|
+
import gegd.parameter_processing.symmetry_operations as symOp
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
from mpi4py import MPI
|
|
7
|
+
comm = MPI.COMM_WORLD
|
|
8
|
+
|
|
9
|
+
class optimizer:
|
|
10
|
+
def __init__(self,
|
|
11
|
+
Nx,
|
|
12
|
+
Ny,
|
|
13
|
+
Ntrial,
|
|
14
|
+
symmetry,
|
|
15
|
+
periodic,
|
|
16
|
+
padding,
|
|
17
|
+
high_fidelity_setting,
|
|
18
|
+
brush_size,
|
|
19
|
+
upsample_ratio=1,
|
|
20
|
+
beta_proj=8,
|
|
21
|
+
brush_shape='circle',
|
|
22
|
+
cost_obj=None,
|
|
23
|
+
Nthreads=1,
|
|
24
|
+
):
|
|
25
|
+
|
|
26
|
+
self.Nx = Nx
|
|
27
|
+
self.Ny = Ny
|
|
28
|
+
self.Ntrial = Ntrial
|
|
29
|
+
self.symmetry = symmetry
|
|
30
|
+
self.periodic = periodic
|
|
31
|
+
self.padding = padding
|
|
32
|
+
self.beta_proj = beta_proj
|
|
33
|
+
self.brush_size = brush_size
|
|
34
|
+
self.brush_shape = brush_shape
|
|
35
|
+
self.high_fidelity_setting = high_fidelity_setting
|
|
36
|
+
self.cost_obj = cost_obj
|
|
37
|
+
self.Nthreads = Nthreads
|
|
38
|
+
|
|
39
|
+
# Get Number of Independent Parameters
|
|
40
|
+
if symmetry == 0:
|
|
41
|
+
self.Ndim = Nx*Ny
|
|
42
|
+
|
|
43
|
+
elif symmetry == 1:
|
|
44
|
+
self.Ndim = int(np.floor(Nx/2 + 0.5)*Ny)
|
|
45
|
+
|
|
46
|
+
elif symmetry == 2:
|
|
47
|
+
self.Ndim = int(np.floor(Nx/2 + 0.5)*np.floor(Ny/2 + 0.5))
|
|
48
|
+
|
|
49
|
+
elif symmetry == 4:
|
|
50
|
+
self.Ndim = int(np.floor(Nx/2 + 0.5)*(np.floor(Nx/2 + 0.5) + 1)/2)
|
|
51
|
+
|
|
52
|
+
def straight_through_jacobian(self, x0):
|
|
53
|
+
# Get Brush Binarized Densities ------------------------------------------------------------
|
|
54
|
+
x_bin = dtf.binarize(x0, self.symmetry, self.periodic, self.Nx, self.Ny, self.brush_size, self.brush_shape, self.beta_proj, self.sigma_filter, padding=self.padding, Nthreads=self.Nthreads)
|
|
55
|
+
|
|
56
|
+
# Sample Modified Cost Function --------------------------------------------------------------
|
|
57
|
+
self.cost_obj.set_accuracy(self.high_fidelity_setting)
|
|
58
|
+
|
|
59
|
+
f0 = np.zeros(self.Ntrial)
|
|
60
|
+
jac_STE = np.zeros((self.Ntrial, self.Ndim))
|
|
61
|
+
for n in range(self.Ntrial):
|
|
62
|
+
f0[n], jac_temp = self.cost_obj.get_cost(x_bin[n,:], get_grad=True)
|
|
63
|
+
jac_sym = jac_temp.reshape(self.Nx, self.Ny)
|
|
64
|
+
jac_STE[n,:] = dtf.backprop_filter_and_project(jac_sym, x0[n,:], self.symmetry, self.periodic, self.Nx, self.Ny, self.brush_size, self.sigma_filter, self.beta_proj, padding=self.padding)
|
|
65
|
+
|
|
66
|
+
return f0, jac_STE, x_bin
|
|
67
|
+
|
|
68
|
+
def ADAM(self,
|
|
69
|
+
x_bounded,
|
|
70
|
+
lb,
|
|
71
|
+
ub,
|
|
72
|
+
beta_ADAM1,
|
|
73
|
+
beta_ADAM2,
|
|
74
|
+
eta_ADAM,
|
|
75
|
+
maxiter,
|
|
76
|
+
beta=0,
|
|
77
|
+
jac_mean=None,
|
|
78
|
+
jac_var=None,
|
|
79
|
+
adam_iter=None,
|
|
80
|
+
):
|
|
81
|
+
|
|
82
|
+
# Dummy Variables
|
|
83
|
+
x = -np.log((ub[np.newaxis,:] - lb[np.newaxis,:])/(x_bounded - lb[np.newaxis,:]) - 1)
|
|
84
|
+
x[:,ub==lb] = ub[ub==lb][np.newaxis,:]
|
|
85
|
+
|
|
86
|
+
if jac_mean is None:
|
|
87
|
+
jac_mean = np.zeros_like(x)
|
|
88
|
+
if jac_var is None:
|
|
89
|
+
jac_var = np.zeros_like(x)
|
|
90
|
+
if adam_iter is None:
|
|
91
|
+
adam_iter = 0
|
|
92
|
+
|
|
93
|
+
while True:
|
|
94
|
+
t1 = time.time()
|
|
95
|
+
adam_iter += 1
|
|
96
|
+
|
|
97
|
+
x_bounded = lb[np.newaxis,:] + (ub[np.newaxis,:] - lb[np.newaxis,:])/(1 + np.exp(-x))
|
|
98
|
+
|
|
99
|
+
loss, jac, x_bin = self.straight_through_jacobian(x_bounded)
|
|
100
|
+
|
|
101
|
+
jac[:,ub==lb] = 0
|
|
102
|
+
jac *= np.exp(-x)*(ub[np.newaxis,:] - lb[np.newaxis,:])/(1 + np.exp(-x))**2
|
|
103
|
+
|
|
104
|
+
if self.cost_hist is None:
|
|
105
|
+
self.cost_hist = loss.copy()
|
|
106
|
+
else:
|
|
107
|
+
self.cost_hist = np.vstack((self.cost_hist, loss))
|
|
108
|
+
|
|
109
|
+
if self.x_latent_hist is None:
|
|
110
|
+
self.x_latent_hist = x_bounded[np.argmin(loss),:].copy()
|
|
111
|
+
else:
|
|
112
|
+
self.x_latent_hist = np.vstack((self.x_latent_hist, x_bounded[np.argmin(loss),:]))
|
|
113
|
+
|
|
114
|
+
if self.x_hist is None:
|
|
115
|
+
self.x_hist = x_bin[np.argmin(loss),:].copy()
|
|
116
|
+
else:
|
|
117
|
+
self.x_hist = np.vstack((self.x_hist, x_bin[np.argmin(loss),:]))
|
|
118
|
+
|
|
119
|
+
t2 = time.time()
|
|
120
|
+
|
|
121
|
+
if comm.rank == 0:
|
|
122
|
+
t_rem = (t2 - t1)*(maxiter - self.n_iter + 1)/3600
|
|
123
|
+
print(' | %12d | %12.5f | %5.2f |' %(self.n_iter, np.min(loss), t_rem), flush=True)
|
|
124
|
+
|
|
125
|
+
self.save_data(x_bounded=x_bounded,
|
|
126
|
+
x_bin=x_bin,
|
|
127
|
+
jac_mean=jac_mean,
|
|
128
|
+
jac_var=jac_var,
|
|
129
|
+
adam_iter=adam_iter)
|
|
130
|
+
|
|
131
|
+
if adam_iter >= maxiter:
|
|
132
|
+
break
|
|
133
|
+
|
|
134
|
+
# Update Average Gradients
|
|
135
|
+
jac_mean = beta_ADAM1*jac_mean + (1 - beta_ADAM1)*jac
|
|
136
|
+
jac_var = beta_ADAM2*jac_var + (1 - beta_ADAM2)*jac**2
|
|
137
|
+
|
|
138
|
+
# Unbias Average Gradients
|
|
139
|
+
jac_mean_unbiased = jac_mean/(1 - beta_ADAM1**adam_iter)
|
|
140
|
+
jac_var_unbiased = jac_var/(1 - beta_ADAM2**adam_iter)
|
|
141
|
+
|
|
142
|
+
# Update Variables
|
|
143
|
+
x -= eta_ADAM*jac_mean_unbiased/(np.sqrt(jac_var_unbiased) + 1e-8)
|
|
144
|
+
# eta *= 0.95
|
|
145
|
+
|
|
146
|
+
self.n_iter += 1
|
|
147
|
+
|
|
148
|
+
return x_bounded
|
|
149
|
+
|
|
150
|
+
def run(self, n_seed, output_filename, maxiter, eta_ADAM=0.1, load_data=False):
|
|
151
|
+
if comm.rank == 0:
|
|
152
|
+
print('### Brush Optimization (seed = ' + str(n_seed) + ')\n', flush=True)
|
|
153
|
+
|
|
154
|
+
self.suffix = suffix
|
|
155
|
+
self.sigma_filter = self.brush_size/2/np.sqrt(2)
|
|
156
|
+
|
|
157
|
+
lb = -np.ones(self.Ndim)
|
|
158
|
+
ub = np.ones(self.Ndim)
|
|
159
|
+
|
|
160
|
+
if load_data:
|
|
161
|
+
data_file1 = output_filename + "_AF_STE_results.npz"
|
|
162
|
+
data_file2 = output_filename + "_AF_STE_density_hist.npz"
|
|
163
|
+
|
|
164
|
+
with np.load(data_file1) as data:
|
|
165
|
+
self.n_iter = data['n_iter']
|
|
166
|
+
self.cost_hist = data['cost_hist'][:self.n_iter,:]
|
|
167
|
+
adam_iter = data['adam_iter'] - 1
|
|
168
|
+
|
|
169
|
+
with np.load(data_file2) as data:
|
|
170
|
+
self.x_latent_hist = data['x_latent_hist'][:self.n_iter,:]
|
|
171
|
+
self.x_hist = data['x_hist'][:self.n_iter,:]
|
|
172
|
+
x0 = data['x_bounded']
|
|
173
|
+
jac_mean = data['jac_mean']
|
|
174
|
+
jac_var = data['jac_var']
|
|
175
|
+
|
|
176
|
+
else:
|
|
177
|
+
self.x_latent_hist = None
|
|
178
|
+
self.x_hist = None
|
|
179
|
+
self.cost_hist = None
|
|
180
|
+
|
|
181
|
+
self.n_iter = 0
|
|
182
|
+
|
|
183
|
+
# Initial Structure
|
|
184
|
+
if n_seed is not None:
|
|
185
|
+
np.random.seed(n_seed)
|
|
186
|
+
|
|
187
|
+
x0 = 2*np.random.rand(self.Ntrial, self.Ndim) - 1
|
|
188
|
+
np.random.seed()
|
|
189
|
+
|
|
190
|
+
jac_mean = None
|
|
191
|
+
jac_var = None
|
|
192
|
+
adam_iter = None
|
|
193
|
+
|
|
194
|
+
if comm.rank == 0:
|
|
195
|
+
print(' | Iteration | Best Cost | t_rem(hr) |', flush=True)
|
|
196
|
+
|
|
197
|
+
x_bin = self.ADAM(x0, lb, ub, 0.667, 0.9, eta_ADAM, maxiter, jac_mean=jac_mean, jac_var=jac_var, adam_iter=adam_iter)
|
|
198
|
+
|
|
199
|
+
if comm.rank == 0:
|
|
200
|
+
print('', flush=True)
|
|
201
|
+
|
|
202
|
+
self.save_data(x_bin=x_bin)
|
|
203
|
+
|
|
204
|
+
def save_data(self, x_bounded=0, x_bin=0, jac_mean=0, jac_var=0, adam_iter=0):
|
|
205
|
+
if comm.rank == 0:
|
|
206
|
+
np.savez(output_filename + "_AF_STE_results",
|
|
207
|
+
cost_hist=self.cost_hist,
|
|
208
|
+
n_iter=self.n_iter,
|
|
209
|
+
adam_iter=adam_iter)
|
|
210
|
+
|
|
211
|
+
np.savez(output_filename + "_AF_STE_density_hist",
|
|
212
|
+
x_hist=self.x_hist,
|
|
213
|
+
x_latent_hist=self.x_latent_hist,
|
|
214
|
+
x_bounded=x_bounded,
|
|
215
|
+
x_bin=x_bin,
|
|
216
|
+
jac_mean=jac_mean,
|
|
217
|
+
jac_var=jac_var)
|