gaia.core.metric_yoneda module

Metric Yoneda Lemma for GAIA Framework

Implements Section 6.7 from GAIA paper: “The Metric Yoneda Lemma”

THEORETICAL FOUNDATIONS: - Generalized metric spaces (X, d) with non-symmetric distances - [0,∞]-enriched categories for distance computations - Yoneda embedding y: X → X̂ as universal representer - Isometric property: X(x,x’) = X̂(y(x), y(x’)) - Applications to LLMs, image processing, probability distributions

This enables GAIA to work with non-symmetric distances and build universal representers for any objects in generalized metric spaces.

class gaia.core.metric_yoneda.GeneralizedMetricSpace(objects, distance_function, name=None)[source]

Bases: Generic[X]

Generalized metric space (X, d) From (MAHADEVAN,2024) Section 6.7

Properties: 1. d(x,x) = 0 (reflexivity) 2. d(x,z) ≤ d(x,y) + d(y,z) (triangle inequality)

NOT required: - Symmetry: d(x,y) = d(y,x) - Identity: d(x,y) = 0 ⟹ x = y - Finiteness: d(x,y) < ∞

__init__(objects, distance_function, name=None)[source]
distance(x, y)[source]

Compute distance d(x,y) in generalized metric space

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

class gaia.core.metric_yoneda.EnrichedCategory[source]

Bases: object

[0,∞]-enriched category from GAIA paper

Objects are non-negative real numbers including ∞ Morphisms exist r → s iff r ≤ s Monoidal structure via addition: r ⊗ s = r + s

__init__()[source]
add_object(r)[source]

Add object r ∈ [0,∞] to category

has_morphism(r, s)[source]

Check if morphism r → s exists (i.e., r ≤ s)

tensor_product(r, s)[source]

Monoidal structure: r ⊗ s = r + s

internal_hom(r, s)[source]

Internal hom [0,∞](r,s) for compact closed structure

categorical_product(r, s)[source]

Categorical product: r ⊓ s = max{r,s}

categorical_coproduct(r, s)[source]

Categorical coproduct: r ⊔ s = min{r,s}

class gaia.core.metric_yoneda.YonedaEmbedding(metric_space)[source]

Bases: Generic[X]

Yoneda embedding y: X → X̂ for generalized metric spaces

From (MAHADEVAN,2024) Theorem 8: y(x) = X(-,x): X^op → [0,∞]

Key properties: 1. Isometric: X(x,x’) = X̂(y(x), y(x’)) 2. Universal representer: objects determined by interactions 3. Non-expansive function into presheaf category

__init__(metric_space)[source]
embed(x)[source]

Yoneda embedding: x ↦ X(-,x)

Returns presheaf representing x by its interactions with all objects

presheaf_distance(presheaf1, presheaf2)[source]

Compute distance in presheaf category X̂

X̂(φ,ψ) = sup_y |φ(y) - ψ(y)| for presheaves φ,ψ

verify_isometry(x, x_prime, tolerance=1e-6)[source]

Verify isometric property: X(x,x’) = X̂(y(x), y(x’))

This is Theorem 9 from GAIA paper

universal_property(x, phi)[source]

Universal property: X̂(X(-,x), φ) = φ(x)

This is the metric Yoneda lemma from GAIA paper

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

class gaia.core.metric_yoneda.MetricYonedaApplications[source]

Bases: object

Applications of Metric Yoneda Lemma to AI/ML problems

From GAIA paper: “discriminate two objects (probability distributions, images, text documents) by comparing them in suitable metric space”

static create_text_metric_space(documents)[source]

Create metric space for text documents using edit distance

Example From (MAHADEVAN,2024) Section 6.7 for string comparison

static create_image_metric_space(images)[source]

Create metric space for images using perceptual distance

Non-symmetric distance for image comparison

static create_probability_metric_space(distributions)[source]

Create metric space for probability distributions

Using Wasserstein-like distance (non-symmetric)

static create_neural_embedding_space(embeddings)[source]

Create metric space for neural embeddings (e.g., from LLMs)

Non-symmetric distance based on attention-like mechanism

class gaia.core.metric_yoneda.UniversalRepresenter(metric_space)[source]

Bases: object

Universal representer using Metric Yoneda Lemma

Enables representing any object by its interactions with other objects, crucial for foundation models and generative AI.

__init__(metric_space)[source]
represent(obj)[source]

Create universal representation of object

Returns Yoneda embedding as universal representer

compare_objects(obj1, obj2)[source]

Compare two objects using universal representations

Returns both original and representational distances

find_nearest_neighbors(query_obj, k=5)[source]

Find k nearest neighbors using universal representation

Useful for retrieval in foundation models

gaia.core.metric_yoneda.create_llm_metric_space(token_sequences)[source]

Create metric space for LLM token sequences

gaia.core.metric_yoneda.create_causal_metric_space(causal_graphs)[source]

Create metric space for causal graphs (DAGs)