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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534 | class Selector:
"""Intelligent parameter space explorer that searches for optimal configurations.
Uses a combination of random seeding, delta-based local search around the best
points, and additional random exploration to efficiently search a high-dimensional
parameter space. Scores are evaluated in parallel using multiprocessing.
Example:
from py_misc_utils import select_params as sp
def score_fn(a, b):
return -(a - 2) ** 2 - (b - 20) ** 2
sel = sp.Selector({'a': [1, 2, 3], 'b': [10, 20, 30]},
seeds=[{'a': 1, 'b': 10}])
sel(score_fn, top_n=2, explore_pct=1.0, max_blanks_pct=1.0,
scores_x_run=2, n_jobs=1)
print(sel.best_param) # {'a': 2, 'b': 20}
print(sel.best_score) # 0
Attributes:
cpid: Counter for assigning point IDs.
current_scores: List of scores from the current batch.
processed_scores: Count of scores processed so far.
"""
def __init__(self, params, seeds=None):
"""Initialize the Selector with a parameter space and optional seed points.
Args:
params: Dictionary mapping parameter names to array-like values. Each
value will be converted to a sorted numpy array.
seeds: Optional iterable of parameter dicts to use as starting points.
Each seed's values are matched to the nearest available parameter value.
Defaults to None.
"""
self.nparams = _norm_params(params)
self.processed = set()
self.scores_db = collections.defaultdict(list)
self.best_score, self.best_idx, self.best_param = None, None, None
self.pid_scores = dict()
self.blanks = 0
self.skeys, self.space = _get_space(self.nparams)
self.pts, self.cpid = [], 0
self.current_scores, self.processed_scores = [], 0
if seeds:
self._load_seeds(seeds)
def _load_seeds(self, seeds):
"""Load seed parameter configurations as initial search points.
For each seed, finds the nearest available parameter value for each key
and adds the resulting point if it hasn't been processed already.
Args:
seeds: Iterable of parameter dicts to use as starting points.
"""
for sd in seeds:
idx = np.zeros(len(self.space), dtype=np.int32)
for i, k in enumerate(self.skeys):
v = sd[k]
idx[i] = np.argmin(np.abs(self.nparams[k] - v))
if not self._is_processed(idx):
self.pts.append(Point(self.cpid, idx))
self.cpid += 1
else:
alog.info(f'Seed already processed: {sd}')
def _register_scores(self, xparams, scores):
"""Record scores and parameter values for a batch of evaluated points.
Args:
xparams: List of parameter dicts that were evaluated.
scores: List of score values corresponding to each param dict.
"""
for params, score in zip(xparams, scores):
alog.debug0(f'SCORE: {score} -- {params}')
for k, v in params.items():
self.scores_db[k].append(v)
self.scores_db['SCORE'].append(score)
def _make_param(self, idx):
"""Convert an index array to a parameter dict using scalar values.
Args:
idx: Array-like of integer indices into the parameter value spaces.
Returns:
A dictionary mapping parameter names to Python scalar values.
"""
param = dict()
for i, k in enumerate(self.skeys):
# We keep parameters as numpy arrays, but when we pluck values we want to
# return them in Python scalar form.
pvalue = self.nparams[k][idx[i]]
param[k] = pvalue.item()
return param
def _score_slice(self, pts, score_fn, n_jobs=None, mp_ctx=None):
"""Evaluate score_fn on a slice of points, optionally in parallel.
Args:
pts: List of Point namedtuples to evaluate.
score_fn: Callable accepting keyword arguments from parameter dicts.
n_jobs: Number of parallel workers. If None, uses os.cpu_count(). If 1,
runs sequentially. Defaults to None.
mp_ctx: Multiprocessing context method name. Defaults to None.
Returns:
A list of score values, one per point in pts.
"""
xparams = [self._make_param(pt.idx) for pt in pts]
n_jobs = os.cpu_count() if n_jobs is None else n_jobs
if n_jobs == 1:
scores = [score_fn(**p) for p in xparams]
else:
context = mp.get_context(mp_ctx if mp_ctx is not None else mp.get_start_method())
fn = functools.partial(_mp_score_fn, score_fn)
with mp_pool.Pool(processes=n_jobs if n_jobs > 0 else None,
context=context) as pool:
scores = list(pool.map(fn, xparams))
self._register_scores(xparams, scores)
return scores
def _fetch_scores(self,
score_fn,
n_jobs=None,
mp_ctx=None,
scores_x_run=None,
status_path=None):
"""Fetch scores in batches from the pending points list.
Processes points in chunks of scores_x_run, evaluating each batch and
optionally saving status after each batch.
Points are marked as processed as soon as their score is fetched, so
that status files saved mid-run never carry stale dedup information:
a run resumed from any save does not re-evaluate already scored
configurations.
Args:
score_fn: Callable accepting keyword arguments from parameter dicts.
n_jobs: Number of parallel workers. Defaults to None.
mp_ctx: Multiprocessing context method name. Defaults to None.
scores_x_run: Number of scores to process per batch. Defaults to
_SCORES_X_RUN.
status_path: Optional file path to save intermediate status. Defaults to None.
"""
scores_x_run = scores_x_run or _SCORES_X_RUN
for i in range(self.processed_scores, len(self.pts), scores_x_run):
current_points = self.pts[i:i + scores_x_run]
scores = self._score_slice(current_points, score_fn, n_jobs=n_jobs, mp_ctx=mp_ctx)
for pt in current_points:
self.processed.add(pt.idx.tobytes())
self.current_scores.extend(scores)
self.processed_scores += len(current_points)
alog.info(f'Processed {self.processed_scores}/{len(self.pts)}: ' \
f'{ut.format(sorted(self.current_scores, reverse=True), ".6e")}')
if status_path is not None:
self.save_status(status_path)
def _select_top_n(self, top_n, min_pid_gain_pct):
"""Select indices of the top N points by score, ensuring unique PIDs.
Uses np.argsort with np.flip for descending sort, then filters to keep
only the best score per point ID. Applies a minimum gain threshold.
Args:
top_n: Maximum number of points to select.
min_pid_gain_pct: Minimum relative gain required to accept a new score
for an already-seen point ID.
Returns:
A list of indices into self.pts for the selected top points.
"""
# The np.argsort() has no "reverse" option, so it's either np.flip() or negate
# the scores.
sidx = np.flip(np.argsort(self.current_scores))
pseen, fsidx = set(), []
for i in sidx:
pt = self.pts[i]
if pt.pid not in pseen:
pseen.add(pt.pid)
pscore = self.pid_scores.get(pt.pid)
if pscore is None or _is_worth_gain(pscore, self.current_scores[i],
min_pid_gain_pct):
self.pid_scores[pt.pid] = self.current_scores[i]
fsidx.append(i)
# The sidx array contains indices mapping to a descending sort of the
# scores, so once we have top_n of them, we know we have selected the
# higher ones available.
if len(fsidx) >= top_n:
break
return fsidx
def _is_processed(self, idx):
"""Check if a given index array has already been processed.
Args:
idx: Array-like of integer indices.
Returns:
True if the index's byte representation is in the processed set.
"""
return idx.tobytes() in self.processed
def _generating(self, dest, count):
"""Generator yielding attempt numbers while generating new points.
Yields attempt numbers until either the destination has enough points or
the maximum number of attempts is reached.
Args:
dest: List to append generated points to.
count: Target number of points to generate.
Yields:
Integer attempt numbers.
"""
max_attempts = round(count * _KGEN_EXTRA)
n = 0
while count > len(dest) and n < max_attempts:
yield n
n += 1
def _randgen(self, count):
"""Generate random unprocessed points within the parameter space.
Args:
count: Number of random points to generate.
Returns:
A list of Point namedtuples with random indices.
"""
rng = np.random.default_rng()
high = np.array(self.space, dtype=np.int32)
low = np.zeros_like(high)
rpoints = []
for _ in self._generating(rpoints, count):
idx = rng.integers(low, high)
if not self._is_processed(idx):
rpoints.append(Point(self.cpid, idx))
self.cpid += 1
return rpoints
def _select_deltas(self, pt, delta_spacek, delta_std):
"""Generate delta perturbations around a given point.
Creates new points near pt.idx by sampling within a subset of dimensions
determined by delta_spacek, with perturbation scale delta_std.
Args:
pt: The Point namedtuple to generate deltas around.
delta_spacek: Fraction or count of dimensions to perturb. If None, all
dimensions. If positive, fraction of total dimensions. If negative,
absolute count of dimensions.
delta_std: Standard deviation scale for Gaussian perturbation.
Returns:
A list of Point namedtuples representing delta variations.
"""
if delta_spacek is None:
num_deltas = len(self.space)
elif delta_spacek > 0:
num_deltas = int(np.ceil(len(self.space) * delta_spacek))
else:
num_deltas = int(np.rint(-delta_spacek))
deltas = []
for _ in self._generating(deltas, num_deltas):
idx = _mkdelta(pt.idx, self.space, delta_std)
if not self._is_processed(idx):
deltas.append(Point(pt.pid, idx))
return deltas
def __call__(self,
score_fn,
status_path=None,
delta_spacek=None,
delta_std=0.1,
top_n=10,
explore_pct=0.05,
rnd_pct=0.2,
min_pid_gain_pct=0.01,
max_blanks_pct=0.1,
scores_x_run=None,
n_jobs=None,
mp_ctx=None):
"""Run the parameter search optimization loop.
Iteratively evaluates score batches, selects the best points, explores
around them with deltas, and adds random exploration until the search
budget is exhausted or no improvement is found.
Args:
score_fn: Callable accepting keyword arguments from parameter dicts,
returning a numeric score (higher is better).
status_path: Optional file path to save intermediate status. Defaults
to None.
delta_spacek: Fraction or count of dimensions to perturb for local
search. Defaults to None.
delta_std: Standard deviation scale for Gaussian perturbation around
best points. Defaults to 0.1.
top_n: Number of top points to select each iteration. Defaults to 10.
explore_pct: Fraction of total space to explore as the maximum budget.
Defaults to 0.05.
rnd_pct: Fraction of next points to fill with random exploration.
Defaults to 0.2.
min_pid_gain_pct: Minimum relative gain to accept a new score for an
already-seen point ID. Defaults to 0.01.
max_blanks_pct: Fraction of max_explore allowed as consecutive
non-improving iterations before stopping. Defaults to 0.1.
scores_x_run: Number of scores to process per batch. Defaults to None
(uses _SCORES_X_RUN).
n_jobs: Number of parallel workers. Defaults to None.
mp_ctx: Multiprocessing context method name. Defaults to None.
"""
alog.debug0(f'{len(self.space)} parameters, {np.prod(self.space)} configurations')
if not self.pts:
self.pts.extend(self._randgen(top_n))
max_explore = int(np.prod(self.space) * explore_pct)
max_blanks = int(max_explore * max_blanks_pct)
while self.pts and len(self.processed) < max_explore and self.blanks < max_blanks:
alog.debug0(f'{len(self.pts)} points, {len(self.processed)} processed ' \
f'(max {max_explore}), {self.blanks}/{max_blanks} blanks')
self._fetch_scores(score_fn,
n_jobs=n_jobs,
mp_ctx=mp_ctx,
scores_x_run=scores_x_run,
status_path=status_path)
fsidx = self._select_top_n(top_n, min_pid_gain_pct)
# An empty selection means no pending point improved over its
# recorded score: the iteration counts as blanks instead of
# crashing on the missing best entry.
score = self.current_scores[fsidx[0]] if fsidx else None
if score is not None and (self.best_score is None or score > self.best_score):
self.best_score = score
self.best_idx = self.pts[fsidx[0]].idx
self.best_param = self._make_param(self.best_idx)
self.blanks = 0
alog.debug0(f'BestScore = {self.best_score:.5e}\tParam = {self.best_param}')
else:
self.blanks += len(self.pts)
alog.info(
f'Score not improved (current run top {score}, best {self.best_score})')
# Sample around best points ...
next_pts = []
for i in fsidx:
next_pts.extend(self._select_deltas(self.pts[i], delta_spacek, delta_std))
# And randomly add ones in search of better scores.
rnd_count = max(top_n, int(rnd_pct * len(next_pts)))
next_pts.extend(self._randgen(rnd_count))
self.pts = next_pts
self.current_scores, self.processed_scores = [], 0
def save_status(self, path):
"""Save the current Selector state to a pickle file.
Args:
path: Filesystem path to write the pickle file to.
"""
with gfs.open(path, mode='wb') as sfd:
pickle.dump(self, sfd, protocol=ut.pickle_proto())
@staticmethod
def load_status(path):
"""Load a Selector state from a pickle file.
Args:
path: Filesystem path to read the pickle file from.
Returns:
A Selector instance restored from the pickle file.
"""
with gfs.open(path, mode='rb') as sfd:
return pickle.load(sfd)
|