-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifold.py
85 lines (73 loc) · 2.66 KB
/
manifold.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import Tuple, Union, Optional
import torch
from abc import ABC, abstractmethod
class Manifold(ABC):
def __init__(self,
curvature: float) -> None:
if curvature == 0.0:
raise TypeError("curvature=0 means we're using Euclidean Geometry. Try another value :)")
self.curvature = curvature
def __repr__(self) -> str:
return f"{self.__class__.__name__}Manifold, curvature={self.curvature}"
def __eq__(self, other) -> bool:
return self.__class__.__name__ == other.__class__.__name__ and self.curvature == other.curvature
@abstractmethod
def proj_(self,
x: torch.Tensor,
dim: Union[int, Tuple[int]] = -1) -> None:
raise NotImplementedError()
@abstractmethod
def log(self,
x: torch.Tensor,
v: torch.Tensor,
dim: Union[int, Tuple[int]] = -1) -> torch.Tensor:
'''
Mapping of point v from Manifold to Tangent Space at point x
'''
raise NotImplementedError()
@abstractmethod
def exp(self,
x: torch.Tensor,
v: torch.Tensor,
dim: Union[int, Tuple[int]] = -1) -> torch.Tensor:
'''
Mapping of point v from Tangent space at point x back to Manifold
'''
raise NotImplementedError()
@abstractmethod
def zero_log(self,
x: torch.Tensor,
dim: Union[int, Tuple[int]] = -1) -> torch.Tensor:
'''
Mapping of point x from Manifold to Tangent Space at point 0
'''
raise NotImplementedError()
@abstractmethod
def zero_exp(self,
v: torch.Tensor,
dim: Union[int, Tuple[int]] = -1) -> torch.Tensor:
'''
Mapping from Tangent space at point 0 of point v back to Manifold
'''
raise NotImplementedError()
@abstractmethod
def parallel_transport(self,
x: torch.Tensor,
dim: Union[int, Tuple[int]] = -1,
from_: Optional[torch.Tensor] = None,
to_: Optional[torch.Tensor] = None) -> torch.Tensor:
raise NotImplementedError()
@abstractmethod
def linear(self,
x: torch.Tensor,
w: torch.Tensor) -> torch.Tensor:
'''
zero_log mapping + linear mapping + zero_exp mapping
'''
raise NotImplementedError()
@abstractmethod
def hyperplane(self,
x: torch.Tensor,
p: torch.Tensor,
a: torch.Tensor) -> torch.Tensor:
raise NotImplementedError()