-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.py
88 lines (65 loc) · 2.52 KB
/
base.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
86
87
88
"""Base manifold."""
from torch.nn import Parameter
class Manifold(object):
"""
Abstract class to define operations on a manifold.
"""
def __init__(self):
super().__init__()
self.eps = 10e-8
def sqdist(self, p1, p2, c):
"""Squared distance between pairs of points."""
raise NotImplementedError
def egrad2rgrad(self, p, dp, c):
"""Converts Euclidean Gradient to Riemannian Gradients."""
raise NotImplementedError
def proj(self, p, c):
"""Projects point p on the manifold."""
raise NotImplementedError
def proj_tan(self, u, p, c):
"""Projects u on the tangent space of p."""
raise NotImplementedError
def proj_tan0(self, u, c):
"""Projects u on the tangent space of the origin."""
raise NotImplementedError
def expmap(self, u, p, c):
"""Exponential map of u at point p."""
raise NotImplementedError
def logmap(self, p1, p2, c):
"""Logarithmic map of point p1 at point p2."""
raise NotImplementedError
def expmap0(self, u, c):
"""Exponential map of u at the origin."""
raise NotImplementedError
def logmap0(self, p, c):
"""Logarithmic map of point p at the origin."""
raise NotImplementedError
def mobius_add(self, x, y, c, dim=-1):
"""Adds points x and y."""
raise NotImplementedError
def mobius_matvec(self, m, x, c):
"""Performs hyperboic martrix-vector multiplication."""
raise NotImplementedError
def init_weights(self, w, c, irange=1e-5):
"""Initializes random weigths on the manifold."""
raise NotImplementedError
def inner(self, p, c, u, v=None, keepdim=False):
"""Inner product for tangent vectors at point x."""
raise NotImplementedError
def ptransp(self, x, y, u, c):
"""Parallel transport of u from x to y."""
raise NotImplementedError
def ptransp0(self, x, u, c):
"""Parallel transport of u from the origin to y."""
raise NotImplementedError
class ManifoldParameter(Parameter):
"""
Subclass of torch.nn.Parameter for Riemannian optimization.
"""
def __new__(cls, data, requires_grad, manifold, c):
return Parameter.__new__(cls, data, requires_grad)
def __init__(self, data, requires_grad, manifold, c):
self.c = c
self.manifold = manifold
def __repr__(self):
return '{} Parameter containing:\n'.format(self.manifold.name) + super(Parameter, self).__repr__()