Expand description
Bridge between a Burn Module and a flat parameter vector.
Weight-only neuroevolution evolves a Tensor<B, 2> population of shape
(pop_size, num_params) and must, per population member, splat one flat
parameter row back into a concrete network to score it. The
ParamReshaper trait captures that bidirectional bridge:
flattenwalks a module’s float leaves in a deterministic order and concatenates them into a 1-D tensor.unflattenclones a template module and replaces each float leaf with the matching slice of a flat tensor, in the same order. The reconstructed leaves are views into the flat tensor’s storage, not copies — seeunflatten’s# Aliasingnote.
ModuleReshaper is the concrete implementation. It relies on the fact
that Burn’s #[derive(Module)] generates visit/map traversals that
visit fields in declaration order, recursively — so flatten (a
ModuleVisitor) and unflatten (a ModuleMapper) agree leaf-for-leaf.
§Non-trainable module state
Burn’s visit/map traversal touches every float leaf reachable through
the Module tree, including non-Param running statistics such as a
burn::nn::BatchNorm layer’s running mean/variance (they are wrapped in a
RunningState, which is itself a Module that forwards to visit_float /
map_float). The proof-of-concept in this module’s test submodule verifies this
empirically. The practical consequence: if an evolved network contains
BatchNorm, its running statistics are flattened, perturbed by evolution,
and re-splatted like any weight. For fixed-topology MLP policies (the v1
target) this is moot — there are no running buffers. Callers that evolve
batch-normalized networks should reset running statistics after
unflatten.
§Gradient isolation
This module is generic over B: Backend, not AutodiffBackend.
Tensors produced by unflatten do not require
gradients. Callers holding an autodiff module call .valid() before
constructing a ModuleReshaper, so the constraint is enforced at the
type level rather than by convention.
Structs§
- Module
Reshaper - A
ParamReshaperbacked by a cloned template module.
Traits§
- Param
Reshaper - Bridges a Burn
Moduleand a flatTensor<B, 1>parameter vector.