Skip to main content

Layer-sequential unit-variance (LSUV) initialization for PyTorch

This package for neural network initialization.

Installation

pip install lsuv

Usage

import torch
from lsuv import lsuv_with_dataloader, lsuv_with_singlebatch

# from a dataloader (uses the first batch)
model = lsuv_with_dataloader(model, dataloader, device=torch.device('cpu'))

# or from a single batch of data
model = lsuv_with_singlebatch(model, batch, device=torch.device('cpu'))

For dataloaders yielding dict-style batches, pass get_input to extract the model input:

model = lsuv_with_dataloader(model, dataloader, get_input=lambda batch: batch["image"])

Both functions accept needed_std (target activation std, default 1.0), std_tol (tolerance, default 0.1), max_attempts (default 10), do_orthonorm (orthonormal init before scaling, default True) and verbose.

See more examples in test.

Notes

  • Supported layers: Conv1d/2d/3d, ConvTranspose1d/2d/3d, Linear and MultiheadAttention.
  • nn.MultiheadAttention is treated as one unit: LSUV scales its output projection so the attention block output has unit variance; the input projection only receives the orthonormal init.
  • The model's train/eval mode is restored after initialization (LSUV runs in eval mode internally).
  • If a layer is never called during the forward pass (e.g. an unused module), it is skipped with a warning.

LSUV initialization is described in:

Mishkin, D. and Matas, J.,(2015). All you need is a good init. ICLR 2016 arXiv:1511.06422.

Previous implementations

Original Caffe implementation https://github.com/ducha-aiki/LSUVinit

Torch re-implementation https://github.com/yobibyte/torch-lsuv

PyTorch in fastai https://github.com/fastai/course-v3/blob/master/nbs/dl2/07a_lsuv.ipynb

Keras implementation: https://github.com/ducha-aiki/LSUV-keras

Thinc re-implementation LSUV-thinc

Metadata

Release files for lsuv 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lsuv 0.3.0
File Size Uploaded Provenance
lsuv-0.3.0.tar.gz 6.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for lsuv 0.3.0
File Interpreter ABI Platform
lsuv-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / lsuv-0.3.0.tar.gz

Download URL lsuv-0.3.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
cc684b03e3f5a94aec53a1facc740118e01d84378b5b0518ba2c78f70af4ac02
BLAKE2b-256 checksum
How to use checksums
a642b2eda1a30975556f54b2e3e283edcdff4aeb33bbe53d6996aac213ab3b51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.0

Release files / lsuv-0.3.0-py3-none-any.whl

Download URL lsuv-0.3.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
739cd9d3817611cd491f452b34758ad4cd4051a94b53a94e71041fd94795d213
BLAKE2b-256 checksum
How to use checksums
3fdaa2cc45159893a54947c50066963c9067c90a30b11389593e91216f597cd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.0

Security for lsuv 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page