Skip to main content

Domain-robust PyTorch and sklearn: train on site A, deploy on site B with the same labels.

Project description

matching-pmh

Train on site A. Deploy on site B. Same labels.

Add a domain-robustness regularizer to your PyTorch model or sklearn pipeline — without replacing your architecture or reading a research paper first.

PyPI Python License: MIT CI

PyPI · GitHub · Find your application · Docs map · Open In Colab


New here?

Find your application (nuisance + walkthrough) → Golden paths (code) → pmh-train route --task YOUR_TASK

Doc map — what to read and skip

Developer API

from pmh import check_applicability, explain_task, robust_fit

print(explain_task("pose_or_keypoints"))
print(check_applicability(stack="pytorch", n_source=500, n_target=400).summary())

# PyTorch: one call
out = robust_fit(model, train_loader, source_batches=src, target_batches=tgt, hook="auto", epochs=20)

# sklearn: baseline vs PMH on target holdout
report = evaluate_baseline_vs_pmh(x_source, y_source, x_target, y_target, compare_to=("coral",))
print(report.summary())

# PyTorch: same report shape on val_loader
report = evaluate_robust_fit(model, train_loader, val_loader, source_batches=src, target_batches=tgt, hook="auto", epochs=10)
print(report.summary())

Route your task: pmh-train route --search pose · pmh-train route --task pose_or_keypoints · GOLDEN_PATHS.md · pmh-train wizard


Who this is for

You are doing… Start here
Pose / keypoints, new camera pmh-train route --task pose_or_keypoints
Vision classification / fine-tune pmh-train route --task vision_classification
Frozen .npy / sklearn pmh-train route --task frozen_embeddings_sklearn
LLM style/format drift pmh-train route --task llm_style_or_format
Not sure START_HERE.md (3 gates)

Not for: new test-time classes, unrelated label definitions, or “make any model robust to everything.”

When PMH helps (honest expectations) → · What is PMH →


5-minute try

Open In Colab
or locally:

pip install matching-pmh torch
git clone https://github.com/vishalstark512/matching-pmh.git
cd matching-pmh
python examples/00_first_run_domain_shift.py

You get baseline vs PMH target accuracy on synthetic data, then copy the pattern into your project.

Expected output: docs/DEMO_OUTPUT.md

pmh-train wizard

Default integration (PyTorch)

from pmh import PMHTrainer, PMHConfig

trainer = PMHTrainer(
    model,
    hook=backbone,
    head=classifier,
    nuisance="domain_shift",
    pmh_config=PMHConfig.balanced(),
    artifact_path="artifacts/my_run.pt",
)
trainer.fit(
    train_loader,
    source_batches=source_loader,
    target_batches=target_loader,
    epochs=20,
)

sklearn Pipeline (frozen features)

You already have embeddings x_source, x_target (same dimension). Adapt, then classify — like any other preprocessing step:

pip install "matching-pmh[sklearn]"

from pmh import PMHMatcher
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression

pipe = Pipeline([
    ("adapt", PMHMatcher(nuisance="domain_shift").fit(x_source, x_target)),
    ("clf", LogisticRegression(max_iter=500)),
])
pipe.fit(x_source, y_source)
# Score on held-out TARGET rows — not source-only accuracy

Gallery: tabular · examples/06_office31_sklearn.py


How it works (30 seconds)

flowchart LR
  A[Site A data] --> E[Estimate shift geometry once]
  B[Site B data] --> E
  E --> T[Train your model]
  T --> H[Representation hook h]
  H --> L[Task loss + robustness penalty]

Same hook layer for estimate and train. Docs home · First hour · Troubleshooting glossary


How it relates to CORAL / domain adaptation

PMH keeps your task loss and penalizes sensitivity in representation space along directions that differ between train and deploy environments. See vs CORAL.

Under the hood: estimate deployment geometry once, train with an extra matched penalty on hook h. Details are optional: Theory.


Documentation

Start: docs/index.md (reading order) → D1–D7 subtypesGolden paths

I want to… Read
Will it help? WHEN_PMH_HELPS.md
Integrate my repo GETTING_STARTED.md
Paper / benchmarks PAPER_ALIGNMENT.md · walkthroughs

Install

pip install matching-pmh
pip install "matching-pmh[sklearn]"   # classical ML path
pip install "matching-pmh[vision]"    # ResNet / timm examples
pip install "matching-pmh[hf]"        # LLM style shift

Advanced (estimators D1–D7, research)

The Grand Unification paper unifies many nuisance estimators (subspace, domain Gram, augmentations, style, …). The library exposes them when your shift story is not plain cross-domain:

pmh-train list-methods
pmh-train list-presets
Research / benchmark Doc
Paper block presets paper-presets-by-block.md
Office-31 reference table BENCHMARKS.md
Lemma-level math THEORY.md

Citation

@software{matching_pmh,
  title  = {matching-pmh: Matched PMH training from estimated deployment nuisance geometry},
  author = {Rajput, Vishal},
  year   = {2026},
  url    = {https://github.com/vishalstark512/matching-pmh}
}

License

MIT — see LICENSE.

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

matching_pmh-1.5.3.tar.gz (233.9 kB view details)

Uploaded Source

Built Distribution

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

matching_pmh-1.5.3-py3-none-any.whl (124.9 kB view details)

Uploaded Python 3

File details

Details for the file matching_pmh-1.5.3.tar.gz.

File metadata

  • Download URL: matching_pmh-1.5.3.tar.gz
  • Upload date:
  • Size: 233.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for matching_pmh-1.5.3.tar.gz
Algorithm Hash digest
SHA256 713cf998e394559c51a9976024edbf45b4213a8654db09e91061a284624f773e
MD5 5b83c04f2f7fb11d4471f3d8343df4ef
BLAKE2b-256 84eeb9571f907433ad665d38f573b4c01cb0b467c40cbf523796dbb39a9754c2

See more details on using hashes here.

File details

Details for the file matching_pmh-1.5.3-py3-none-any.whl.

File metadata

  • Download URL: matching_pmh-1.5.3-py3-none-any.whl
  • Upload date:
  • Size: 124.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for matching_pmh-1.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0d1a330ec97292205bec9a970fab5fc572aeee437df96db5ffd7388ce787e5d9
MD5 250fb10c5fbf3b5f68f657948a13daef
BLAKE2b-256 89734517ee8d2bb606ac6b8a7416a195decf1f9523a511d42c814bd0c05b1bb0

See more details on using hashes here.

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