Skip to content

Commit 2f776af

Browse files
committed
Change lejasampler to set init_pivot the way choleskysampler does
1 parent 2fc360c commit 2f776af

File tree

4 files changed

+38
-69
lines changed

4 files changed

+38
-69
lines changed

pyapprox/surrogates/affine/activelearning.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,20 @@ def _generate_new_samples(self, nsamples: int) -> Array:
8484
def _update_ntrain_samples(self):
8585
self._ntrain_samples = self._train_samples.shape[1]
8686

87-
def set_seq_initial_pivots(self, init_seq_pivots: Array):
88-
self._init_seq_pivots = init_seq_pivots
89-
print(self._init_seq_pivots, self._init_seq_pivots.shape)
90-
91-
def _get_seq_init_pivots(self) -> Array:
92-
if hasattr(self, "_init_seq_pivots"):
93-
if (
94-
self._init_seq_pivots.shape[0]
95-
> self._surrogate.basis().nterms()
96-
):
87+
def set_initial_pivots(self, init_pivots: Array):
88+
self._init_pivots = init_pivots
89+
90+
def _get_init_pivots(self) -> Array:
91+
if hasattr(self, "_init_pivots"):
92+
if self._init_pivots.shape[0] > self._surrogate.basis().nterms():
9793
# this conditioned can be removed if code is changed
9894
# to allow init pivots to be passed to update pivots
9995
# but I do not think this case needs to be supported
10096
raise ValueError(
101-
"too many init_seq_pivots given. "
97+
"too many init_pivots given. "
10298
"Increase nterms of initial basis"
10399
)
104-
return self._init_seq_pivots
100+
return self._init_pivots
105101
return None
106102

107103
def _init_factorization(self, nprev_train_samples: int, nsamples: int):
@@ -115,7 +111,7 @@ def _init_factorization(self, nprev_train_samples: int, nsamples: int):
115111
self._LUFactorizer = PivotedLUFactorizer(mat, bkd=self._bkd)
116112

117113
self._L, self._U = self._LUFactorizer.factorize(
118-
nsamples, init_seq_pivots=self._get_seq_init_pivots()
114+
nsamples, init_pivots=self._get_init_pivots()
119115
)
120116
return self._LUFactorizer.pivots()[nprev_train_samples:nsamples]
121117

pyapprox/surrogates/affine/tests/test_activelearning.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ def fun(samples):
125125
gen1 = ExpandingMarginGenerator(basis1.nvars(), init_degree, 1.0, 4)
126126
basis1.set_indices(gen1.get_indices())
127127

128-
print("####")
129128
np.random.seed(1)
130129
bexp1 = AdaptivePolynomialChaosExpansion(
131130
basis1,
@@ -135,31 +134,24 @@ def fun(samples):
135134
# max_nsamples=6,
136135
nqoi=nqoi,
137136
)
138-
import torch
139-
140-
torch.set_printoptions(linewidth=1000)
141137
bexp1.build(fun)
142138

143-
print("$$$$")
144139
init_degree = 3
145140
basis2 = OrthonormalPolynomialBasis(bases_1d)
146141
gen2 = ExpandingMarginGenerator(basis2.nvars(), init_degree, 1.0, 4)
147142
basis2.set_indices(gen1.get_indices())
148143
np.random.seed(
149144
1
150-
) # sue same seed to make sure candidate samples are the same
145+
) # use the same seed to make sure candidate samples are the same
151146
bexp2 = AdaptivePolynomialChaosExpansion(
152147
basis2,
153148
variable,
154149
gen2,
155150
max_nsamples=10,
156-
# max_nsamples=6,
157151
nqoi=nqoi,
158152
)
159-
bexp2.sampler().set_seq_initial_pivots(
160-
bexp1.sampler()._LUFactorizer.seq_pivots()[
161-
: bexp1.sampler().pivots().shape[0]
162-
]
153+
bexp2.sampler().set_initial_pivots(
154+
bexp1.sampler()._LUFactorizer.pivots()
163155
)
164156
bexp2.build(fun)
165157

pyapprox/util/linalg.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,13 @@ def solve_linear_system(self, rhs: Array) -> Array:
246246

247247
class PivotedLUFactorizer(PivotedFactorizer):
248248
def _best_pivot(self, it: int) -> int:
249-
if (
250-
self._init_seq_pivots is not None
251-
and it < self._init_seq_pivots.shape[0]
252-
):
253-
return self._init_seq_pivots[it]
249+
if self._init_pivots is not None and it < len(self._init_pivots):
250+
return self._bkd.where(self._pivots == self._init_pivots[it])[0][0]
251+
# if (
252+
# self._init_seq_pivots is not None
253+
# and it < self._init_seq_pivots.shape[0]
254+
# ):
255+
# return self._init_seq_pivots[it]
254256
else:
255257
return (
256258
self._bkd.argmax(self._bkd.abs(self._LU_factor[it:, it])) + it
@@ -293,23 +295,24 @@ def _split_lu(self, LU_factor: Array, npivots: int) -> Tuple[Array, Array]:
293295
def factorize(
294296
self,
295297
npivots: int,
296-
init_seq_pivots: Array = None,
298+
init_pivots: Array = None,
297299
pivot_weights: Array = None,
298300
) -> Tuple[Array, Array]:
299-
self._init_seq_pivots = init_seq_pivots
301+
self._init_pivots = init_pivots
300302
self._ncompleted_pivots = 0
301303
self._LU_factor = self._bkd.copy(self._Amat)
302304
self._seq_pivots = self._bkd.arange(self._Amat.shape[0])
303305
return self.update(npivots)
304306

305307
def update(self, npivots: int) -> Tuple[Array, Array]:
306308
npivots = min(npivots, min(*self._Amat.shape))
307-
for it in range(self._ncompleted_pivots, npivots):
309+
for it in self._bkd.arange(self._ncompleted_pivots, npivots):
308310
pivot = self._best_pivot(it)
309311
# update pivots vector
310312
self._seq_pivots[it] = pivot
311313
# apply pivots(swap rows) in L factorization
312314
swap_rows(self._LU_factor, it, pivot)
315+
swap_rows(self._pivots, it, pivot, self._bkd)
313316
if self._terminate(it):
314317
self._termination_flag = 1
315318
return self._split_lu(self._LU_factor, it)
@@ -340,6 +343,15 @@ def add_rows(self, new_rows: Array):
340343
),
341344
)
342345
)
346+
self._pivots = self._bkd.hstack(
347+
(
348+
self._pivots,
349+
self._bkd.arange(
350+
self._Amat.shape[0],
351+
self._Amat.shape[0] + new_rows.shape[0],
352+
),
353+
)
354+
)
343355
self._Amat = self._bkd.vstack((self._Amat, new_rows))
344356
LU_factor_extra = self._bkd.copy(new_rows)
345357
for it in range(self._ncompleted_pivots):
@@ -394,29 +406,6 @@ def add_columns(self, new_cols: Array):
394406
)
395407
self._LU_factor = self._bkd.hstack((self._LU_factor, new_cols))
396408

