Python package allowing for the exploration of latent spaces of generative models through Riemannian geometry.
Project description
Latent Geometry
Master's thesis python package allowing for the exploration of latent spaces of generative models through Riemannian geometry.
By employing a pull-back metric from the observation space one can reveal nuanced geometrical structures in hidden spaces. The framework is agnostic to the automatic differetiation backend e.g. PyTorch, TensorFlow. It works even with custom, hand-made differentiable mappings.
Installation
pip install latent-geometry
Usage
Geodesics
import numpy as np
import torch.nn as nn # just for the sake of example
your_neural_net: nn.Module = YourPyTorchNet(latent_dim=8)
# create Mapping
from latent_geometry.mapping import Mapping, TorchModelMapping
mapping: Mapping = TorchModelMapping(
model=your_neural_net,
in_shape=(8,), # dimensionality of the domain w/o batch size
out_shape=(3, 32, 32), # dimensionality of the co-domain w/o batch size
batch_size=batch_size,
call_fn=your_neural_net.forward, # optional
)
# define your favourite metric for the observation space
from latent_geometry.metric import EuclideaMetric
ambient_metric = EuclideanMetric()
# create the manifold spanned by your latent space with the pulled-back ambient metric
from latent_geometry.manifold import LatentManifold
latent_manifold = LatentManifold(
mapping=latent_mapping,
ambient_metric=ambient_metric,
)
# calculate the geodesic starting from z_0 with velocity v_0
from latent_geometry.path import ManifoldPath
z_0 = np.zeros(8)
v_0 = np.ones_like(z_0)
geodesic: ManifoldPath = latent_manifold.geodesic(z=z_0, velocity_vec=v_0)
# geodesic(0) == z_0
# calculate the the shortest path between z_a and z_b
z_a = np.zeros(8)
z_b = np.zeros(8) + 3
shortest_path: ManifoldPath = shortest_path.geodesic(z_a=z_a, z_b=z_b)
# shortest_path(0.0), shortest_path(1.0) == z_a, z_b
If your mapping is not based on PyTorch you need to implement one of two possible interfaces
# you can implement first and second derivative of output wrt. to input
from latent_geometry.mapping import DerivativeMapping
class YourMappingWrapper(DerivativeMapping):
def __init__(self, your_mapping: Callable) -> None: ...
def jacobian(self, zs: np.ndarray) -> np.ndarray: ...
def second_derivative(self, zs: np.ndarray) -> np.ndarray: ...
# or follow the other interface (for speed-up purpuses)
from latent_geometry.mapping import MatrixMapping
class YourMappingWrapper(MatrixMapping):
def __init__(self, your_mapping: Callable) -> None: ...
def jacobian(self, zs: np.ndarray) -> np.ndarray: ...
def metric_matrix_derivative(
self, zs: np.ndarray, ambient_metric_matrices: np.ndarray
) -> np.ndarray: ...
Riemannian optimizer
only for PyTorch right now
import torch
import torch.nn as nn
from latent_geometry.optim import TorchMetric, InputGDOptimizer
your_neural_net: nn.Module = YourPyTorchNet()
loss_fn = lambda x: x.mean()
example_input = torch.zeros(8, requires_grad=True)
optimizer = InputDGOptimizer(
param=example_input,
metric=TorchMetric(mapping=your_neural_net),
lr=0.001,
gradient_type="geometric" # may also be "standard", "retractive"
)
for _ in range(1_000):
optimizer.zero_grad()
x = your_neural_net(example_input)
loss = loss_fn(x)
loss.backward()
optimizer.step()
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
File details
Details for the file latent-geometry-1.1.0.tar.gz
.
File metadata
- Download URL: latent-geometry-1.1.0.tar.gz
- Upload date:
- Size: 34.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd7299dc01bdb93cef3971349cbe632a1716e9aec20df57c48fdf9e48f99f424 |
|
MD5 | b0e26ce8fbd719f1334dabd8f572fd3f |
|
BLAKE2b-256 | ff014ec893ac9f597dceb4e0d5e97ffcf89b8e2338430311d38caf8e08ffd9e6 |
File details
Details for the file latent_geometry-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: latent_geometry-1.1.0-py3-none-any.whl
- Upload date:
- Size: 37.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 121b7f6d0efdb33360fc616078e89d147a6404e4cedaf897b73bdbbe2d7dbb27 |
|
MD5 | 387830a3b4048604d43d709d7b04c22c |
|
BLAKE2b-256 | f969bc880f6b1d9399087ace4af93a0754276a1e38ad747cbc456ec48174e77c |