Skip to content

Commit 4a28f47

Browse files
committed
Normalize custom maps in SL parametrizations
1 parent a0c03a5 commit 4a28f47

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎geotorch/sl.py‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,15 @@ def __init__(self, size, f="softplus", triv="expm"):
3030
super().__init__(size, SL.parse_f(f), triv)
3131

3232
@staticmethod
33-
def parse_f(f_name):
34-
if f_name in FixedRank.fs.keys():
35-
f, inv = FixedRank.parse_f(f_name)
36-
37-
def f_sl(x):
38-
y = f(x)
39-
log_y = y.log()
40-
return (log_y - log_y.mean(dim=-1, keepdim=True)).exp()
41-
42-
return (f_sl, inv)
43-
else:
44-
return f_name
33+
def parse_f(f):
34+
f, inv = FixedRank.parse_f(f)
35+
36+
def f_sl(x):
37+
y = f(x)
38+
log_y = y.log()
39+
return (log_y - log_y.mean(dim=-1, keepdim=True)).exp()
40+
41+
return f_sl, inv
4542

4643
def in_manifold_singular_values(self, S, eps=5e-3):
4744
rank_eps = torch.finfo(S.dtype).eps * max(self.n, self.k)

‎test/test_sl.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ def test_singular_values_are_normalized_in_log_domain(self):
2626
)
2727
)
2828

29+
def test_custom_map_is_normalized(self):
30+
manifold = SL(size=(2, 3, 3), f=(torch.exp, torch.log)).double()
31+
X = torch.randn(2, 3, 3, dtype=torch.float64)
32+
matrix = manifold(X)
33+
sign, logabsdet = torch.linalg.slogdet(matrix)
34+
35+
self.assertTrue(torch.equal(sign, torch.ones_like(sign)))
36+
torch.testing.assert_close(
37+
logabsdet, torch.zeros_like(logabsdet), atol=1e-6, rtol=0
38+
)
39+
40+
inverse = manifold.right_inverse(matrix)
41+
torch.testing.assert_close(manifold(inverse), matrix)
42+
2943
def test_negative_determinant_is_rejected(self):
3044
manifold = SL(size=(2, 2))
3145
self.assertFalse(manifold.in_manifold(torch.diag(torch.tensor([-1.0, 1.0]))))

0 commit comments

Comments
 (0)