Player-Aware NEAT
Per-example and per-task gradients in a custom TensorFlow training loop.
In player-aware mode, each element along the leading dimension of player_grads is treated as a player. By default, a player's opponent signal is the mean of the other players. Optional L1 shrinkage and hard pruning push the model toward sparser, lighter weights.
TensorFlow backend required
player_train_step computes per-example gradients with tf.GradientTape, so it needs KERAS_BACKEND=tensorflow.
Custom TensorFlow loop
import keras
from neat_optim import PlayerNEATConfig
from neat_optim.training import create_player_states, player_train_step
model = keras.Sequential([
keras.layers.Input((32,)),
keras.layers.Dense(64, activation="relu"),
keras.layers.Dense(10),
])
loss_fn = keras.losses.SparseCategoricalCrossentropy(
from_logits=True,
reduction="none",
)
states = create_player_states(model)
config = PlayerNEATConfig(
learning_rate=1e-2,
alpha=0.25,
beta=0.9,
sparsity_l1=1e-4, # soft sparsity pressure
prune_threshold=1e-3, # hard-zero small weights
)
for x_batch, y_batch in dataset:
result = player_train_step(model, x_batch, y_batch, loss_fn, states, config)
states = result.states
print(f"loss={result.loss:.4f}")Per-example losses
Set reduction="none" on the loss so that each example's gradient is computed separately.
Related
PlayerNEATConfig: configuration for explicit per-player gradient updates.neat_player_step: player-aware one-step API with conflict and sparsity metrics.- Algorithm: the math behind the correction.