Metadata-Version: 2.4
Name: spacelearn
Version: 0.1.0
Summary: Guided latent subspace learning for deep neural networks
Project-URL: Homepage, https://github.com/K-T0BIAS/SPACE
Project-URL: Issues, https://github.com/K-T0BIAS/SPACE/issues
Author-email: Tobias Karusseit <karusseittobi@gmail.com>
License: MIT License
        
        Copyright (c) [2026] [Tobias Karusseit]
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: latent-space,physics-informed,pytorch,representation-learning,subspaces
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Requires-Dist: numpy>=2.0.0
Requires-Dist: torch>=2.0.0
Provides-Extra: dev
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: tox; extra == 'dev'
Description-Content-Type: text/markdown

[README](./README.md)

# 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

```bash
pip install spacelearn
```

### 2. Example Training Setup

```python
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:

[spacelearn](./docs/SPACELEARN.md)

[spacelearn.settings](./docs/SETTINGS.md)

[spacelearn.data](./docs/data/DATA.md)

[spacelearn.loss](./docs/loss/LOSS.md)

[spacelearn.optim](./docs/optim/OPTIM.md)