Skip to main content

No project description provided

Project description

Mapping Networks

License Python Version PyTorch Version Build Status Documentation Status arXiv

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 MLPMapper so they stay frozen and are excluded from DDP/optimizer updates.
  • Additive Modulation: Implemented in AdditiveModulation representing $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).

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.compile in 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:

👉 mapping-networks.readthedocs.io

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

mapping_networks-0.1.1.tar.gz (42.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mapping_networks-0.1.1-py3-none-any.whl (65.0 kB view details)

Uploaded Python 3

File details

Details for the file mapping_networks-0.1.1.tar.gz.

File metadata

  • Download URL: mapping_networks-0.1.1.tar.gz
  • Upload date:
  • Size: 42.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mapping_networks-0.1.1.tar.gz
Algorithm Hash digest
SHA256 411d0a181ca8b0d37f2eef1261b2a062d3f444b4167a2758d2188d7e39d5951c
MD5 8f3a7a0c0e7dcc81fd3cae288b3e8426
BLAKE2b-256 c2d956c7cdf64d102ffccb1981721cf4bd67f71acaaaf8588fe3abf8418f3f32

See more details on using hashes here.

Provenance

The following attestation bundles were made for mapping_networks-0.1.1.tar.gz:

Publisher: ci.yml on arjunmnath/mapping_networks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mapping_networks-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mapping_networks-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f923e9745844ba3b1337fe0e5376204d5e56a6a05ca1f79b9cfdb4c632064144
MD5 d8cb7046aa8d4d029b2836e4c058a8d3
BLAKE2b-256 74784a66364ef635785984c365a21587613e8d933eae6c36ff42a321495f3174

See more details on using hashes here.

Provenance

The following attestation bundles were made for mapping_networks-0.1.1-py3-none-any.whl:

Publisher: ci.yml on arjunmnath/mapping_networks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page