A repo for RR layer in Pytorch.
Project description
RR_layer
RR_layer is a JAX / Equinox library implementing a Rank-Reduction (RR) layer: a layer that compresses its input by projecting onto a low-rank SVD basis.
The mechanics are different between training and evaluation:
- During training, the SVD basis is recomputed from scratch for every batch. For each batch, the layer computes the SVD of the batch, truncates to the top
rcomponents, and uses that truncated basis to project — so the basis is always data-dependent and up to date with the current batch, and gradients flow through the SVD/projection step. The model also automatically saves the lastN=basis_history_sizebases to be later used in evaluation. - During evaluation, when layer.eval() is called, the layer will find a common basis to the whole dataset
Uf, based on the lastN=basis_history_sizethat were saved. When the layer is called in evaluation mode, the SVD is replaced by a projection on that basisUf. - In both cases, the shape of the input should be (N x ...), where N is the number samples in a batch. Note that the output will have the same shape is the input. This is because the layer performs the SVD, truncates, but then returns the reconstructed latent space. So the compression happens inside eventhough the output still has the same shape.
What's inside
RR_layer/
├── RR_layer/
│ ├── __init__.py
│ └── rr_layer.py # the layer implementation(s)
├── examples/
| ├──── autoencoder
│ | ├── generate_data.ipynb
│ | ├── train_baseline.ipynb
│ | ├── train_RRAE.ipynb
│ | └── compare.ipynb
└── tests/
└── tests.py
RR_layer/rr_layer.py
The library code. Contains the RR_layer Equinox module: a layer with two modes of operation —
- train mode: computes the SVD of the current batch's activations, truncates to rank
r, and projects onto that basis (gradients flow through this). - eval mode: projects onto a basis that was saved from training, rather than computing a new SVD.
Examples/
The example notebooks are meant to be worked through in order:
-
generate_data.ipynb— generates the dataset used in the other notebooks. Run this first; it should leave behind whatever data artifacts the later notebooks load. -
train_baseline.ipynb— trains a standard, non-RR autoencoder as a reference point. This gives you a baseline reconstruction quality and runtime to compare the RRAE against. -
train_RRAE.ipynb— trains the Rank-Reduction Auto-Encoder, built with one or moreRR_layerin the latent space. During training this recomputes the SVD basis per batch as described above; at the end of training it should save the basis to be reused at evaluation time. -
compare.ipynb— loads the trained baseline and RRAE models (using the saved, frozen SVD basis for the RRAE) and compares them for tasks such as linear interpolation and random generation.
Run the notebooks in the order above — each later notebook generally depends on artifacts (data, images) produced by the earlier ones.
tests/tests.py
Unit tests for the RR_layer module. Useful both to verify correctness after installing, and as additional usage examples of the layer API beyond the notebooks (e.g. shape checks, gradient flow through the SVD step in train mode, etc.)
Running the tests
python -m pytest tests/tests.py
Installation
pip install RR_layer
Quick start
import jax
from RR_layer import RRLayer # replace with actual export
inp = torch.randn(32, 768)
rr = RRLayer(rank=8)
rr.train()
y = rr(inp) # will compute the SVD
rr.eval()
y = rr(inp) # will project onto a saved basis
Background
For more information about Rank Reduction layers, please refer to:
- Paper: Rank Reduction Autoencoders
- Related repo: JadM133/RRAEs
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 rr_layer-0.1.1.tar.gz.
File metadata
- Download URL: rr_layer-0.1.1.tar.gz
- Upload date:
- Size: 450.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b78b68e4955586a5de7d20f84dc2c0ff7e36244707e02b888ffe0bbae45489f
|
|
| MD5 |
815d94e27d1cc346446d4e633092739b
|
|
| BLAKE2b-256 |
bd8dceab50c62bb8cab846aed275138481d4b09a45253c03eda6483e51c7d23b
|
File details
Details for the file rr_layer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: rr_layer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
502cac6dfc64cf5f87197ad2204aeb6560160be3b3800ba0ef8c9cb9b4c5cdcf
|
|
| MD5 |
cd8e959cc0e25e79bfa4e9f67c2deaf8
|
|
| BLAKE2b-256 |
d6fa27a505ca08b99aedc1247dce3aaf57dc0ca9071f8422fe4e0c9c3af4157c
|