Skip to main content

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 -> z and optionally round-trip z -> 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


Download files

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

Source Distribution

latent_spaces_by_example-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

latent_spaces_by_example-0.1.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file latent_spaces_by_example-0.1.1.tar.gz.

File metadata

File hashes

Hashes for latent_spaces_by_example-0.1.1.tar.gz
Algorithm Hash digest
SHA256 adc99d1f6ad149988f48b9602233a604f65d0feb357aee53f3efb8b6835bce7d
MD5 757c998fef1f556c318a8c146ddcb0c6
BLAKE2b-256 e6e008df81b36206f62039e0da391b3e7342a9e3e53959a7dd8251b130cea640

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file latent_spaces_by_example-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for latent_spaces_by_example-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 849e235d0df603979614ce364fc007f8944b0ba90512521db85f126613fa89ac
MD5 2b548f15bf9254dd7858a61441004e96
BLAKE2b-256 1504b763b18f95d929c7cb36d308abac4d07a8e1a5a495071d954b2be0262ea0

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page