-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
84 lines (63 loc) · 2.43 KB
/
utils.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
from typing import Any
import torch
from torch.autograd import Function
class Atanh(Function):
eps = 1e-5
@staticmethod
def forward(ctx: Any, x: Any) -> Any:
x = torch.clamp(x, min=-1 + Atanh.eps, max=1 - Atanh.eps)
ctx.save_for_backward(x)
return 0.5 * torch.log((1 + x) / (1 - x))
@staticmethod
def backward(ctx: Any, grad_output: Any) -> Any:
x, = ctx.saved_tensors
return grad_output / (1 - (x * x)) if ctx.needs_input_grad[0] else None
class Asinh(Function):
eps = 1e-5
@staticmethod
def forward(ctx: Any, x: Any) -> Any:
sqrt_x = torch.sqrt(x * x + 1)
ctx.save_for_backward(sqrt_x)
return torch.log(torch.clamp(sqrt_x + x, min=Asinh.eps))
@staticmethod
def backward(ctx: Any, grad_output: Any) -> Any:
x, = ctx.saved_tensors
return grad_output / x if ctx.needs_input_grad[0] else None
class Acosh(Function):
eps = 1e-5
@staticmethod
def forward(ctx: Any, x: Any) -> Any:
x = torch.clamp(x, min=1 + Acosh.eps)
sqrt_x = torch.sqrt(x * x - 1)
ctx.save_for_backward(sqrt_x)
return torch.log(sqrt_x)
@staticmethod
def backward(ctx: Any, grad_output: Any) -> Any:
x, = ctx.saved_tensors
return grad_output / x if ctx.needs_input_grad[0] else None
def lorentz_2_poincare(x: torch.Tensor,
curvature: float,
dim: int = -1) -> torch.Tensor:
'''
Diffeomorphism maps Hyperboloid onto Poincare Ball
x : Tensor - point on the Hyperboloid
curvature: float - manifold curvature
dim : int - reduction dimension for operations
'''
curvature = torch.as_tensor(curvature)
d = x.size(dim) - 1
return x.narrow(dim, 1, d) / (x.narrow(-dim, 0, 1) + torch.sqrt(curvature))
def poincare_2_lorentz(x: torch.Tensor,
curvature: float,
dim: int = -1,
eps: float = 1e-6) -> torch.Tensor:
'''
Diffeomorphism maps Poincare Ball onto Hyperboloid
x : Tensor - point on the Poincare Ball
curvature: float - manifold curvature
dim : int - reduction dimension for operations
'''
curvature = torch.as_tensor(curvature)
norm_squared = torch.sum(x * x, dim=dim, keepdim=True)
res = torch.sqrt(curvature) * torch.cat((1 + norm_squared, 2 * x), dim=dim)
return res / (1.0 - norm_squared + eps)