SPACE (Subspace Partitioning for Accessible Controlled Encodings)
SPACE-Learn provides a utility library build on pytorch, that offers functions to train your Deep-Learning Models on the Latent-SPACE regime. The SPACE method helps create structured and easy to decode embedding spaces or explicitly guide a models internal latent space using training side information. This method encodes features that are known during training time or intermediate results directly into a models latentspace.
Example Setup
1. Install
pip install spacelearn
2. Example Training Setup
import torch
from torch.optim import Adam
from collections import defaultdict
from spacelearn import (
solve_dims,
solve_subspaces,
combined_loss,
)
from spacelearn.data import input_to_quantity
from spacelearn.optim import minimal_latent_dim
from my_model import Model
from my_data import (
DataLoader,
quantity_helper_a,
quantity_helper_b,
)
EPOCHS = 5
LR = 1e-3
MAX_BINS = 10
# target variance retained by pooling
BIN_THRESHOLDS = 0.80
# target variance retained by subspaces
K_THRESHOLDS = {
"A": 0.90,
"B": 0.85,
}
REEVAL_EVERY = 2
INIT_SAMPLES = 100
dataloader = DataLoader()
# ------------------------------------------------------------------
# 1. Collect data for dimension estimation
# ------------------------------------------------------------------
test_data = defaultdict(list)
samples = []
for i in range(INIT_SAMPLES):
sample = dataloader[i]
test_data["A"].append(
quantity_helper_a(samples=sample)
)
test_data["B"].append(
quantity_helper_b(samples=sample)
)
samples.append(sample)
test_data["A"] = torch.stack(test_data["A"])
test_data["B"] = torch.stack(test_data["B"])
samples = torch.stack(samples)
# ------------------------------------------------------------------
# 2. Estimate n and k
# ------------------------------------------------------------------
dims = solve_dims(
test_data,
max_bins=MAX_BINS,
bin_thresholds=BIN_THRESHOLDS,
k_thresholds=K_THRESHOLDS,
k_per_quantity=True,
)
# shared pooling resolution
N = next(iter(dims.values()))[0]
# per-quantity subspace dimensions
K = {q: k for q, (_, k) in dims.items()}
# latent dimension
D = minimal_latent_dim(
K,
free_frac=0.10,
)
# ------------------------------------------------------------------
# 3. Build quantity extraction helper
# ------------------------------------------------------------------
pool_helper = input_to_quantity(
N,
"avg",
A=quantity_helper_a,
B=quantity_helper_b,
)
# ------------------------------------------------------------------
# 4. Initialize model
# ------------------------------------------------------------------
model = Model(D)
optimizer = Adam(model.parameters(), lr=LR)
# ------------------------------------------------------------------
# 5. Training loop
# ------------------------------------------------------------------
W_prev = None
for epoch in range(EPOCHS):
# periodically recompute subspaces
if epoch % REEVAL_EVERY == 0:
with torch.no_grad():
Z_ref = model(samples)
Y_ref = pool_helper(samples=samples)
WAV = solve_subspaces(
Z_ref,
Y_ref,
k=k,
)
W = WAV.W
A = WAV.A
if W_prev is None:
W_prev = {
q: w.clone()
for q, w in W.items()
}
for batch in dataloader:
Y = pool_helper(samples=batch)
Z = model(batch)
space_loss = combined_loss(
Z,
W,
A,
Y,
W_prev,
)
# task_loss = ...
# loss = task_loss + space_loss
optimizer.zero_grad()
space_loss.backward()
optimizer.step()
W_prev = {
q: w.clone()
for q, w in W.items()
}
DOCS:
Metadata
Release files for spacelearn 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spacelearn-0.1.0.tar.gz | 95.9 kB | Without provenance | Details for spacelearn-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spacelearn-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for spacelearn-0.1.0-py3-none-any.whl |
Total release size: 127.2 kB
Release files / spacelearn-0.1.0.tar.gz
| Download URL | spacelearn-0.1.0.tar.gz |
|---|---|
| Size | 95.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
277b7725a6db75087b41cd86670a7b79e67f752fa9a2554cdf2dcc7ac6cb5d49
|
|
BLAKE2b-256 checksum How to use checksums |
7aa9f5a0afe2f21a4dc83462a8bcb618078c7fed525bd774974f7dcdfc98cd26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Release files / spacelearn-0.1.0-py3-none-any.whl
| Download URL | spacelearn-0.1.0-py3-none-any.whl |
|---|---|
| Size | 31.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
807abc2e3ef22695c59383027a6a281c18a864753ab44c7da6fc58c10607a266
|
|
BLAKE2b-256 checksum How to use checksums |
e35a302e58fa887f2033bd1922efb30c9fa5da8b6d09d2baba424c1d690dadd3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Security for spacelearn 0.1.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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.