Configuration and registries¶
Status: Implemented in Milestone 7.
This guide describes how to configure mapping networks using typed Pydantic models, load configuration files, and extend the toolkit with custom component registries.
Configuration models¶
The configuration system uses Pydantic (v2) to validate parameters, forbid unknown keys, and support robust serialization.
MappingConfig¶
MappingConfig is the top-level configuration that groups generator and mapper parameters.
from mapping_networks import MappingConfig, GeneratorConfig, MapperConfig
config = MappingConfig(
generator=GeneratorConfig(
strategy="layerwise",
latent_dim=128,
),
mapper=MapperConfig(
type="mlp",
hidden_dims=(64, 64),
modulation="additive",
alpha=0.01,
),
)
Config Class |
Attributes |
Default |
Description |
|---|---|---|---|
|
|
|
Registry key for the mapper. |
|
|
Hidden layer dimensions. |
|
|
|
Registry key for the modulation. |
|
|
|
Additive modulation strength. |
|
|
|
Hidden dimension for residual MLP. |
|
|
|
Number of residual blocks. |
|
|
|
|
Strategy: |
|
|
Latent vector size. |
|
|
|
Mapping of group name to list of parameters. |
|
|
|
Upper bound for SLVT projection matrices. |
|
|
|
|
Task loss configuration. |
|
|
Weight for stability loss. |
|
|
|
Weight for smoothness loss. |
|
|
|
Weight for alignment loss. |
|
|
|
Perturbation scale. |
|
|
|
Number of noise samples. |
|
|
|
|
|
|
|
Number of projection vectors. |
|
|
|
Whether coefficients are trainable. |
|
|
|
Enable stability loss component. |
|
|
|
Enable smoothness loss component. |
|
|
|
Enable alignment loss component. |
|
|
|
|
Maximum number of training epochs. |
|
|
Optimizer name: |
|
|
|
Base learning rate. |
|
|
|
Weight decay factor. |
|
|
|
Scheduler name: |
|
|
|
Arguments passed to the scheduler. |
|
|
|
Max norm for gradient clipping. |
|
|
|
Max value for gradient clipping. |
|
|
|
Number of batches for gradient accumulation. |
|
|
|
Enable Automatic Mixed Precision (AMP). |
|
|
|
Target device ( |
|
|
|
Random seed for reproducibility. |
Loading YAML configuration files¶
Use load_config to load configurations from a YAML file or a Python dictionary.
from mapping_networks import load_config
# Load from file
config = load_config("configs/layerwise_adam.yaml")
# Load from dictionary
config = load_config({
"generator": {"latent_dim": 64},
"mapper": {"type": "residual_mlp", "depth": 3}
})
Validation errors¶
When Pydantic encounters type mismatches or unknown keys (due to extra="forbid"), it raises a ValidationError:
from pydantic import ValidationError
from mapping_networks import load_config
try:
load_config({"generator": {"latent_dim": "invalid"}})
except ValidationError as e:
print(e) # Clear explanation of path and cause
Component registries¶
The package uses a generic Registry class to manage pluggable components. Registries map string keys to component classes.
from mapping_networks import (
MAPPER_REGISTRY,
MODULATION_REGISTRY,
LOSS_REGISTRY,
GENERATOR_REGISTRY,
)
print(MAPPER_REGISTRY.available())
# Output: ('mlp', 'residual_mlp')
Registries API¶
register(key, cls): Register a custom component class. RaisesValueErrorif the key is already taken.get(key): Retrieve the registered class. RaisesKeyErrorif the key does not exist, listing available keys.available(): Returns a sorted tuple of all registered keys.
Adding custom plugins¶
You can register custom implementations to extend the toolkit. Once registered, they can be referenced by string names in configurations.
from torch import nn
from mapping_networks import BaseMapper, MAPPER_REGISTRY, MappingModel
class MyCustomMapper(BaseMapper):
def __init__(self, latent_dim, output_dim, modulation):
super().__init__(latent_dim, output_dim, modulation)
# Initialization logic
def forward(self, latent):
# Mapping logic
...
# Register custom component
MAPPER_REGISTRY.register("custom_mapper", MyCustomMapper)
# Instantiate config using custom component
config = load_config({
"mapper": {"type": "custom_mapper"}
})