-
Notifications
You must be signed in to change notification settings - Fork 239
/
_hyperbolic.py
564 lines (461 loc) · 19.4 KB
/
_hyperbolic.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
"""Base class for the n-dimensional hyperbolic space.
The n-dimensional hyperbolic regardless its different representations.
"""
import geomstats.backend as gs
from geomstats.geometry.riemannian_metric import RiemannianMetric
class _Hyperbolic:
"""Class for the n-dimensional hyperbolic space.
Common class for the n-dimensional hyperbolic space regardless the
representation being used. This class gathers the change of coordinate
methods that are common to all representations.
This class cannot be instantiated in itself, but a public class
`Hyperbolic` is available, that returns the class that corresponds to the
chosen representation: Hyperboloid, Poincare Ball or Poincare Half-space.
Parameters
----------
dim : int
Dimension of the hyperbolic space.
point_type : str, {'extrinsic', 'intrinsic', etc}
Default coordinates to represent points in hyperbolic space.
Optional, default: 'extrinsic'.
scale : int
Scale of the hyperbolic space, defined as the set of points
in Minkowski space whose squared norm is equal to -scale.
Optional, default: 1.
"""
def __init__(self, dim, scale=1, **kwargs):
super(_Hyperbolic, self).__init__(dim=dim, **kwargs)
self.dim = dim
self.scale = scale
@staticmethod
def _extrinsic_to_extrinsic_coordinates(point):
"""Convert the parameterization of a point.
Dummy method that returns the input point.
Parameters
----------
point : array-like, shape=[..., dim + 1]
Point in hyperbolic space in extrinsic coordinates.
Returns
-------
point_extrinsic : array-like, shape=[..., dim]
Point in hyperbolic space in extrinsic coordinates.
"""
return point
@staticmethod
def _intrinsic_to_extrinsic_coordinates(point):
"""Convert intrinsic to extrinsic coordinates.
Convert the parameterization of a point in hyperbolic space
from its intrinsic coordinates, to its extrinsic coordinates
in Minkowski space.
Parameters
----------
point : array-like, shape=[..., dim]
Point in hyperbolic space in intrinsic coordinates.
Returns
-------
point_extrinsic : array-like, shape=[..., dim + 1]
Point in hyperbolic space in extrinsic coordinates.
"""
coord_0 = gs.sqrt(1.0 + gs.sum(point ** 2, axis=-1))
point_extrinsic = gs.concatenate([coord_0[..., None], point], axis=-1)
return point_extrinsic
@staticmethod
def _extrinsic_to_intrinsic_coordinates(point):
"""Convert extrinsic to intrinsic coordinates.
Convert the parameterization of a point in hyperbolic space
from its extrinsic coordinates in Minkowski space, to its
intrinsic coordinates.
Parameters
----------
point : array-like, shape=[..., dim + 1]
Point in hyperbolic space in extrinsic coordinates.
Returns
-------
point_intrinsic : array-like, shape=[..., dim]
"""
point_intrinsic = point[..., 1:]
return point_intrinsic
@staticmethod
def _extrinsic_to_ball_coordinates(point):
"""Convert extrinsic to ball coordinates.
Convert the parameterization of a point in hyperbolic space
from its intrinsic coordinates, to the Poincare ball model
coordinates.
Parameters
----------
point : array-like, shape=[..., dim + 1]
Point in hyperbolic space in extrinsic coordinates.
Returns
-------
point_ball : array-like, shape=[..., dim]
Point in hyperbolic space in Poincare ball coordinates.
"""
point_ball = point[..., 1:] / (1 + point[..., :1])
return point_ball
@staticmethod
def _ball_to_extrinsic_coordinates(point):
"""Convert ball to extrinsic coordinates.
Convert the parameterization of a point in hyperbolic space
from its Poincare ball model coordinates, to the extrinsic
coordinates.
Parameters
----------
point : array-like, shape=[..., dim]
Point in hyperbolic space in Poincare ball coordinates.
Returns
-------
point_extrinsic : array-like, shape=[..., dim + 1]
Point in hyperbolic space in extrinsic coordinates.
"""
squared_norm = gs.sum(point ** 2, -1)
denominator = 1 - squared_norm
t = (1 + squared_norm) / denominator
intrinsic = gs.einsum("...i, ...->...i", 2 * point, 1.0 / denominator)
point_extrinsic = gs.concatenate([t[..., None], intrinsic], -1)
return point_extrinsic
@classmethod
def _half_space_to_extrinsic_coordinates(cls, point):
"""Convert half-space to extrinsic coordinates.
Convert the parameterization of a point in the hyperbolic space
from its Poincare half-space coordinates, to the extrinsic
coordinates.
Parameters
----------
point : array-like, shape=[..., dim]
Point in hyperbolic space in half-space coordinates.
Returns
-------
point_extrinsic : array-like, shape=[..., dim + 1]
Point in hyperbolic space in extrinsic coordinates.
"""
point_ball = cls.half_space_to_ball_coordinates(point)
point_extrinsic = cls._ball_to_extrinsic_coordinates(point_ball)
return point_extrinsic
@classmethod
def _extrinsic_to_half_space_coordinates(cls, point):
"""Convert extrinsic to half-space coordinates.
Convert the parameterization of a point in the hyperbolic space
from its extrinsic coordinates, to the Poincare half-space
coordinates.
Parameters
----------
point : array-like, shape=[..., dim + 1]
Point in the hyperbolic space in extrinsic coordinates.
Returns
-------
point_half_space : array-like, shape=[..., dim]
Point in the hyperbolic space in half-space coordinates.
"""
point_ball = cls._extrinsic_to_ball_coordinates(point)
point_half_space = cls.ball_to_half_space_coordinates(point_ball)
return point_half_space
@staticmethod
def half_space_to_ball_coordinates(point):
"""Convert half-space to ball coordinates.
Convert the parameterization of a point in the hyperbolic space
from its Poincare half-space coordinates, to the Poincare ball
coordinates.
Parameters
----------
point : array-like, shape=[..., dim]
Point in the hyperbolic space in half-space coordinates.
Returns
-------
point_ball : array-like, shape=[..., dim]
Point in the hyperbolic space in Poincare ball coordinates.
"""
sq_norm = gs.sum(point ** 2, axis=-1)
den = 1 + sq_norm + 2 * point[..., -1]
component_1 = gs.einsum("...i,...->...i", point[..., :-1], 2.0 / den)
component_2 = (sq_norm - 1) / den
point_ball = gs.concatenate([component_1, component_2[..., None]], axis=-1)
return point_ball
@staticmethod
def ball_to_half_space_coordinates(point):
"""Convert ball to half space coordinates.
Convert the parameterization of a point in the hyperbolic space
from its Poincare ball coordinates, to the Poincare half-space
coordinates.
Parameters
----------
point : array-like, shape=[..., dim]
Point in the hyperbolic space in Poincare ball coordinates.
Returns
-------
point_ball : array-like, shape=[..., dim]
Point in the hyperbolic space in half-space coordinates.
"""
sq_norm = gs.sum(point ** 2, axis=-1)
den = 1 + sq_norm - 2 * point[..., -1]
component_1 = gs.einsum("...i,...->...i", point[..., :-1], 2.0 / den)
component_2 = (1 - sq_norm) / den
point_half_space = gs.concatenate(
[component_1, component_2[..., None]], axis=-1
)
return point_half_space
@staticmethod
def half_space_to_ball_tangent(tangent_vec, base_point):
"""Convert half-space to ball tangent coordinates.
Convert the parameterization of a tangent vector to the
hyperbolic space from its Poincare half-space coordinates, to
the Poinare ball coordinates.
Parameters
----------
tangent_vec : array-like, shape=[..., dim]
Tangent vector at the base point in the Poincare half-space.
base_point : array-like, shape=[..., dim]
Point in the Poincare half-space.
Returns
-------
tangent_vec_ball : array-like, shape=[..., dim]
Tangent vector in the Poincare ball.
"""
sq_norm = gs.sum(base_point ** 2, axis=-1)
den = 1.0 + sq_norm + 2.0 * base_point[..., -1]
scalar_prod = gs.sum(base_point * tangent_vec, axis=-1)
component_1 = gs.einsum(
"...i,...->...i", tangent_vec[..., :-1], 2.0 / den
) - 4.0 * gs.einsum(
"...i,...->...i",
base_point[..., :-1],
(scalar_prod + tangent_vec[..., -1]) / den ** 2,
)
component_2 = (
2 * scalar_prod / den
- 2 * (sq_norm - 1) * (scalar_prod + tangent_vec[..., -1]) / den ** 2
)
tangent_vec_ball = gs.concatenate(
[component_1, component_2[..., None]], axis=-1
)
return tangent_vec_ball
@staticmethod
def ball_to_half_space_tangent(tangent_vec, base_point):
"""Convert ball to half-space tangent coordinates.
Convert the parameterization of a tangent vector to the
hyperbolic space from its Poincare ball coordinates, to
the Poinare half-space coordinates.
Parameters
----------
tangent_vec : array-like, shape=[..., dim]
Tangent vector at the base point in the Poincare ball.
base_point : array-like, shape=[..., dim]
Point in the Poincare ball.
Returns
-------
tangent_vec_half_spacel : array-like, shape=[..., dim]
Tangent vector in the Poincare half-space.
"""
sq_norm = gs.sum(base_point ** 2, axis=-1)
den = 1 + sq_norm - 2 * base_point[..., -1]
scalar_prod = gs.sum(base_point * tangent_vec, -1)
component_1 = gs.einsum(
"...i,...->...i", tangent_vec[..., :-1], 2.0 / den
) - 4.0 * gs.einsum(
"...i,...->...i",
base_point[..., :-1],
(scalar_prod - tangent_vec[..., -1]) / den ** 2,
)
component_2 = (
-2.0 * scalar_prod / den
- 2 * (1.0 - sq_norm) * (scalar_prod - tangent_vec[..., -1]) / den ** 2
)
tangent_vec_half_space = gs.concatenate(
[component_1, component_2[..., None]], axis=-1
)
return tangent_vec_half_space
@staticmethod
def change_coordinates_system(
point, from_coordinates_system, to_coordinates_system
):
"""Convert coordinates of a point.
Convert the parameterization of a point in the hyperbolic space
from current given coordinate system to an other also given in
parameters. The possible coordinates system are 'extrinsic',
'intrinsic', 'ball' and 'half-plane' that correspond respectivelly
to extrinsic coordinates in the hyperboloid , intrinsic
coordinates in the hyperboloid, ball coordinates in the Poincare
ball model and coordinates in the Poincare upper half-plane model.
Parameters
----------
point : array-like, shape=[..., {dim, dim + 1}]
Point in hyperbolic space.
from_coordinates_system : str, {'extrinsic', 'intrinsic', etc}
Coordinates type.
to_coordinates_system : str, {'extrinsic', 'intrinsic', etc}
Coordinates type.
Returns
-------
point_to : array-like, shape=[..., dim]
or shape=[n_sample, dim + 1]
Point in hyperbolic space in coordinates given by to_point_type.
"""
coords_transform = {
"ball-extrinsic": _Hyperbolic._ball_to_extrinsic_coordinates,
"extrinsic-ball": _Hyperbolic._extrinsic_to_ball_coordinates,
"intrinsic-extrinsic": _Hyperbolic._intrinsic_to_extrinsic_coordinates,
"extrinsic-intrinsic": _Hyperbolic._extrinsic_to_intrinsic_coordinates,
"extrinsic-half-space": _Hyperbolic._extrinsic_to_half_space_coordinates,
"half-space-extrinsic": _Hyperbolic._half_space_to_extrinsic_coordinates,
"extrinsic-extrinsic": _Hyperbolic._extrinsic_to_extrinsic_coordinates,
}
if from_coordinates_system == to_coordinates_system:
return point
extrinsic = coords_transform[from_coordinates_system + "-extrinsic"](point)
return coords_transform["extrinsic-" + to_coordinates_system](extrinsic)
def to_coordinates(self, point, to_coords_type="ball"):
"""Convert coordinates of a point.
Convert the parameterization of a point in the hyperbolic space
from current coordinate system to the coordinate system given.
Parameters
----------
point : array-like, shape=[..., {dim, dim + 1}]
Point in hyperbolic space.
to_coords_type : str, {'extrinsic', 'intrinsic', etc}
Coordinates type.
Optional, default: 'ball'.
Returns
-------
point_to : array-like, shape=[..., {dim, dim + 1}]
Point in hyperbolic space in coordinates given by to_point_type.
"""
return _Hyperbolic.change_coordinates_system(
point, self.coords_type, to_coords_type
)
def from_coordinates(self, point, from_coords_type):
"""Convert to a type of coordinates given some type.
Convert the parameterization of a point in hyperbolic space
from given coordinate system to the current coordinate system.
Parameters
----------
point : array-like, shape=[..., {dim, dim + 1}]
Point in hyperbolic space in coordinates from_point_type.
from_coords_type : str, {'ball', 'extrinsic', 'intrinsic', ...}
Coordinates type.
Returns
-------
point_current : array-like, shape=[..., {dim, dim + 1}]
Point in hyperbolic space.
"""
return _Hyperbolic.change_coordinates_system(
point, from_coords_type, self.coords_type
)
def random_point(self, n_samples=1, bound=1.0):
"""Sample over the hyperbolic space using uniform distribution.
Sample over the hyperbolic space. The sampling is performed
by sampling over uniform distribution, the sampled examples
are considered in the intrinsic coordinates system.
The function then transforms intrinsic samples into system
coordinate selected.
Parameters
----------
n_samples : int
Number of samples.
Optional, default: 1.
bound: float
Bound defining the hypersquare in which to sample uniformly.
Optional, default: 1.
Returns
-------
samples : array-like, shape=[..., dim + 1]
Samples in hyperbolic space.
"""
size = (n_samples, self.dim)
samples = bound * 2.0 * (gs.random.rand(*size) - 0.5)
samples = _Hyperbolic.change_coordinates_system(
samples, "intrinsic", self.coords_type
)
if n_samples == 1:
samples = gs.squeeze(samples, axis=0)
return samples
class HyperbolicMetric(RiemannianMetric):
"""Class that defines operations using a hyperbolic metric.
Parameters
----------
dim : int
Dimension of the hyperbolic space.
point_type : str, {'extrinsic', 'intrinsic', etc}, optional
Default coordinates to represent points in hyperbolic space.
scale : int, optional
Scale of the hyperbolic space, defined as the set of points
in Minkowski space whose squared norm is equal to -scale.
"""
default_point_type = "vector"
default_coords_type = "extrinsic"
def __init__(self, dim, scale=1):
super(HyperbolicMetric, self).__init__(dim=dim, signature=(dim, 0))
self.point_type = HyperbolicMetric.default_point_type
self.scale = scale
def inner_product(self, tangent_vec_a, tangent_vec_b, base_point=None):
"""Compute the inner-product of two tangent vectors at a base point.
Parameters
----------
tangent_vec_a : array-like, shape=[..., dim + 1]
First tangent vector at base point.
tangent_vec_b : array-like, shape=[..., dim + 1]
Second tangent vector at base point.
base_point : array-like, shape=[..., dim + 1]
Point in hyperbolic space.
Optional, default: None.
Returns
-------
inner_prod : array-like, shape=[..., 1]
Inner-product of the two tangent vectors.
"""
inner_prod = self._inner_product(tangent_vec_a, tangent_vec_b, base_point)
inner_prod *= self.scale ** 2
return inner_prod
def squared_norm(self, vector, base_point=None):
"""Compute the squared norm of a vector.
Squared norm of a vector associated with the inner-product
at the tangent space at a base point.
Parameters
----------
vector : array-like, shape=[..., dim + 1]
Vector on the tangent space of the hyperbolic space at base point.
base_point : array-like, shape=[..., dim + 1]
Point in hyperbolic space in extrinsic coordinates.
Optional, default: None.
Returns
-------
sq_norm : array-like, shape=[..., 1]
Squared norm of the vector.
"""
sq_norm = self._squared_norm(vector)
sq_norm *= self.scale ** 2
return sq_norm
def _squared_norm(self, vector, base_point=None):
"""Squared norm with hyperbolic scale 1.
Squared norm of a vector associated with the inner-product
at the tangent space at a base point with scale=1.
Parameters
----------
vector : array-like, shape=[..., dim + 1]
Vector on the tangent space of the hyperbolic space at base point.
base_point : array-like, shape=[..., dim + 1]
Point in hyperbolic space in extrinsic coordinates.
Optional, default: None.
Returns
-------
sq_norm : array-like, shape=[..., 1]
Squared norm of the vector.
"""
return super().squared_norm(vector, base_point=base_point)
def _inner_product(self, tangent_vec_a, tangent_vec_b, base_point=None):
"""Compute the inner-product of two tangent vectors with scale=1.
Parameters
----------
tangent_vec_a : array-like, shape=[..., dim + 1]
First tangent vector at base point.
tangent_vec_b : array-like, shape=[..., dim + 1]
Second tangent vector at base point.
base_point : array-like, shape=[..., dim + 1]
Point in hyperbolic space.
Optional, default: None.
Returns
-------
inner_prod : array-like, shape=[..., 1]
Inner-product of the two tangent vectors.
"""
return super().inner_product(
tangent_vec_a, tangent_vec_b, base_point=base_point
)