EvoTF¶
EvoTF_ES ¶
EvoTF_ES(population_size: int, solution: Solution, max_context_len: int = 100, model_config: dict = dict(num_layers=1, num_heads=1, num_latents=8, latent_dim=32, embed_dim=128, use_fitness_encoder=True, use_dist_encoder=True, use_crossd_encoder=False), solution_config: dict = dict(norm_diff_mean=True, norm_diff_mean_sq=True, diff_best=True, norm_diff_best=True, maximize=False), fitness_config: dict = dict(improved_best=True, z_score=True, norm_diff_best=True, norm_range=True, snes_weights=True, des_weights=True, w_decay=0.0, maximize=False), distrib_config: dict = dict(use_mean=False, use_sigma=False, use_evopaths=True, use_momentum=True, evopath_timescales=[0.9, 0.95, 0.99], momentum_timescales=[0.9, 0.95, 0.99], use_oai_grad=True), use_antithetic_sampling: bool = False, params: PyTree | None = None, params_path: str | None = None, fitness_shaping_fn: Callable = identity_fitness_shaping_fn, metrics_fn: Callable = metrics_fn)
Bases: DistributionBasedAlgorithm
Evolution Transformer (EvoTF).
Samples the population from a Gaussian with a per-dimension standard
deviation. In tell, the population, its fitness and the search distribution
are turned into features and appended to a sliding window over the last
max_context_len generations. A transformer reads that context and predicts
the update: the mean moves by lr_mean * std * prediction and the standard
deviation is multiplied by exp(lr_std / 2 * prediction).
The network weights come from params, from a checkpoint at params_path,
or, if both are None, from the checkpoint bundled with evosax. When a
checkpoint is loaded, the model and featurizers are rebuilt from the
configuration stored in it, and model_config, solution_config,
fitness_config and distrib_config are ignored. They only take effect
together with params.
Parameters:
-
population_size(int) –Number of candidate solutions sampled and evaluated per generation. Must be even if
use_antithetic_samplingis True. -
solution(Solution) –Example solution PyTree. Only its structure and leaf shapes are used: they set the number of search dimensions and how flat search vectors are unravelled into the PyTrees returned by
ask. -
max_context_len(int, default:100) –Number of most recent generations kept in the transformer context. Once it is full, the oldest generation is dropped.
-
model_config(dict, default:dict(num_layers=1, num_heads=1, num_latents=8, latent_dim=32, embed_dim=128, use_fitness_encoder=True, use_dist_encoder=True, use_crossd_encoder=False)) –Keyword arguments for the
EvoTransformernetwork.num_layers: Number of blocks in each encoder and in the transformer backbone.num_heads: Number of attention heads.num_latents: Number of learned latent vectors in the perceiver encoders.latent_dim: Size of each learned latent vector.embed_dim: Embedding size of the encoders and the backbone.use_fitness_encoder: If True, encode the fitness features and append the encoding to the solution encoding.use_dist_encoder: If True, encode the distribution features per dimension and append the encoding.use_crossd_encoder: If True, encode the distribution features across dimensions with a perceiver and append the encoding.
-
solution_config(dict, default:dict(norm_diff_mean=True, norm_diff_mean_sq=True, diff_best=True, norm_diff_best=True, maximize=False)) –Keyword arguments for the
SolutionFeaturizer. The sampled solutions themselves are always a feature.norm_diff_mean: If True, add(population - mean) / std.norm_diff_mean_sq: If True, add the square of(population - mean) / std.diff_best: If True, add the difference to the best solution so far.norm_diff_best: If True, add the difference to the best solution so far, rescaled per dimension to[-0.5, 0.5].maximize: If True, the best solution so far is the one with the highest fitness instead of the lowest.
-
fitness_config(dict, default:dict(improved_best=True, z_score=True, norm_diff_best=True, norm_range=True, snes_weights=True, des_weights=True, w_decay=0.0, maximize=False)) –Keyword arguments for the
FitnessFeaturizer. The centered ranks of the fitness are always a feature.improved_best: If True, add an indicator of whether a member improved on the best fitness so far.z_score: If True, add the standardized fitness.norm_diff_best: If True, add the difference to the best fitness so far, divided by its range over the population and clipped to[-1, 1].norm_range: If True, add the fitness rescaled to[-0.5, 0.5].snes_weights: If True, add the rank-based weights ofevosax.algorithms.distribution_based.xnes.get_weights.des_weights: If True, add the rank-based weights ofevosax.algorithms.distribution_based.discovered_es.get_weights.w_decay: If nonzero, add the mean squared entry of each solution. The value itself is not used as a coefficient.maximize: If True, negate the fitness before computing the features.
-
distrib_config(dict, default:dict(use_mean=False, use_sigma=False, use_evopaths=True, use_momentum=True, evopath_timescales=[0.9, 0.95, 0.99], momentum_timescales=[0.9, 0.95, 0.99], use_oai_grad=True)) –Keyword arguments for the
DistributionFeaturizer. Rank weighted gradient estimates for the mean and the standard deviation are always features.use_mean: If True, add the mean.use_sigma: If True, add the standard deviation.use_evopaths: If True, add exponential moving averages of the two gradient estimates, one per entry ofevopath_timescales.use_momentum: If True, add exponential moving averages of the change in mean and standard deviation, one per entry ofmomentum_timescales.evopath_timescales: Update rateslrof the evolution path averages,path = (1 - lr) * path + lr * value.momentum_timescales: Update rates of the momentum averages, used in the same way.use_oai_grad: If True, add a gradient estimate for the mean computed from centered fitness ranks.
-
use_antithetic_sampling(bool, default:False) –If True, sample perturbations in mirrored pairs
(z, -z). -
params(PyTree | None, default:None) –Network weights for
EvoTransformer. IfNone, the weights are loaded fromparams_pathor from the bundled checkpoint. -
params_path(str | None, default:None) –Path to a pickled checkpoint holding the entries
net_params,model_config,solution_config,fitness_configanddistrib_config. Only used ifparamsisNone. -
fitness_shaping_fn(Callable, default:identity_fitness_shaping_fn) –Function
(population, fitness, state, params) -> shaped_fitnessapplied to the raw fitness intellbefore the algorithm update. Seeevosax.core.fitness_shaping. -
metrics_fn(Callable, default:metrics_fn) –Function
(key, population, fitness, state, params) -> metricsevaluated intell; its output is returned alongside the new state.
Warning
Pickle loading can execute arbitrary code. Only pass trusted files to
params_path.
References
default_params
property
¶
Default hyperparameters of the algorithm.
Returns a Params dataclass. Use params.replace(...) to override
individual hyperparameters before passing it to init, ask and tell.
init ¶
Initialize the algorithm state from an initial mean.
Parameters:
-
key(Array) –PRNG key.
-
mean(Solution) –Initial mean of the search distribution, a PyTree with the structure of
solution. -
params(Params) –Algorithm hyperparameters, e.g.
default_params.
Returns:
-
State–The initial algorithm state.
ask ¶
Ask for a population of candidate solutions to evaluate.
Parameters:
-
key(Array) –PRNG key.
-
state(Any) –Current algorithm state.
-
params(Any) –Algorithm hyperparameters.
Returns:
-
tuple[Population, Any]–A tuple
(population, state).populationis a PyTree with the structure ofsolutionand a leading axis of sizepopulation_size.
tell ¶
tell(key: Array, population: Population, fitness: Fitness, state: Any, params: Any) -> tuple[Any, Metrics]
Update the algorithm state with the fitness of a population.
Parameters:
-
key(Array) –PRNG key.
-
population(Population) –Population returned by
ask. -
fitness(Fitness) –Fitness of each population member, shape
(population_size,). Lower is better. NaN values mark failed evaluations and never become the best solution. -
state(Any) –Algorithm state returned by
ask. -
params(Any) –Algorithm hyperparameters.
Returns:
-
tuple[Any, Metrics]–A tuple
(state, metrics)with the updated state and the output ofmetrics_fn.
get_mean ¶
Return the mean of the search distribution as a solution PyTree.