Keras optimizer implementation of NEAT.
Use this when training a Keras model through model.compile(...).
Important constructor arguments:
learning_rate: step sizealpha: scale of the Nash correction estimatebeta: momentum mixing factornce_mode: one ofprojection,cosine, oroffweight_decay: weight decay coefficientdecouple_weight_decay: whether weight decay is decoupled from the momentum updatence_clip_ratio: clips the correction norm relative to the gradient normopponent_source:momentum,previous_gradient, orgradient_emaopponent_ema_decay: decay factor used bygradient_emacorrection_warmup_steps: steps to wait before applying the correctionconflict_threshold: minimum conflict ratio required before correctionadaptive_correction: scales the correction using conflict and opponent reliabilityadaptive_preconditioning: enables the diagonal second-moment preconditionerprecondition_nce: computes the correction in the preconditioned spaceupdate_mode:momentumorlion;lionapplies a sign-based final updateadaptive_alpha: makesalphadynamic from conflict, gradient noise, and alignment EMAsadaptive_alpha_min/adaptive_alpha_max: clamps the dynamic alphagradient_noise_decay: EMA decay for gradient-noise and alignment signalsgradient_centralization: subtracts the feature mean from matrix-like gradients before the optimizer updatenesterov: uses a Nesterov-style lookahead momentum updatelookahead_k/lookahead_alpha: optional Lookahead slow-weight syncsparsity_l1: optional soft-threshold shrinkage for sparse weightsprune_threshold: optional hard pruning threshold
Diagnostics:
diagnostic_snapshot(): returns aggregate optimizer diagnostics gathered during trainingreset_diagnostics(): clears those running aggregates
Snapshot keys:
mean_conflict_ratiomean_correction_ratiomean_update_alignmentmean_opponent_normcorrection_active_fractionmean_effective_alphamean_gradient_noise
Optional PyTorch optimizer adapter used by the modern benchmark suite. Install
the torch extra before importing it.
from neat_optim import TorchNEAT
optimizer = TorchNEAT(
model.parameters(),
lr=1e-3,
opponent_source="previous_gradient",
adaptive_alpha=True,
adaptive_preconditioning=True,
diagnostic_interval=10,
)diagnostic_interval controls how often parameter-level diagnostic reductions
are sampled. It defaults to 1 for complete measurement. Larger values reduce
accelerator synchronization overhead without changing parameter updates. The
adapter supports normal PyTorch parameter groups, state_dict() checkpointing,
closures, and the same algorithm settings validated by NEATConfig.
Keras callback for optimizer diagnostics.
Use this when you want epoch-level diagnostics in memory or TensorBoard:
from neat_optim import NEATDiagnosticsCallback
callbacks = [
NEATDiagnosticsCallback(log_dir="runs/neat", reset_each_epoch=True),
]
model.fit(x_train, y_train, callbacks=callbacks)If log_dir is set, TensorFlow must be installed because the callback writes
with tf.summary. Without log_dir, the callback stores snapshots in
callback.history.
Immutable configuration object for the reference and functional engines.
Use this when working with neat_step(...) directly or when serializing
optimizer configuration outside Keras.
Per-parameter state container for the NumPy and functional engines.
Fields:
momentumnceprevious_gradientgradient_emasecond_momentslow_paramconflict_emagradient_noise_emaalignment_emastep
Create a zero-initialized state with ArrayState.zeros_like(array).
Configuration object for explicit per-player updates.
Additional arguments:
opponent_mode:mean_excluding_selforbatch_meanplayer_reduction:meanorsumsparsity_l1: soft-threshold sparsity pressureprune_threshold: hard-prune threshold
Explicit player-aware one-step API.
Inputs:
- parameter array
player_gradswith shape(num_players, *param.shape)ArrayStatePlayerNEATConfig
Returns a PlayerStepResult containing:
- updated parameter tensor
- updated state
- player-conflict and sparsity diagnostics
TensorFlow helper for custom training loops with per-example gradients.
Use this when you want each training example in a batch to act as a player. This requires a loss function that returns unreduced per-example losses.
Framework-agnostic one-step API for NEAT updates.
Inputs:
- parameter array
- gradient array
ArrayStateNEATConfig
Returns a StepResult containing:
- updated parameter tensor
- updated state
- scalar metrics for the step, including conflict ratio, correction ratio, update alignment, and opponent norm
NEAT.get_config()supports standard Keras optimizer serialization.NEATConfig.as_dict()returns a plain serializable configuration mapping.PlayerNEATConfig.as_dict()does the same for the player-aware mode.ArrayStateis intended for the reference NumPy engine and test fixtures, not for framework checkpoint interchange.
ConfigurationError: invalid optimizer configurationNativeCoreUnavailableError: native engine requested but not available