Latent spaces by example.
Project description
latent-spaces-by-example
Python package: latent-spaces-by-example (import as latent_spaces_by_example).
What this repo is
This repo provides utilities for performing black-box search/optimisation over the outputs of generative (diffusion/flow matching) models.
This works by defining surrogate latent spaces that map a model’s high-dimensional latent representation into a low-dimensional space where standard black-box optimisation algorithms can be applied.
You form these spaces by providing a small set of latents corresponding to specific outputs of the generative model (called “seeds”). A surrogate latent space then defines a map:
u: hypercube coordinates, shape ((N, n_seeds-1))w: nonnegative weights on the unit sphere, shape ((N, n_seeds))z: latent vectors, shape ((N, n_latent_dims))
The main API is latent_spaces_by_example.SurrogateChart.
Paper
Defining latent spaces by example: optimisation over the outputs of generative models
Install
Create / sync the environment (uses uv):
uv sync
Quick usage
The minimal workflow is:
- choose
seed_latents(shape(num_seeds, latent_dim)) - build a chart (e.g.
knothe_rosenblatt_surrogate_chart(seed_latents)) - sample
u ~ Uniform([0,1]^(num_seeds-1)) - map
u -> zand optionally round-tripz -> u
See examples/basic_surrogate_chart.py.
Examples
Flux + CLIP optimisation (3 seeds → 2D u-space)
This example optimises a 2D coordinate (u \in [0,1]^2) (formed from three basis latents) to maximise a CLIP similarity score of images generated by Flux. It also evaluates a 2D grid and saves a contour plot. In this example the basis latents are sampled randomly (i.e. they correspond to random images).
Install optional dependencies:
uv sync --group examples
Dry run (no model download; checks shape plumbing only):
uv run python examples/image_optimisation.py --dry-run
Small real run (downloads models; will be slow and may require GPU + huggingface-cli login):
uv run python examples/image_optimisation.py
Development
Run tests
uv run pytest
Lint
uv run ruff check .
Format
uv run isort --profile black . && black .
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 latent_spaces_by_example-0.1.1.tar.gz.
File metadata
- Download URL: latent_spaces_by_example-0.1.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adc99d1f6ad149988f48b9602233a604f65d0feb357aee53f3efb8b6835bce7d
|
|
| MD5 |
757c998fef1f556c318a8c146ddcb0c6
|
|
| BLAKE2b-256 |
e6e008df81b36206f62039e0da391b3e7342a9e3e53959a7dd8251b130cea640
|
Provenance
The following attestation bundles were made for latent_spaces_by_example-0.1.1.tar.gz:
Publisher:
python-publish.yml on sam-willis/latent-spaces-by-example
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latent_spaces_by_example-0.1.1.tar.gz -
Subject digest:
adc99d1f6ad149988f48b9602233a604f65d0feb357aee53f3efb8b6835bce7d - Sigstore transparency entry: 924681389
- Sigstore integration time:
-
Permalink:
sam-willis/latent-spaces-by-example@dc91afe5dbfcbbb21905fc6e5345e5a205fde200 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/sam-willis
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@dc91afe5dbfcbbb21905fc6e5345e5a205fde200 -
Trigger Event:
release
-
Statement type:
File details
Details for the file latent_spaces_by_example-0.1.1-py3-none-any.whl.
File metadata
- Download URL: latent_spaces_by_example-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
849e235d0df603979614ce364fc007f8944b0ba90512521db85f126613fa89ac
|
|
| MD5 |
2b548f15bf9254dd7858a61441004e96
|
|
| BLAKE2b-256 |
1504b763b18f95d929c7cb36d308abac4d07a8e1a5a495071d954b2be0262ea0
|
Provenance
The following attestation bundles were made for latent_spaces_by_example-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on sam-willis/latent-spaces-by-example
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latent_spaces_by_example-0.1.1-py3-none-any.whl -
Subject digest:
849e235d0df603979614ce364fc007f8944b0ba90512521db85f126613fa89ac - Sigstore transparency entry: 924681439
- Sigstore integration time:
-
Permalink:
sam-willis/latent-spaces-by-example@dc91afe5dbfcbbb21905fc6e5345e5a205fde200 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/sam-willis
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@dc91afe5dbfcbbb21905fc6e5345e5a205fde200 -
Trigger Event:
release
-
Statement type: