-
Notifications
You must be signed in to change notification settings - Fork 239
/
test_hyperbolic_space.py
412 lines (332 loc) · 15.4 KB
/
test_hyperbolic_space.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
"""
Unit tests for the Hyperbolic space.
"""
import math
import numpy as np
import geomstats.backend as gs
import geomstats.tests
import tests.helper as helper
from geomstats.hyperbolic_space import HyperbolicSpace
from geomstats.minkowski_space import MinkowskiSpace
# Tolerance for errors on predicted vectors, relative to the *norm*
# of the vector, as opposed to the standard behavior of gs.allclose
# where it is relative to each element of the array
RTOL = 1e-6
class TestHyperbolicSpaceMethods(geomstats.tests.TestCase):
_multiprocess_can_split_ = True
def setUp(self):
gs.random.seed(1234)
self.dimension = 3
self.space = HyperbolicSpace(dimension=self.dimension)
self.metric = self.space.metric
self.n_samples = 10
def test_random_uniform_and_belongs(self):
point = self.space.random_uniform()
result = self.space.belongs(point)
expected = gs.array([[True]])
self.assertAllClose(result, expected)
def test_random_uniform(self):
result = self.space.random_uniform()
self.assertAllClose(gs.shape(result), (1, self.dimension + 1))
def test_intrinsic_and_extrinsic_coords(self):
"""
Test that the composition of
intrinsic_to_extrinsic_coords and
extrinsic_to_intrinsic_coords
gives the identity.
"""
point_int = gs.ones(self.dimension)
point_ext = self.space.intrinsic_to_extrinsic_coords(point_int)
result = self.space.extrinsic_to_intrinsic_coords(point_ext)
expected = point_int
expected = helper.to_vector(expected)
self.assertAllClose(result, expected)
point_ext = gs.array([2.0, 1.0, 1.0, 1.0])
point_int = self.space.extrinsic_to_intrinsic_coords(point_ext)
result = self.space.intrinsic_to_extrinsic_coords(point_int)
expected = point_ext
expected = helper.to_vector(expected)
self.assertAllClose(result, expected)
def test_intrinsic_and_extrinsic_coords_vectorization(self):
"""
Test that the composition of
intrinsic_to_extrinsic_coords and
extrinsic_to_intrinsic_coords
gives the identity.
"""
point_int = gs.array([[.1, 0., 0., .1, 0., 0.],
[.1, .1, .1, .4, .1, 0.],
[.1, .3, 0., .1, 0., 0.],
[-0.1, .1, -.4, .1, -.01, 0.],
[0., 0., .1, .1, -0.08, -0.1],
[.1, .1, .1, .1, 0., -0.5]])
point_ext = self.space.intrinsic_to_extrinsic_coords(point_int)
result = self.space.extrinsic_to_intrinsic_coords(point_ext)
expected = point_int
expected = helper.to_vector(expected)
self.assertAllClose(result, expected)
point_ext = gs.array([[2., 1., 1., 1.],
[4., 1., 3., math.sqrt(5.)],
[3., 2., 0., 2.]])
point_int = self.space.extrinsic_to_intrinsic_coords(point_ext)
result = self.space.intrinsic_to_extrinsic_coords(point_int)
expected = point_ext
expected = helper.to_vector(expected)
self.assertAllClose(result, expected)
def test_log_and_exp_general_case(self):
"""
Test that the riemannian exponential
and the riemannian logarithm are inverse.
Expect their composition to give the identity function.
"""
# Riemannian Log then Riemannian Exp
# General case
base_point = gs.array([4.0, 1., 3.0, math.sqrt(5.)])
point = gs.array([2.0, 1.0, 1.0, 1.0])
log = self.metric.log(point=point, base_point=base_point)
result = self.metric.exp(tangent_vec=log, base_point=base_point)
expected = helper.to_vector(point)
self.assertAllClose(result, expected)
def test_exp_and_belongs(self):
H2 = HyperbolicSpace(dimension=2)
METRIC = H2.metric
base_point = gs.array([1., 0., 0.])
with self.session():
self.assertTrue(gs.eval(H2.belongs(base_point)))
tangent_vec = H2.projection_to_tangent_space(
vector=gs.array([1., 2., 1.]),
base_point=base_point)
exp = METRIC.exp(tangent_vec=tangent_vec,
base_point=base_point)
with self.session():
self.assertTrue(gs.eval(H2.belongs(exp)))
def test_exp_vectorization(self):
n_samples = 3
dim = self.dimension + 1
one_vec = gs.array([2.0, 1.0, 1.0, 1.0])
one_base_point = gs.array([4.0, 3., 1.0, math.sqrt(5)])
n_vecs = gs.array([[2., 1., 1., 1.],
[4., 1., 3., math.sqrt(5.)],
[3., 2., 0., 2.]])
n_base_points = gs.array([
[2.0, 0.0, 1.0, math.sqrt(2)],
[5.0, math.sqrt(8), math.sqrt(8), math.sqrt(8)],
[1.0, 0.0, 0.0, 0.0]])
one_tangent_vec = self.space.projection_to_tangent_space(
one_vec, base_point=one_base_point)
result = self.metric.exp(one_tangent_vec, one_base_point)
self.assertAllClose(gs.shape(result), (1, dim))
n_tangent_vecs = self.space.projection_to_tangent_space(
n_vecs, base_point=one_base_point)
result = self.metric.exp(n_tangent_vecs, one_base_point)
self.assertAllClose(gs.shape(result), (n_samples, dim))
expected = np.zeros((n_samples, dim))
with self.session():
for i in range(n_samples):
expected[i] = gs.eval(
self.metric.exp(n_tangent_vecs[i], one_base_point))
expected = helper.to_vector(gs.array(expected))
self.assertAllClose(result, expected)
one_tangent_vec = self.space.projection_to_tangent_space(
one_vec, base_point=n_base_points)
result = self.metric.exp(one_tangent_vec, n_base_points)
self.assertAllClose(gs.shape(result), (n_samples, dim))
expected = np.zeros((n_samples, dim))
with self.session():
for i in range(n_samples):
expected[i] = gs.eval(self.metric.exp(one_tangent_vec[i],
n_base_points[i]))
expected = helper.to_vector(gs.array(expected))
self.assertAllClose(result, expected)
n_tangent_vecs = self.space.projection_to_tangent_space(
n_vecs, base_point=n_base_points)
result = self.metric.exp(n_tangent_vecs, n_base_points)
self.assertAllClose(gs.shape(result), (n_samples, dim))
expected = np.zeros((n_samples, dim))
with self.session():
for i in range(n_samples):
expected[i] = gs.eval(self.metric.exp(n_tangent_vecs[i],
n_base_points[i]))
expected = helper.to_vector(gs.array(expected))
self.assertAllClose(result, expected)
def test_log_vectorization(self):
n_samples = 3
dim = self.dimension + 1
one_point = gs.array([2.0, 1.0, 1.0, 1.0])
one_base_point = gs.array([4.0, 3., 1.0, math.sqrt(5)])
n_points = gs.array([[2.0, 1.0, 1.0, 1.0],
[4.0, 1., 3.0, math.sqrt(5)],
[3.0, 2.0, 0.0, 2.0]])
n_base_points = gs.array([
[2.0, 0.0, 1.0, math.sqrt(2)],
[5.0, math.sqrt(8), math.sqrt(8), math.sqrt(8)],
[1.0, 0.0, 0.0, 0.0]])
result = self.metric.log(one_point, one_base_point)
self.assertAllClose(gs.shape(result), (1, dim))
result = self.metric.log(n_points, one_base_point)
self.assertAllClose(gs.shape(result), (n_samples, dim))
result = self.metric.log(one_point, n_base_points)
self.assertAllClose(gs.shape(result), (n_samples, dim))
result = self.metric.log(n_points, n_base_points)
self.assertAllClose(gs.shape(result), (n_samples, dim))
def test_inner_product(self):
"""
Test that the inner product between two tangent vectors
is the Minkowski inner product.
"""
minkowski_space = MinkowskiSpace(self.dimension+1)
base_point = gs.array(
[1.16563816, 0.36381045, -0.47000603, 0.07381469])
tangent_vec_a = self.space.projection_to_tangent_space(
vector=gs.array([10., 200., 1., 1.]),
base_point=base_point)
tangent_vec_b = self.space.projection_to_tangent_space(
vector=gs.array([11., 20., -21., 0.]),
base_point=base_point)
result = self.metric.inner_product(
tangent_vec_a, tangent_vec_b, base_point)
expected = minkowski_space.metric.inner_product(
tangent_vec_a, tangent_vec_b, base_point)
with self.session():
self.assertAllClose(result, expected)
def test_squared_norm_and_squared_dist(self):
"""
Test that the squared distance between two points is
the squared norm of their logarithm.
"""
point_a = gs.array([2.0, 1.0, 1.0, 1.0])
point_b = gs.array([4.0, 1., 3.0, math.sqrt(5)])
log = self.metric.log(point=point_a, base_point=point_b)
result = self.metric.squared_norm(vector=log)
expected = self.metric.squared_dist(point_a, point_b)
with self.session():
self.assertAllClose(result, expected)
def test_norm_and_dist(self):
"""
Test that the distance between two points is
the norm of their logarithm.
"""
point_a = gs.array([2.0, 1.0, 1.0, 1.0])
point_b = gs.array([4.0, 1., 3.0, math.sqrt(5)])
log = self.metric.log(point=point_a, base_point=point_b)
result = self.metric.norm(vector=log)
expected = self.metric.dist(point_a, point_b)
with self.session():
self.assertAllClose(result, expected)
def test_log_and_exp_edge_case(self):
"""
Test that the riemannian exponential
and the riemannian logarithm are inverse.
Expect their composition to give the identity function.
"""
# Riemannian Log then Riemannian Exp
# Edge case: two very close points, base_point_2 and point_2,
# form an angle < epsilon
base_point_intrinsic = gs.array([1., 2., 3.])
base_point = self.space.intrinsic_to_extrinsic_coords(
base_point_intrinsic)
point_intrinsic = (base_point_intrinsic
+ 1e-12 * gs.array([-1., -2., 1.]))
point = self.space.intrinsic_to_extrinsic_coords(
point_intrinsic)
log = self.metric.log(point=point, base_point=base_point)
result = self.metric.exp(tangent_vec=log, base_point=base_point)
expected = point
with self.session():
self.assertAllClose(result, expected)
def test_exp_and_log_and_projection_to_tangent_space_general_case(self):
"""
Test that the riemannian exponential
and the riemannian logarithm are inverse.
Expect their composition to give the identity function.
"""
# Riemannian Exp then Riemannian Log
# General case
base_point = gs.array([4.0, 1., 3.0, math.sqrt(5)])
vector = gs.array([2.0, 1.0, 1.0, 1.0])
vector = self.space.projection_to_tangent_space(
vector=vector,
base_point=base_point)
exp = self.metric.exp(tangent_vec=vector, base_point=base_point)
result = self.metric.log(point=exp, base_point=base_point)
expected = vector
with self.session():
self.assertAllClose(result, expected)
def test_dist(self):
# Distance between a point and itself is 0.
point_a = gs.array([4.0, 1., 3.0, math.sqrt(5)])
point_b = point_a
result = self.metric.dist(point_a, point_b)
expected = gs.array([[0]])
with self.session():
self.assertAllClose(result, expected)
def test_exp_and_dist_and_projection_to_tangent_space(self):
base_point = gs.array([4.0, 1., 3.0, math.sqrt(5)])
vector = gs.array([0.001, 0., -.00001, -.00003])
tangent_vec = self.space.projection_to_tangent_space(
vector=vector,
base_point=base_point)
exp = self.metric.exp(tangent_vec=tangent_vec,
base_point=base_point)
result = self.metric.dist(base_point, exp)
sq_norm = self.metric.embedding_metric.squared_norm(
tangent_vec)
expected = sq_norm
with self.session():
self.assertAllClose(result, expected, atol=1e-2)
def test_geodesic_and_belongs(self):
# TODO(nina): Fix this tests, as it fails when geodesic goes "too far"
initial_point = gs.array([4.0, 1., 3.0, math.sqrt(5)])
n_geodesic_points = 100
vector = gs.array([1., 0., 0., 0.])
initial_tangent_vec = self.space.projection_to_tangent_space(
vector=vector,
base_point=initial_point)
geodesic = self.metric.geodesic(
initial_point=initial_point,
initial_tangent_vec=initial_tangent_vec)
t = gs.linspace(start=0., stop=1., num=n_geodesic_points)
points = geodesic(t)
result = self.space.belongs(points)
expected = gs.array(n_geodesic_points * [[True]])
with self.session():
self.assertAllClose(expected, result)
def test_exp_and_log_and_projection_to_tangent_space_edge_case(self):
"""
Test that the riemannian exponential
and the riemannian logarithm are inverse.
Expect their composition to give the identity function.
"""
# Riemannian Exp then Riemannian Log
# Edge case: tangent vector has norm < epsilon
base_point = gs.array([2., 1., 1., 1.])
vector = 1e-10 * gs.array([.06, -51., 6., 5.])
exp = self.metric.exp(tangent_vec=vector, base_point=base_point)
result = self.metric.log(point=exp, base_point=base_point)
expected = self.space.projection_to_tangent_space(
vector=vector,
base_point=base_point)
self.assertAllClose(result, expected, atol=1e-8)
@geomstats.tests.np_only
def test_variance(self):
point = gs.array([2., 1., 1., 1.])
result = self.metric.variance([point, point])
expected = 0.
self.assertAllClose(result, expected)
@geomstats.tests.np_only
def test_mean(self):
point = gs.array([2., 1., 1., 1.])
result = self.metric.mean([point, point])
expected = point
self.assertAllClose(result, expected)
@geomstats.tests.np_only
def test_mean_and_belongs(self):
point_a = self.space.random_uniform()
point_b = self.space.random_uniform()
point_c = self.space.random_uniform()
mean = self.metric.mean([point_a, point_b, point_c])
result = self.space.belongs(mean)
expected = gs.array([[True]])
self.assertAllClose(result, expected)
if __name__ == '__main__':
geomstats.tests.main()