Reference library for the matching principle: estimate Sigma_task (D1-D7) and matched PMH penalties
Project description
matching-pmh
Independent library for the matching principle: estimate $\Sigma_{\mathrm{task}}$ (Lemmas D1–D7), run matched PMH penalties, save/load artifacts, and wire into training loops.
Paper: The Matching Principle (separate repository). v0.2 adds typed configs, artifacts, pre-flight eigengap, PMHLoss, and collect_features.
Install
cd matching-pmh
pip install -e ".[dev]"
pytest
Quick start (v0.2)
import torch
from pmh import (
SigmaTaskConfig,
PMHConfig,
PMHLoss,
estimate_from_config,
collect_features,
)
# 1) Estimate + diagnostics
cfg = SigmaTaskConfig.for_domain(rank=64)
artifact = estimate_from_config(cfg, source_feats, target_feats)
print(artifact.preflight, artifact.eigengap) # pass | marginal | fail
# 2) Save for another job / machine
artifact.save("checkpoints/style_sigma")
# 3) Train
pmh = PMHLoss(artifact, PMHConfig(weight=0.3, cap_ratio=0.3, warmup_epochs=2))
h = backbone(x)
task_loss = ...
total, pmh_term = pmh.capped_total(task_loss, h)
Legacy one-liner (still supported)
from pmh import estimate_sigma_task, pmh_penalty_on_rep
sigma = estimate_sigma_task(src, tgt, method="D4", rank=64)
pen = pmh_penalty_on_rep(h, sigma)
Load a saved estimate
from pmh import SigmaTaskEstimate, PMHLoss
artifact = SigmaTaskEstimate.load("checkpoints/style_sigma.pt")
pmh = PMHLoss(artifact)
Examples
| Script | What it shows |
|---|---|
examples/01_domain_shift_d4.py |
collect_features + D4 + PMHLoss training |
examples/02_save_load_artifact.py |
.pt + .json artifact I/O |
examples/03_compositional_d5.py |
D5 coordinate-block $\Sigma$ |
examples/04_falsification_controls.py |
matched / wrong-W / isotropic modes |
examples/05_yaml_config.py |
JSON job dict → configs |
examples/minimal_loop.py |
Short end-to-end loop |
examples/06_office31_sklearn.py |
D1 + MatchedSubspaceProjector + logistic |
examples/07_vision_multilayer.py |
MultiLayerPMHLoss + per-layer Gram noise |
API map
| Goal | API |
|---|---|
| Pick estimator | SigmaTaskConfig.for_domain(), .for_isotropic(), … |
| Estimate | estimate_from_config(cfg, ...) → SigmaTaskEstimate |
| Pre-flight | artifact.preflight, preflight_eigengap(cov, rank) |
| Train | PMHLoss(artifact, PMHConfig(...)) |
| Controls | PMHLoss(..., mode="wrong_w"), signal_W_projector |
| Data hook | collect_features(encoder, loader) |
Estimators (method=)
| Method | Lemma | Config helper |
|---|---|---|
| D1 | Subspace SVD | for_subspace(rank=) |
| D2 | Isotropic | for_isotropic(dim, noise_level) |
| D3 | Aug modes | for_augmentation() |
| D4 | Domain Gram | for_domain(rank=) |
| D5 | Compositional | for_compositional(indices) |
| D6 | Temporal | for_temporal() |
| D7 | Style / alignment | for_alignment(rank=) |
Status
0.5.0 — PMHTrainer (HF), CORAL baseline, GitHub Actions CI + PyPI publish guide.
| Extra | Install | Example |
|---|---|---|
| HF Trainer | pip install "matching-pmh[hf]" |
examples/10_hf_trainer.py |
| CORAL baseline | pmh.baselines.coral |
examples/06_office31_sklearn.py |
| CI / PyPI | see PUBLISHING.md |
tag v0.5.0 |
0.4.0 — Hugging Face D7 (estimate_style_sigma), Lightning (add_pmh_to_loss), Office-31 features (--office31-root).
| Extra | Install | Example |
|---|---|---|
| HF | pip install "matching-pmh[hf]" |
examples/08_hf_style_d7.py |
| Lightning | pip install "matching-pmh[lightning]" |
examples/09_lightning_module.py |
| Vision / Office-31 | pip install "matching-pmh[vision]" |
examples/06_office31_sklearn.py --office31-root ... |
Project details
Release history Release notifications | RSS feed
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 matching_pmh-0.6.0.tar.gz.
File metadata
- Download URL: matching_pmh-0.6.0.tar.gz
- Upload date:
- Size: 51.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98521c2ab028806cea0e1d7e28b63f97fb7bf98acd72451b3744cbdda3af4191
|
|
| MD5 |
53fe8f20d3afff24e8791819516923a7
|
|
| BLAKE2b-256 |
98dc93a034739900d7c53ad5cff378a486cdc80184b06ff73d5c49bb89507dcc
|
File details
Details for the file matching_pmh-0.6.0-py3-none-any.whl.
File metadata
- Download URL: matching_pmh-0.6.0-py3-none-any.whl
- Upload date:
- Size: 42.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
295f5c4dc54cab33c8056c345dea4aae7e1709a228b36c2f01bc756bdd98a333
|
|
| MD5 |
d03606a008beb09210aaf62d9d5fcb1f
|
|
| BLAKE2b-256 |
2f3f2198f71791216788d05eaa4959c3966434cd261a8155b1dad750ed865d25
|