API Reference
The public surface of the neat_optim package.
The stable v2 API is intentionally narrow, so optimizer behavior is easier to inspect, serialize, benchmark, and test.
| Symbol | Purpose |
|---|---|
neat_optim.NEAT | Keras optimizer for model.compile |
neat_optim.NEATDiagnosticsCallback | Epoch-level diagnostics in History and TensorBoard |
neat_optim.NEATConfig | Reference engine configuration |
neat_optim.PlayerNEATConfig | Configuration for per-player gradient updates |
neat_optim.ArrayState | Per-parameter reference engine state |
neat_optim.engine.functional.neat_step | Framework-agnostic one-step update |
neat_optim.engine.multiplayer.neat_player_step | Player-aware one-step update with conflict and sparsity metrics |
neat_optim.training.player_train_step | TensorFlow helper for custom loops with per-example losses |
neat_optim.TorchNEAT | Optional PyTorch optimizer adapter (requires the torch extra) |
neat_optim.compact_dense_model | Remove weak hidden units from a Sequential Dense model |
NEAT
Keras 3 optimizer for model.compile. Registered as a Keras serializable (package="neat_optim"), so it survives model.save() / keras.models.load_model(). See Keras and Configuration.
Methods beyond the standard Keras optimizer API:
diagnostic_snapshot() -> dict[str, float]: running means since the last reset.reset_diagnostics(): clear the running means, for example at the start of each epoch.
NEATDiagnosticsCallback
NEATDiagnosticsCallback(
log_dir=None, # write TensorBoard scalars here (needs tensorboard)
reset_each_epoch=True, # reset optimizer diagnostics after logging each epoch
prefix="neat", # metric name prefix in History
report_path=None, # optional .json or .html training report
)Adds neat_* metrics to History at the end of every epoch.
NEATConfig
Reference engine configuration. It accepts the same algorithm arguments as the Keras optimizer and validates them. See Configuration.
ArrayState
Per-parameter state for the reference engine. Create one with ArrayState.zeros_like(param).
neat_step
neat_step(param, grad, state, config) -> StepResultFramework-agnostic one-step update for NumPy arrays. It uses the native C++ core when available and falls back to the reference implementation otherwise. StepResult exposes .param, .state and .metrics.
import numpy as np
from neat_optim import NEATConfig
from neat_optim.engine.functional import neat_step
from neat_optim.state import ArrayState
param = np.array([1.0, -2.0, 0.5], dtype=np.float32)
grad = np.array([0.5, -0.25, 0.1], dtype=np.float32)
state = ArrayState.zeros_like(param)
config = NEATConfig(learning_rate=0.1, alpha=0.25, beta=0.9)
for _ in range(10):
result = neat_step(param, grad, state, config)
param, state = result.param, result.state
print(result.metrics)
# conflict_ratio and nce_norm stay 0.0 here: a constant gradient never
# conflicts with its own momentum, so the correction stays off.PlayerNEATConfig
Configuration for explicit per-player gradient updates, including sparsity_l1 and prune_threshold. See Player-Aware NEAT.
neat_player_step
neat_player_step(param, player_grads, state, config) -> PlayerStepResultPlayer-aware one-step update. Each slice along the leading dimension of player_grads is one player; by default, each player's opponent is the mean of the others.
player_train_step
from neat_optim.training import create_player_states, player_train_step
states = create_player_states(model)
result = player_train_step(model, x, y, loss_fn, states, config)TensorFlow helper for custom loops with per-example losses (reduction="none"). Requires the TensorFlow backend.
TorchNEAT
Optional PyTorch optimizer adapter, used by the PyTorch benchmark suite. Install the torch extra first.
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,
)It supports normal PyTorch parameter groups, state_dict() checkpointing, closures, and the same algorithm settings validated by NEATConfig. diagnostic_interval (default 1) controls how often diagnostics are sampled. Larger values reduce accelerator synchronization without changing the parameter updates.
Model compaction
compact_dense_model(model, *, unit_threshold=0.0)
search_compact_dense_model(model, *, thresholds, scorer, min_score=None)Return a smaller Sequential Dense model by removing weak hidden units, with a report of what was removed. The search variant tries several thresholds and keeps the smallest model that still meets min_score. These pair naturally with the sparsity options in player-aware mode.