Skip to main content

Module param_reshaper

Module param_reshaper 

Source
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:

  • flatten walks a module’s float leaves in a deterministic order and concatenates them into a 1-D tensor.
  • unflatten clones 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 — see unflatten’s # Aliasing note.

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§

ModuleReshaper
A ParamReshaper backed by a cloned template module.

Traits§

ParamReshaper
Bridges a Burn Module and a flat Tensor<B, 1> parameter vector.