MappingModel guide¶
Status: Implemented in Milestone 5. Requires generators (Milestone 4) and the parameter runtime (Milestone 1).
MappingModel is the user-facing entry point for training a PyTorch model
through a low-dimensional parameter manifold. It composes a frozen target, a
parameter generation strategy, and exposes a clean forward interface whose
output carries everything downstream losses need.
Quick start¶
import torch
from torch import nn
from mapping_networks import MappingModel
target = nn.Sequential(nn.Linear(784, 128), nn.ReLU(), nn.Linear(128, 10))
model = MappingModel(target, latent_dim=64, strategy="layerwise")
inputs = torch.randn(32, 784)
result = model(inputs)
# result.predictions — target output, shape (32, 10)
# result.generated_parameters — ParameterTree used for this forward
# result.latent_vectors — dict[str, Tensor] of named latent vectors
Constructor¶
MappingModel(
target_model: nn.Module,
latent_dim: int = 256,
strategy: str | GenerationStrategy = "layerwise",
*,
mapper_factory: MapperFactory | None = None,
)
Parameter |
Description |
|---|---|
|
Any |
|
Dimensionality of each trainable latent vector. Must be positive. |
|
|
|
Optional |
Strategies¶
String |
Strategy |
Description |
|---|---|---|
|
|
One latent per target module that owns parameters. Memory-efficient default. |
|
|
Single latent vector for the entire target. Paper baseline, memory-heavy. |
Pass a GenerationStrategy instance for custom grouping:
from mapping_networks import GroupedStrategy
groups = {"encoder": ("enc.weight", "enc.bias"), "decoder": ("dec.weight", "dec.bias")}
model = MappingModel(target, latent_dim=32, strategy=GroupedStrategy(groups))
ForwardResult¶
model(inputs) returns a ForwardResult dataclass:
@dataclass
class ForwardResult:
predictions: Any # target model output
generated_parameters: ParameterTree # for loss computation
latent_vectors: dict[str, Tensor] # for stability/alignment losses
The result carries autograd-live tensors. Call .predictions.sum().backward()
and gradients flow to the latent vectors only — the target is never modified.
Diagnostics¶
model.trainable_parameter_count # number of trainable latent parameters
model.target_parameter_count # total target parameters
model.compression_ratio # target / trainable ratio
Keyword arguments¶
All positional and keyword arguments to model(...) are forwarded to the
target model:
class CustomTarget(nn.Module):
def forward(self, x, *, temperature=1.0):
...
model = MappingModel(CustomTarget(), latent_dim=32)
result = model(inputs, temperature=0.5)
Supported targets¶
Any nn.Module with positional and keyword inputs is supported, including:
nn.Linear,nn.Conv2d,nn.LSTMnn.Sequential, nested modulesResidual architectures with skip connections
Not supported (will raise UnsupportedTargetModelError):
Tied/shared parameters (e.g., weight tying between encoder/decoder)
Parametrized modules (
torch.nn.utils.parametrize)
API reference¶
Method / Property |
Returns |
Description |
|---|---|---|
|
|
Generate parameters and run target |
|
|
Count of requires_grad parameters |
|
|
Total target parameter elements |
|
|
Target / trainable ratio |
|
|
The underlying generator module |
|
|
The frozen target wrapper |