-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
manifold.py
592 lines (497 loc) · 17.8 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import torch.nn
from typing import Tuple, Optional, List
from . import math
import geoopt
from ...utils import size2shape, broadcast_shapes
from ..base import Manifold, ScalingInfo
__all__ = [
"Stereographic",
"StereographicExact",
"PoincareBall",
"PoincareBallExact",
"SphereProjection",
"SphereProjectionExact",
]
_stereographic_doc = r"""
:math:`\kappa`-Stereographic model.
Parameters
----------
k : float|tensor
sectional curvature :math:`\kappa` of the manifold
- k<0: Poincaré ball (stereographic projection of hyperboloid)
- k>0: Stereographic projection of sphere
- k=0: Euclidean geometry
Notes
-----
It is extremely recommended to work with this manifold in double precision.
Documentation & Illustration
----------------------------
http://andbloch.github.io/K-Stereographic-Model/ or :doc:`/extended/stereographic`
"""
_references = """References
----------
The functions for the mathematics in gyrovector spaces are taken from the
following resources:
[1] Ganea, Octavian, Gary Bécigneul, and Thomas Hofmann. "Hyperbolic
neural networks." Advances in neural information processing systems.
2018.
[2] Bachmann, Gregor, Gary Bécigneul, and Octavian-Eugen Ganea. "Constant
Curvature Graph Convolutional Networks." arXiv preprint
arXiv:1911.05076 (2019).
[3] Skopek, Ondrej, Octavian-Eugen Ganea, and Gary Bécigneul.
"Mixed-curvature Variational Autoencoders." arXiv preprint
arXiv:1911.08411 (2019).
[4] Ungar, Abraham A. Analytic hyperbolic geometry: Mathematical
foundations and applications. World Scientific, 2005.
[5] Albert, Ungar Abraham. Barycentric calculus in Euclidean and
hyperbolic geometry: A comparative introduction. World Scientific,
2010.
"""
_poincare_ball_doc = r"""
Poincare ball model.
See more in :doc:`/extended/stereographic`
Parameters
----------
c : float|tensor
ball's negative curvature. The parametrization is constrained to have positive c
Notes
-----
It is extremely recommended to work with this manifold in double precision
"""
_sphere_projection_doc = r"""
Stereographic Projection Spherical model.
See more in :doc:`/extended/stereographic`
Parameters
----------
k : float|tensor
sphere's positive curvature. The parametrization is constrained to have positive k
Notes
-----
It is extremely recommended to work with this manifold in double precision
"""
# noinspection PyMethodOverriding
class Stereographic(Manifold):
__doc__ = r"""{}
{}
See Also
--------
:class:`StereographicExact`
:class:`PoincareBall`
:class:`PoincareBallExact`
:class:`SphereProjection`
:class:`SphereProjectionExact`
""".format(
_stereographic_doc, _references,
)
ndim = 1
reversible = False
name = property(lambda self: self.__class__.__name__)
__scaling__ = Manifold.__scaling__.copy()
@property
def radius(self):
return self.k.abs().sqrt().reciprocal()
def __init__(self, k=0.0, learnable=False):
super().__init__()
k = torch.as_tensor(k)
if not torch.is_floating_point(k):
k = k.to(torch.get_default_dtype())
self.k = torch.nn.Parameter(k, requires_grad=learnable)
def _check_point_on_manifold(
self, x: torch.Tensor, *, atol=1e-5, rtol=1e-5, dim=-1
) -> Tuple[bool, Optional[str]]:
px = math.project(x, k=self.k, dim=dim)
ok = torch.allclose(x, px, atol=atol, rtol=rtol)
if not ok:
reason = "'x' norm lies out of the bounds [-1/sqrt(c)+eps, 1/sqrt(c)-eps]"
else:
reason = None
return ok, reason
def _check_vector_on_tangent(
self, x: torch.Tensor, u: torch.Tensor, *, atol=1e-5, rtol=1e-5, dim=-1
) -> Tuple[bool, Optional[str]]:
return True, None
def dist(
self, x: torch.Tensor, y: torch.Tensor, *, keepdim=False, dim=-1
) -> torch.Tensor:
return math.dist(x, y, k=self.k, keepdim=keepdim, dim=dim)
def dist2(
self, x: torch.Tensor, y: torch.Tensor, *, keepdim=False, dim=-1
) -> torch.Tensor:
return math.dist(x, y, k=self.k, keepdim=keepdim, dim=dim) ** 2
def egrad2rgrad(self, x: torch.Tensor, u: torch.Tensor, *, dim=-1) -> torch.Tensor:
return math.egrad2rgrad(x, u, k=self.k, dim=dim)
def retr(self, x: torch.Tensor, u: torch.Tensor, *, dim=-1) -> torch.Tensor:
# always assume u is scaled properly
approx = x + u
return math.project(approx, k=self.k, dim=dim)
def projx(self, x: torch.Tensor, *, dim=-1) -> torch.Tensor:
return math.project(x, k=self.k, dim=dim)
def proju(self, x: torch.Tensor, u: torch.Tensor, *, dim=-1) -> torch.Tensor:
target_shape = broadcast_shapes(x.shape, u.shape)
return u.expand(target_shape)
def inner(
self,
x: torch.Tensor,
u: torch.Tensor,
v: torch.Tensor = None,
*,
keepdim=False,
dim=-1,
) -> torch.Tensor:
if v is None:
v = u
return math.inner(x, u, v, k=self.k, keepdim=keepdim, dim=dim)
def norm(
self, x: torch.Tensor, u: torch.Tensor, *, keepdim=False, dim=-1
) -> torch.Tensor:
return math.norm(x, u, k=self.k, keepdim=keepdim, dim=dim)
def expmap(
self, x: torch.Tensor, u: torch.Tensor, *, project=True, dim=-1
) -> torch.Tensor:
res = math.expmap(x, u, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def logmap(self, x: torch.Tensor, y: torch.Tensor, *, dim=-1) -> torch.Tensor:
return math.logmap(x, y, k=self.k, dim=dim)
def transp(self, x: torch.Tensor, y: torch.Tensor, v: torch.Tensor, *, dim=-1):
return math.parallel_transport(x, y, v, k=self.k, dim=dim)
def transp_follow_retr(
self, x: torch.Tensor, u: torch.Tensor, v: torch.Tensor, *, dim=-1
) -> torch.Tensor:
y = self.retr(x, u, dim=dim)
return self.transp(x, y, v, dim=dim)
def transp_follow_expmap(
self, x: torch.Tensor, u: torch.Tensor, v: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
y = self.expmap(x, u, dim=dim, project=project)
return self.transp(x, y, v, dim=dim)
def expmap_transp(
self, x: torch.Tensor, u: torch.Tensor, v: torch.Tensor, *, dim=-1, project=True
) -> Tuple[torch.Tensor, torch.Tensor]:
y = self.expmap(x, u, dim=dim, project=project)
v_transp = self.transp(x, y, v, dim=dim)
return y, v_transp
def retr_transp(
self, x: torch.Tensor, u: torch.Tensor, v: torch.Tensor, *, dim=-1
) -> Tuple[torch.Tensor, torch.Tensor]:
y = self.retr(x, u, dim=dim)
v_transp = self.transp(x, y, v, dim=dim)
return y, v_transp
def mobius_add(
self, x: torch.Tensor, y: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
res = math.mobius_add(x, y, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def mobius_sub(
self, x: torch.Tensor, y: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
res = math.mobius_sub(x, y, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def mobius_coadd(
self, x: torch.Tensor, y: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
res = math.mobius_coadd(x, y, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def mobius_cosub(
self, x: torch.Tensor, y: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
res = math.mobius_cosub(x, y, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def mobius_scalar_mul(
self, r: torch.Tensor, x: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
res = math.mobius_scalar_mul(r, x, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def mobius_pointwise_mul(
self, w: torch.Tensor, x: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
res = math.mobius_pointwise_mul(w, x, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def mobius_matvec(
self, m: torch.Tensor, x: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
res = math.mobius_matvec(m, x, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def geodesic(
self, t: torch.Tensor, x: torch.Tensor, y: torch.Tensor, *, dim=-1
) -> torch.Tensor:
return math.geodesic(t, x, y, k=self.k, dim=dim)
@__scaling__(ScalingInfo(t=-1))
def geodesic_unit(
self, t: torch.Tensor, x: torch.Tensor, u: torch.Tensor, *, dim=-1, project=True
) -> torch.Tensor:
res = math.geodesic_unit(t, x, u, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
def lambda_x(self, x: torch.Tensor, *, dim=-1, keepdim=False) -> torch.Tensor:
return math.lambda_x(x, k=self.k, dim=dim, keepdim=keepdim)
@__scaling__(ScalingInfo(1))
def dist0(self, x: torch.Tensor, *, dim=-1, keepdim=False) -> torch.Tensor:
return math.dist0(x, k=self.k, dim=dim, keepdim=keepdim)
@__scaling__(ScalingInfo(u=-1))
def expmap0(self, u: torch.Tensor, *, dim=-1, project=True) -> torch.Tensor:
res = math.expmap0(u, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
@__scaling__(ScalingInfo(1))
def logmap0(self, x: torch.Tensor, *, dim=-1) -> torch.Tensor:
return math.logmap0(x, k=self.k, dim=dim)
def transp0(self, y: torch.Tensor, u: torch.Tensor, *, dim=-1) -> torch.Tensor:
return math.parallel_transport0(y, u, k=self.k, dim=dim)
def transp0back(self, y: torch.Tensor, u: torch.Tensor, *, dim=-1) -> torch.Tensor:
return math.parallel_transport0back(y, u, k=self.k, dim=dim)
def gyration(
self, x: torch.Tensor, y: torch.Tensor, z: torch.Tensor, *, dim=-1
) -> torch.Tensor:
return math.gyration(x, y, z, k=self.k, dim=dim)
def antipode(self, x: torch.Tensor, *, dim=-1) -> torch.Tensor:
return math.antipode(x, k=self.k, dim=dim)
@__scaling__(ScalingInfo(1))
def dist2plane(
self,
x: torch.Tensor,
p: torch.Tensor,
a: torch.Tensor,
*,
dim=-1,
keepdim=False,
signed=False,
scaled=False,
) -> torch.Tensor:
return math.dist2plane(
x, p, a, dim=dim, k=self.k, keepdim=keepdim, signed=signed, scaled=scaled
)
# this does not yet work with scaling
@__scaling__(ScalingInfo.NotCompatible)
def mobius_fn_apply(
self, fn: callable, x: torch.Tensor, *args, dim=-1, project=True, **kwargs
) -> torch.Tensor:
res = math.mobius_fn_apply(fn, x, *args, k=self.k, dim=dim, **kwargs)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
# this does not yet work with scaling
@__scaling__(ScalingInfo.NotCompatible)
def mobius_fn_apply_chain(
self, x: torch.Tensor, *fns: callable, project=True, dim=-1
) -> torch.Tensor:
res = math.mobius_fn_apply_chain(x, *fns, k=self.k, dim=dim)
if project:
return math.project(res, k=self.k, dim=dim)
else:
return res
@__scaling__(ScalingInfo(std=-1), "random")
def random_normal(
self, *size, mean=0, std=1, dtype=None, device=None
) -> "geoopt.ManifoldTensor":
"""
Create a point on the manifold, measure is induced by Normal distribution on the tangent space of zero.
Parameters
----------
size : shape
the desired shape
mean : float|tensor
mean value for the Normal distribution
std : float|tensor
std value for the Normal distribution
dtype: torch.dtype
target dtype for sample, if not None, should match Manifold dtype
device: torch.device
target device for sample, if not None, should match Manifold device
Returns
-------
ManifoldTensor
random point on the PoincareBall manifold
Notes
-----
The device and dtype will match the device and dtype of the Manifold
"""
size = size2shape(*size)
self._assert_check_shape(size, "x")
if device is not None and device != self.k.device:
raise ValueError(
"`device` does not match the manifold `device`, set the `device` argument to None"
)
if dtype is not None and dtype != self.k.dtype:
raise ValueError(
"`dtype` does not match the manifold `dtype`, set the `dtype` argument to None"
)
tens = (
torch.randn(size, device=self.k.device, dtype=self.k.dtype)
* std
/ size[-1] ** 0.5
+ mean
)
return geoopt.ManifoldTensor(self.expmap0(tens), manifold=self)
random = random_normal
def origin(
self, *size, dtype=None, device=None, seed=42
) -> "geoopt.ManifoldTensor":
"""
Zero point origin.
Parameters
----------
size : shape
the desired shape
device : torch.device
the desired device
dtype : torch.dtype
the desired dtype
seed : int
ignored
Returns
-------
ManifoldTensor
random point on the manifold
"""
return geoopt.ManifoldTensor(
torch.zeros(*size, dtype=dtype, device=device), manifold=self
)
def weighted_midpoint(
self,
xs: torch.Tensor,
weights: Optional[torch.Tensor] = None,
*,
reducedim: Optional[List[int]] = None,
dim: int = -1,
keepdim: bool = False,
lincomb: bool = False,
posweight=False,
project=True,
):
mid = math.weighted_midpoint(
xs=xs,
weights=weights,
k=self.k,
reducedim=reducedim,
dim=dim,
keepdim=keepdim,
lincomb=lincomb,
posweight=posweight,
)
if project:
return math.project(mid, k=self.k, dim=dim)
else:
return mid
def sproj(self, x: torch.Tensor, *, dim: int = -1):
return math.sproj(x, k=self.k, dim=dim)
def inv_sproj(self, x: torch.Tensor, *, dim: int = -1):
return math.inv_sproj(x, k=self.k, dim=dim)
class StereographicExact(Stereographic):
__doc__ = r"""{}
The implementation of retraction is an exact exponential map, this retraction will be used in optimization.
See Also
--------
:class:`Stereographic`
:class:`PoincareBall`
:class:`PoincareBallExact`
:class:`SphereProjection`
:class:`SphereProjectionExact`
""".format(
_stereographic_doc
)
reversible = True
retr_transp = Stereographic.expmap_transp
transp_follow_retr = Stereographic.transp_follow_expmap
retr = Stereographic.expmap
def extra_repr(self):
return "exact"
class PoincareBall(Stereographic):
__doc__ = r"""{}
See Also
--------
:class:`Stereographic`
:class:`StereographicExact`
:class:`PoincareBallExact`
:class:`SphereProjection`
:class:`SphereProjectionExact`
""".format(
_poincare_ball_doc
)
@property
def k(self):
return -self.c
@property
def c(self):
return torch.nn.functional.softplus(self.isp_c)
def __init__(self, c=1.0, learnable=False):
super().__init__(k=c, learnable=learnable)
k = self._parameters.pop("k")
with torch.no_grad():
self.isp_c = k.exp_().sub_(1).log_()
class PoincareBallExact(PoincareBall, StereographicExact):
__doc__ = r"""{}
The implementation of retraction is an exact exponential map, this retraction will be used in optimization.
See Also
--------
:class:`Stereographic`
:class:`StereographicExact`
:class:`PoincareBall`
:class:`SphereProjection`
:class:`SphereProjectionExact`
""".format(
_poincare_ball_doc
)
class SphereProjection(Stereographic):
__doc__ = r"""{}
See Also
--------
:class:`Stereographic`
:class:`StereographicExact`
:class:`PoincareBall`
:class:`PoincareBallExact`
:class:`SphereProjectionExact`
:class:`Sphere`
""".format(
_sphere_projection_doc
)
@property
def k(self):
return torch.nn.functional.softplus(self.isp_k)
def __init__(self, k=1.0, learnable=False):
super().__init__(k=k, learnable=learnable)
k = self._parameters.pop("k")
with torch.no_grad():
self.isp_k = k.exp_().sub_(1).log_()
class SphereProjectionExact(SphereProjection, StereographicExact):
__doc__ = r"""{}
The implementation of retraction is an exact exponential map, this retraction will be used in optimization.
See Also
--------
:class:`Stereographic`
:class:`StereographicExact`
:class:`PoincareBall`
:class:`PoincareBallExact`
:class:`SphereProjectionExact`
:class:`Sphere`
""".format(
_sphere_projection_doc
)