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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lsuv-0.3.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

lsuv-0.3.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file lsuv-0.3.0.tar.gz.

File metadata

  • Download URL: lsuv-0.3.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.0

File hashes

Hashes for lsuv-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cc684b03e3f5a94aec53a1facc740118e01d84378b5b0518ba2c78f70af4ac02
MD5 bcc6cf40dcc24a72343644ade3c445f4
BLAKE2b-256 a642b2eda1a30975556f54b2e3e283edcdff4aeb33bbe53d6996aac213ab3b51

See more details on using hashes here.

File details

Details for the file lsuv-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: lsuv-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.0

File hashes

Hashes for lsuv-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 739cd9d3817611cd491f452b34758ad4cd4051a94b53a94e71041fd94795d213
MD5 65803b049df07e818a2a323b91b630da
BLAKE2b-256 3fdaa2cc45159893a54947c50066963c9067c90a30b11389593e91216f597cd0

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.2

1 file

0.2.1

1 file

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