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 maps between:

  • 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:

  • sample seed_latents (shape (num_seeds, latent_dim)) from the latent distribution
  • build a chart (e.g. knothe_rosenblatt_surrogate_chart(seed_latents))
  • pick a coordinate u \in [0,1]^(num_seeds-1))
  • map u -> z and optionally round-trip z -> u
  • generate z -> x using the generative model

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 seed 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.2.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.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for latent_spaces_by_example-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c0b9b317b41a6be75db24d2090759cb2541c600d608546ebf7e3f337b791f441
MD5 217bddc8af20e676c0f8245d5c798863
BLAKE2b-256 59bc7268713088ddfd56f00de5782ff7a8bad52e3eca3a5dc5a35c47d095ac77

See more details on using hashes here.

Provenance

The following attestation bundles were made for latent_spaces_by_example-0.1.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for latent_spaces_by_example-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8e7ff6f2490a5368841104cd6f16fb869948f1102a24c461bfdbd7b4afd06355
MD5 ea92309a6f57d4c92e49e972cf46aee2
BLAKE2b-256 42511040090163a3992e7c84e7220de67665f3ac17536a14ee009ed18670ec6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for latent_spaces_by_example-0.1.2-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