Featurizers¶
Under development
The featurizer system is being actively developed and its API may still change. This page documents only the flat featurizer — a single flat vector of scalar features per state. Richer, structured observations (multi-field bundles, and object-token representations for attention / pointer-network policies) already exist in the package but are still evolving and are intentionally not documented here yet.
A featurizer defines the numeric state representation fed to a neural
network — the encoding of a state that an NNAgent,
PPO, or DCL policy consumes. It is declared separately
from the MDP, so the same MDP can be paired with different representations
without changing the model.
The flat featurizer¶
The simplest featurizer writes a flat vector of scalar features for each
state through a single GlobalStateWriter:
from dataclasses import dataclass
from dynaplex import Featurizer, featurizer, GlobalStateWriter
@featurizer
@dataclass(slots=True)
class AirplaneFeaturizer(Featurizer):
mdp: AirplaneMDP
v: GlobalStateWriter
def write_features(self, state: State) -> None:
# Must be valid DynaML. Each append adds one scalar to the flat vector,
# in a fixed order.
self.v.append(state.remaining_days / self.mdp.initial_days)
self.v.append(state.remaining_seats / self.mdp.initial_seats)
self.v.append(state.price_offered_per_seat / self.mdp.average_price)
The essentials:
- Subclass
Featurizerand decorate the@dataclass(slots=True)with@featurizer. The base class carries no fields; it lets a type checker recognise the class as a featurizer (thefeatures=argument istype[Featurizer]) and see the methods the decorator adds — the same pattern asTrajectoryContextand@trajectory_context. - Declare an
mdpfield (the MDP this representation is for) and one writer field. For the flat featurizer that writer is aGlobalStateWriter. write_features(self, state)fills one row byappend-ing scalars in a fixed order. Its body must be valid DynaML — the same restricted subset used for the MDP's own methods (see the language reference).@featurizerderives aHolderclass and synthesizes the field-walks (install / reset / finish) and aspec()describing the observation.spec()is auto-sized by counting theappends on a probe state, so a flat featurizer needs no hand-written spec. The probe state is the initial state walked (with a fixed seed) to its first decision; the walk gives up after 1000 events. An MDP whose first decision lies further out raises the limit for its featurizer with@featurizer(probe_steps=N), which every user of the featurizer (the synthesizedspec(),check_mdp,rehearse, DCL) honours — or starts from an initial state closer to a decision, which is usually the better model anyway.
Using a featurizer¶
A featurizer is supplied to the training and agent APIs as the observation representation. See PPO and Deep Controlled Learning for how a featurizer is wired into training, and Comparing policies for evaluating the resulting neural-network policy.
Checking your featurizer statically¶
assert_featurizer_for_mdp(mdp, AirplaneFeaturizer) does nothing at runtime, but
it lets pyright verify that the featurizer
matches the MDP's state type and the expected featurizer interface — catching
mistakes before anything is compiled.