397-
def pivots(self) -> Array:
398-
"""
399-
Return the vector that changes the original array to the final
400-
permuted array in 1 shot.
401-
402-
Return
403-
------
404-
pivots: Array (ncompleted_pivots,)
405-
"""
406-
return get_final_pivots_from_sequential_pivots(
407-
self._seq_pivots, bkd=self._bkd
408-
)[: self._ncompleted_pivots]
409-
410-
def seq_pivots(self) -> Array:
411-
"""
412-
Return the pivot vector obtained by inserting pivot at each iteratation
413-
414-
Return
415-
------
416-
seq_pivots: Array (ncandidates,)
417-
"""
418-
return self._seq_pivots
419-
420409
def _undo_preconditioning(
421410
self,
422411
precond_weights: Array,

pyapprox/util/tests/test_linalg.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def test_add_columns_to_pivoted_lu_factorization(self):
312312

313313
factorizer.update(3)
314314
full_factorizer = PivotedLUFactorizer(A, bkd=bkd)
315-
full_factorizer.factorize(3, init_seq_pivots=factorizer.seq_pivots())
315+
full_factorizer.factorize(3, init_pivots=factorizer.pivots())
316316
assert bkd.allclose(factorizer._LU_factor, full_factorizer._LU_factor)
317317

318318
def test_add_rows_to_pivoted_lu_factorization(self):
@@ -369,7 +369,7 @@ def test_add_rows_to_pivoted_lu_factorization(self):
369369
factorizer3.update(npivots + 1)
370370
full_factorizer = PivotedLUFactorizer(A_reordered, bkd=bkd)
371371
full_factorizer.factorize(
372-
npivots + 1, init_seq_pivots=factorizer3.seq_pivots()
372+
npivots + 1, init_pivots=factorizer3.pivots()
373373
)
374374
assert bkd.allclose(factorizer3._LU_factor, full_factorizer._LU_factor)
375375

@@ -389,9 +389,7 @@ def test_undo_lu_preconditioning(self):
389389
)
390390

391391
factorizer2 = PivotedLUFactorizer(A, bkd=bkd)
392-
L, U = factorizer2.factorize(
393-
npivots, init_seq_pivots=factorizer1.seq_pivots()
394-
)
392+
L, U = factorizer2.factorize(npivots, init_pivots=factorizer1.pivots())
395393
assert bkd.allclose(factorizer2.pivots(), factorizer1.pivots())
396394
assert bkd.allclose(L @ U, A[factorizer2.pivots()])
397395

@@ -426,9 +424,7 @@ def test_undo_lu_preconditioning(self):
426424
L_precond, U_precond = factorizer1.factorize(npivots)
427425

428426
factorizer2 = PivotedLUFactorizer(A, bkd=bkd)
429-
L, U = factorizer2.factorize(
430-
npivots, init_seq_pivots=factorizer1.seq_pivots()
431-
)
427+
L, U = factorizer2.factorize(npivots, init_pivots=factorizer1.pivots())
432428

433429
L_adjusted, U_adjusted = factorizer1.undo_preconditioning(
434430
precond_weights, npivots, update_internal_state=True
@@ -451,9 +447,7 @@ def test_update_lu_preconditioning(self):
451447
)
452448
new_precond_weights = precond_weights**2
453449
factorizer2 = PivotedLUFactorizer(new_precond_weights * A, bkd=bkd)
454-
L, U = factorizer2.factorize(
455-
npivots, init_seq_pivots=factorizer1.seq_pivots()
456-
)
450+
L, U = factorizer2.factorize(npivots, init_pivots=factorizer1.pivots())
457451
assert bkd.allclose(factorizer2.pivots(), factorizer1.pivots())
458452
assert bkd.allclose(
459453
L @ U, (precond_weights**2 * A)[factorizer2.pivots()]
@@ -475,9 +469,7 @@ def test_update_lu_preconditioning(self):
475469
L_precond, U_precond = factorizer1.factorize(npivots)
476470

477471
factorizer2 = PivotedLUFactorizer(new_precond_weights * A, bkd=bkd)
478-
L, U = factorizer2.factorize(
479-
npivots, init_seq_pivots=factorizer1.seq_pivots()
480-
)
472+
L, U = factorizer2.factorize(npivots, init_pivots=factorizer1.pivots())
481473

482474
L_adjusted, U_adjusted = factorizer1.update_preconditioning(
483475
precond_weights,

0 commit comments

Comments
 (0)