|
10 | 10 |
|
11 | 11 | from __future__ import annotations |
12 | 12 |
|
13 | | -from typing import Optional |
| 13 | +import sys |
| 14 | +from typing import Dict, List, Optional, Tuple |
14 | 15 |
|
| 16 | +import numpy as np |
15 | 17 | import torch |
16 | 18 | from jaxtyping import Float |
17 | 19 |
|
18 | 20 | from ..manifolds import ProductManifold |
19 | 21 | from ._base import BaseEmbedder |
| 22 | +from ._losses import distortion_loss |
20 | 23 |
|
| 24 | +# TQDM: notebook or regular |
| 25 | +if "ipykernel" in sys.modules: |
| 26 | + from tqdm.notebook import tqdm |
| 27 | +else: |
| 28 | + from tqdm import tqdm |
21 | 29 |
|
22 | | -class SiameseNetwork(torch.nn.Module): |
| 30 | + |
| 31 | +class SiameseNetwork(BaseEmbedder, torch.nn.Module): |
23 | 32 | """Siamese network for embedding data into a product manifold space. |
24 | 33 |
|
25 | 34 | A Siamese network consists of an encoder network that maps input data to a latent representation in a product |
26 | 35 | manifold, and optionally a decoder network that maps the latent representation back to the original feature space. |
27 | 36 |
|
28 | 37 | Attributes: |
29 | | - pm: The product manifold object defining the embedding space. |
30 | | - encoder: Neural network module that maps input data to the embedding space. |
31 | | - decoder: Optional neural network module for reconstructing input data from embeddings. |
32 | | - reconstruction_loss: Loss function for measuring reconstruction quality. |
| 38 | + pm: Product manifold defining the structure of the latent space. |
| 39 | + random_state: Random state for reproducibility. |
| 40 | + encoder: Neural network that maps inputs to latent embeddings. |
| 41 | + decoder: Neural network that reconstructs inputs from latent embeddings. |
| 42 | + beta: Weight for the distortion term in the loss function. |
| 43 | + device: Device for tensor computations. |
| 44 | + reconstruction_loss: Type of reconstruction loss to use. |
| 45 | + |
33 | 46 |
|
34 | 47 | Args: |
35 | | - pm: Product manifold object defining the target embedding space. |
36 | | - encoder: Neural network module that maps inputs to the embedding space. |
37 | | - decoder: Optional neural network module that maps embeddings back to input space. If None, a no-op identity |
38 | | - module is used. |
39 | | - reconstruction_loss: Type of reconstruction loss to use. Currently only "mse" (mean squared error) is supported. |
40 | | -
|
41 | | - Raises: |
42 | | - ValueError: If an unsupported reconstruction_loss is specified. |
| 48 | + pm: Product manifold defining the structure of the latent space. |
| 49 | + encoder: Neural network module that maps inputs to the manifold's intrinsic dimension. |
| 50 | + The output dimension should match the intrinsic dimension of the product manifold. |
| 51 | + decoder: Neural network module that maps latent representations back to the input space. |
| 52 | + random_state: Optional random state for reproducibility. |
| 53 | + device: Optional device for tensor computations. |
| 54 | + beta: Weight of the distortion term in the loss function. |
| 55 | + reconstruction_loss: Type of reconstruction loss to use. |
43 | 56 | """ |
44 | 57 |
|
45 | 58 | def __init__( |
|
0 commit comments