No project description provided
Project description
Mapping Networks
mapping_networks is a model-agnostic, production-oriented PyTorch package for training target models through low-dimensional parameter manifolds. It is inspired by the paper Mapping Networks. The library decouples model architecture from parameter representation, allowing you to optimize neural networks by updating compact, trainable latent coordinates instead of mutating the target module's parameters.
Installation
Install mapping_networks from your local workspace:
pip install mapping_networks
Or add it using Poetry:
poetry add mapping_networks
Quickstart
Train a standard PyTorch model through a low-dimensional layer-wise latent representation:
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset
from mapping_networks import MappingModel, MappingLoss, ClassificationLoss, MappingTrainer
# 1. Create target model and training data
target = nn.Sequential(nn.Linear(10, 16), nn.ReLU(), nn.Linear(16, 2))
data = TensorDataset(torch.randn(100, 10), torch.randint(0, 2, (100,)))
loader = DataLoader(data, batch_size=16)
# 2. Wrap target model with low-dimensional mapping strategy
model = MappingModel(
target_model=target,
latent_dim=32,
strategy="layerwise"
)
# 3. Configure composite loss and trainer
loss_fn = MappingLoss(task_loss=ClassificationLoss())
trainer = MappingTrainer(
model=model,
train_loader=loader,
loss_fn=loss_fn,
learning_rate=1e-3,
)
# 4. Train the latent parameters
trainer.fit(epochs=5)
Core Concepts
- Latent Manifold: Trainable coordinates $z$ which undergo transformation to map to target parameter space.
- BaseMapper: Projects $z$ to flat generated parameter descriptors (e.g., using fixed, orthogonal projections).
- BaseModulation: Integrates generated descriptors back into target parameters (e.g., additive $W_{ij} \leftarrow W_{ij} + \alpha z_i$, or affine).
- Generation Strategy: Defines the mapping scope.
"slvt"(Single Latent Vector Training) projects the entire model from a single global latent vector."layerwise"constructs independent smaller latent coordinates per layer."grouped"allows custom parameter subdivision.
- MappingLoss: A composite loss that balances task loss (classification/regression) with stability, smoothness, and cosine alignment regularization components.
Paper Correspondence
The code maps directly to the concepts defined in the paper:
- Fixed Projection Matrices: Registered as PyTorch buffers inside
MLPMapperso they stay frozen and are excluded from DDP/optimizer updates. - Additive Modulation: Implemented in
AdditiveModulationrepresenting $w_{ij} \leftarrow w_{ij} + \alpha \cdot z_i$. - Regularization Terms: Fully implemented in
MappingLoss:- Stability Loss ($L_{\text{stability}}$): penalizes changes in output predictions when adding small noise perturbation to the latent vector (
StabilityLoss). - Smoothness Loss ($L_{\text{smoothness}}$): penalizes the Jacobian norm of the mapper to enforce a smooth manifold (
SmoothnessLoss). - Alignment Loss ($L_{\text{alignment}}$): maximizes alignment via cosine distance between latent vectors and weight summaries (
AlignmentLoss).
- Stability Loss ($L_{\text{stability}}$): penalizes changes in output predictions when adding small noise perturbation to the latent vector (
Limitations
- Stateless Execution: The target model is executed via
torch.func.functional_call. It is never mutated, but any custom operations inside the target model must support stateless calls. - Stateful Buffers (e.g., BatchNorm): Persistent target buffers (like running mean/variance) are isolated. During training, running stats do not mutate.
- Parameter Tying: Target models with tied parameters (e.g., shared embeddings in language models) are not supported out of the box and will fail validation explicitly.
- torch.compile: Compatible with
torch.compilein non-fullgraph mode; fullgraph compilation may fail due to dynamic shapes or functional execution limits depending on the target model.
Documentation
The official documentation is hosted on Read the Docs:
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mapping_networks-0.2.1.tar.gz.
File metadata
- Download URL: mapping_networks-0.2.1.tar.gz
- Upload date:
- Size: 48.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a6def4e5d12b8965d51fd5447140da6e02ed077e79fa219e559d5efb5d29bbe
|
|
| MD5 |
f6c7b25b0f39b35ca8561dba7ef23b46
|
|
| BLAKE2b-256 |
c83fdc2a02ad8e8df5749d03e2669f4800f4af51dc678b7921cd80a51e1ae3a0
|
Provenance
The following attestation bundles were made for mapping_networks-0.2.1.tar.gz:
Publisher:
ci.yml on arjunmnath/mapping_networks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mapping_networks-0.2.1.tar.gz -
Subject digest:
1a6def4e5d12b8965d51fd5447140da6e02ed077e79fa219e559d5efb5d29bbe - Sigstore transparency entry: 1994880114
- Sigstore integration time:
-
Permalink:
arjunmnath/mapping_networks@3a77051365409244827f5826d614de8526ff6664 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/arjunmnath
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@3a77051365409244827f5826d614de8526ff6664 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mapping_networks-0.2.1-py3-none-any.whl.
File metadata
- Download URL: mapping_networks-0.2.1-py3-none-any.whl
- Upload date:
- Size: 75.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d44df9c888a5a19bb6a099a831a5a937d40bf36943936938053f3a6bc833db35
|
|
| MD5 |
3c2874d1dbfebdfc3897e96bdbfbeb84
|
|
| BLAKE2b-256 |
397d5980371eae08ae5338809d91a30533f51ec24c053a729cf668168a129f50
|
Provenance
The following attestation bundles were made for mapping_networks-0.2.1-py3-none-any.whl:
Publisher:
ci.yml on arjunmnath/mapping_networks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mapping_networks-0.2.1-py3-none-any.whl -
Subject digest:
d44df9c888a5a19bb6a099a831a5a937d40bf36943936938053f3a6bc833db35 - Sigstore transparency entry: 1994880207
- Sigstore integration time:
-
Permalink:
arjunmnath/mapping_networks@3a77051365409244827f5826d614de8526ff6664 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/arjunmnath
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@3a77051365409244827f5826d614de8526ff6664 -
Trigger Event:
release
-
Statement type: