mccvae 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. mccvae/__init__.py +5 -0
  2. mccvae/agent.py +288 -0
  3. mccvae/distributions/EuclideanNormal/__init__.py +15 -0
  4. mccvae/distributions/EuclideanNormal/arguments.py +3 -0
  5. mccvae/distributions/EuclideanNormal/distribution.py +26 -0
  6. mccvae/distributions/EuclideanNormal/layers.py +35 -0
  7. mccvae/distributions/EuclideanNormal/prior.py +18 -0
  8. mccvae/distributions/HWNormal/__init__.py +15 -0
  9. mccvae/distributions/HWNormal/arguments.py +3 -0
  10. mccvae/distributions/HWNormal/distribution.py +56 -0
  11. mccvae/distributions/HWNormal/layers.py +44 -0
  12. mccvae/distributions/HWNormal/prior.py +24 -0
  13. mccvae/distributions/LearnablePGMNormal/__init__.py +24 -0
  14. mccvae/distributions/LearnablePGMNormal/arguments.py +4 -0
  15. mccvae/distributions/LearnablePGMNormal/distribution.py +81 -0
  16. mccvae/distributions/LearnablePGMNormal/layers.py +218 -0
  17. mccvae/distributions/LearnablePGMNormal/prior.py +18 -0
  18. mccvae/distributions/PGMNormal/__init__.py +24 -0
  19. mccvae/distributions/PGMNormal/arguments.py +4 -0
  20. mccvae/distributions/PGMNormal/distribution.py +71 -0
  21. mccvae/distributions/PGMNormal/layers.py +82 -0
  22. mccvae/distributions/PGMNormal/prior.py +18 -0
  23. mccvae/distributions/PoincareNormal/__init__.py +16 -0
  24. mccvae/distributions/PoincareNormal/arguments.py +4 -0
  25. mccvae/distributions/PoincareNormal/ars.py +133 -0
  26. mccvae/distributions/PoincareNormal/distribution.py +58 -0
  27. mccvae/distributions/PoincareNormal/hyperbolic_radius.py +371 -0
  28. mccvae/distributions/PoincareNormal/hyperbolic_uniform.py +43 -0
  29. mccvae/distributions/PoincareNormal/layers.py +48 -0
  30. mccvae/distributions/PoincareNormal/prior.py +18 -0
  31. mccvae/distributions/__init__.py +0 -0
  32. mccvae/distributions/utils.py +21 -0
  33. mccvae/environment.py +361 -0
  34. mccvae/mixin.py +671 -0
  35. mccvae/model.py +480 -0
  36. mccvae/module.py +519 -0
  37. mccvae/palantir/__init__.py +28 -0
  38. mccvae/palantir/core.py +289 -0
  39. mccvae/palantir/diffusion.py +254 -0
  40. mccvae/palantir/magic.py +50 -0
  41. mccvae/palantir/pseudotime.py +431 -0
  42. mccvae/palantir/test_palantir.py +140 -0
  43. mccvae/palantir/utils.py +193 -0
  44. mccvae/unified_models/__init__.py +67 -0
  45. mccvae/unified_models/base_model.py +361 -0
  46. mccvae/unified_models/cellblast_model.py +349 -0
  47. mccvae/unified_models/clear_model.py +258 -0
  48. mccvae/unified_models/gmvae_model.py +560 -0
  49. mccvae/unified_models/scagcl_model.py +486 -0
  50. mccvae/unified_models/scalex_model.py +352 -0
  51. mccvae/unified_models/scdac_model.py +269 -0
  52. mccvae/unified_models/scdeepcluster_model.py +300 -0
  53. mccvae/unified_models/scdhmap_model.py +454 -0
  54. mccvae/unified_models/scdiffusion_model.py +479 -0
  55. mccvae/unified_models/scgcc_model.py +374 -0
  56. mccvae/unified_models/scgnn_model.py +210 -0
  57. mccvae/unified_models/scgpcl_model.py +665 -0
  58. mccvae/unified_models/schsc_model.py +626 -0
  59. mccvae/unified_models/scsimgcl_model.py +471 -0
  60. mccvae/unified_models/scsmd_model.py +387 -0
  61. mccvae/unified_models/sivae_model.py +460 -0
  62. mccvae/utils.py +228 -0
  63. mccvae-0.0.1.dist-info/METADATA +185 -0
  64. mccvae-0.0.1.dist-info/RECORD +67 -0
  65. mccvae-0.0.1.dist-info/WHEEL +5 -0
  66. mccvae-0.0.1.dist-info/licenses/LICENSE +21 -0
  67. mccvae-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,218 @@
1
+ import torch
2
+ import geoopt
3
+ from torch import nn
4
+ from torch.nn import functional as F
5
+
6
+
7
+ # ============================================================================
8
+ # Hyperbolic Geometry Coordinate Transformations
9
+ # Replacing dgnn.utils functions with standalone implementations
10
+ # ============================================================================
11
+
12
+ def halfplane2disk(x, c):
13
+ """
14
+ Convert from Poincaré half-plane to Poincaré disk model
15
+
16
+ Half-plane: {(x1, x2) | x2 > 0}
17
+ Disk: {x | ||x||^2 < 1/c}
18
+
19
+ Args:
20
+ x: [..., 2] tensor, half-plane coordinates (x1, x2 with x2 > 0)
21
+ c: curvature parameter (positive scalar)
22
+
23
+ Returns:
24
+ [..., 2] tensor, disk coordinates
25
+ """
26
+ x1, x2 = x[..., 0:1], x[..., 1:2]
27
+
28
+ # Transformation formula
29
+ denominator = x1.pow(2) + (x2 + 1).pow(2)
30
+ u1 = 2 * x1 / denominator
31
+ u2 = (x1.pow(2) + x2.pow(2) - 1) / denominator
32
+
33
+ disk_coords = torch.cat([u1, u2], dim=-1)
34
+
35
+ # Scale by curvature
36
+ disk_coords = disk_coords * torch.sqrt(c)
37
+
38
+ return disk_coords
39
+
40
+
41
+ def disk2halfplane(x, c):
42
+ """
43
+ Convert from Poincaré disk to Poincaré half-plane model
44
+
45
+ Inverse of halfplane2disk
46
+
47
+ Args:
48
+ x: [..., 2] tensor, disk coordinates
49
+ c: curvature parameter (positive scalar)
50
+
51
+ Returns:
52
+ [..., 2] tensor, half-plane coordinates
53
+ """
54
+ # Unscale by curvature
55
+ x = x / torch.sqrt(c)
56
+
57
+ u1, u2 = x[..., 0:1], x[..., 1:2]
58
+
59
+ # Transformation formula
60
+ denominator = u1.pow(2) + (1 - u2).pow(2)
61
+ x1 = 2 * u1 / denominator
62
+ x2 = (1 - u1.pow(2) - u2.pow(2)) / denominator
63
+
64
+ halfplane_coords = torch.cat([x1, x2], dim=-1)
65
+
66
+ # Ensure x2 > 0 (numerical stability)
67
+ halfplane_coords[..., 1:2] = torch.clamp(halfplane_coords[..., 1:2], min=1e-7)
68
+
69
+ return halfplane_coords
70
+
71
+
72
+ def disk2lorentz(x, c):
73
+ """
74
+ Convert from Poincaré disk to Lorentz (hyperboloid) model
75
+
76
+ Disk: {x | ||x||^2 < 1/c}
77
+ Lorentz: {x | <x, x>_L = -1/c, x0 > 0}
78
+
79
+ Args:
80
+ x: [..., n] tensor, disk coordinates
81
+ c: curvature parameter (positive scalar)
82
+
83
+ Returns:
84
+ [..., n+1] tensor, Lorentz coordinates
85
+ """
86
+ # Unscale by curvature
87
+ x = x / torch.sqrt(c)
88
+
89
+ x_norm_sq = (x ** 2).sum(dim=-1, keepdim=True)
90
+
91
+ # Formula: (1 + ||x||^2) / (1 - ||x||^2), 2x / (1 - ||x||^2)
92
+ x0 = (1 + x_norm_sq) / (1 - x_norm_sq + 1e-7)
93
+ x_rest = 2 * x / (1 - x_norm_sq + 1e-7)
94
+
95
+ lorentz_coords = torch.cat([x0, x_rest], dim=-1)
96
+
97
+ # Scale by curvature
98
+ lorentz_coords = lorentz_coords / torch.sqrt(c)
99
+
100
+ return lorentz_coords
101
+
102
+
103
+ def lorentz2disk(x, c):
104
+ """
105
+ Convert from Lorentz (hyperboloid) to Poincaré disk model
106
+
107
+ Inverse of disk2lorentz
108
+
109
+ Args:
110
+ x: [..., n+1] tensor, Lorentz coordinates
111
+ c: curvature parameter (positive scalar)
112
+
113
+ Returns:
114
+ [..., n] tensor, disk coordinates
115
+ """
116
+ # Unscale by curvature
117
+ x = x * torch.sqrt(c)
118
+
119
+ x0 = x[..., 0:1]
120
+ x_rest = x[..., 1:]
121
+
122
+ # Formula: x_rest / (1 + x0)
123
+ disk_coords = x_rest / (1 + x0 + 1e-7)
124
+
125
+ # Scale by curvature
126
+ disk_coords = disk_coords * torch.sqrt(c)
127
+
128
+ return disk_coords
129
+
130
+
131
+ # ============================================================================
132
+ # Original Encoder/Decoder Layers
133
+ # ============================================================================
134
+
135
+ class EncoderLayer(nn.Module):
136
+ def __init__(self, args, feature_dim) -> None:
137
+ super().__init__()
138
+
139
+ self.latent_dim = args.latent_dim
140
+ self.feature_dim = feature_dim
141
+
142
+ self.variational = nn.Linear(
143
+ self.feature_dim,
144
+ 4 * self.latent_dim
145
+ )
146
+
147
+ def forward(self, feature):
148
+ feature = self.variational(feature)
149
+ alpha, beta, logc, gamma = torch.split(
150
+ feature,
151
+ [
152
+ self.latent_dim,
153
+ self.latent_dim,
154
+ self.latent_dim,
155
+ self.latent_dim
156
+ ],
157
+ dim=-1
158
+ )
159
+
160
+ return torch.stack([alpha, beta, logc], dim=-1), gamma
161
+
162
+
163
+ class VanillaEncoderLayer(nn.Module):
164
+ def __init__(self, args, feature_dim) -> None:
165
+ super().__init__()
166
+
167
+ self.encoder = EncoderLayer(args, feature_dim)
168
+
169
+ def forward(self, feature):
170
+ return self.encoder(feature)
171
+
172
+
173
+ class ExpEncoderLayer(nn.Module):
174
+ def __init__(self, args, feature_dim) -> None:
175
+ super().__init__()
176
+
177
+ self.c = torch.tensor([args.c], device=args.device)
178
+
179
+ self.manifold = geoopt.manifolds.PoincareBall(-args.c)
180
+ self.encoder = EncoderLayer(args, feature_dim)
181
+
182
+ def forward(self, feature):
183
+ mean, gamma = self.encoder(feature)
184
+ mean = disk2halfplane(
185
+ self.manifold.expmap0(mean),
186
+ self.c
187
+ )
188
+ mean = torch.stack([mean[..., 0], mean[..., 1].log() * 2], dim=-1)
189
+
190
+ return mean, gamma
191
+
192
+
193
+ class VanillaDecoderLayer(nn.Module):
194
+ def __init__(self, args) -> None:
195
+ super().__init__()
196
+
197
+ def forward(self, z):
198
+ a, b = z[..., 0], (z[..., 1] * 0.5).exp()
199
+ z = torch.concat([a, b], dim=-1)
200
+ return z
201
+
202
+
203
+ class LogDecoderLayer(nn.Module):
204
+ def __init__(self, args) -> None:
205
+ super().__init__()
206
+
207
+ self.c = torch.tensor([args.c], device=args.device)
208
+
209
+ self.manifold = geoopt.manifolds.PoincareBall(-args.c)
210
+
211
+ def forward(self, z):
212
+ a, b = z[..., 0], (z[..., 1] * 0.5).exp()
213
+ z = torch.stack([a, b], dim=-1)
214
+ z = self.manifold.logmap0(
215
+ halfplane2disk(z, self.c)
216
+ )
217
+
218
+ return z.reshape(*z.shape[:-2], -1)
@@ -0,0 +1,18 @@
1
+ import torch
2
+
3
+ from .distribution import Distribution
4
+
5
+
6
+ def get_prior(args):
7
+ mean = torch.zeros(
8
+ [1, args.latent_dim, 2],
9
+ device=args.device
10
+ )
11
+ covar = torch.zeros(
12
+ [1, args.latent_dim],
13
+ device=args.device
14
+ )
15
+
16
+ prior = Distribution(mean, covar)
17
+ return prior
18
+
@@ -0,0 +1,24 @@
1
+ """
2
+ Pseudo-Gaussian Mixture Normal Distribution
3
+ Gaussian mixture approximation on hyperbolic space using Poincaré half-plane model
4
+ """
5
+
6
+ from .distribution import Distribution
7
+ from .layers import (
8
+ EncoderLayer,
9
+ VanillaEncoderLayer,
10
+ GeoEncoderLayer,
11
+ VanillaDecoderLayer,
12
+ GeoDecoderLayer
13
+ )
14
+ from .prior import get_prior
15
+
16
+ __all__ = [
17
+ 'Distribution',
18
+ 'EncoderLayer',
19
+ 'VanillaEncoderLayer',
20
+ 'GeoEncoderLayer',
21
+ 'VanillaDecoderLayer',
22
+ 'GeoDecoderLayer',
23
+ 'get_prior',
24
+ ]
@@ -0,0 +1,4 @@
1
+ def add_distribution_args(parser):
2
+ group = parser.add_argument_group('PGMNormal')
3
+ group.add_argument('--c', type=float, default=-1.)
4
+ group.add_argument('--layer', type=str, choices=['Vanilla', 'Geo'], default='Vanilla')
@@ -0,0 +1,71 @@
1
+ import torch
2
+ from math import log, sqrt, pi
3
+ from torch.distributions import Normal, Gamma
4
+
5
+ from ..utils import euclidean_kl_div, gamma_kl_div
6
+
7
+
8
+ class Distribution():
9
+ def __init__(self, means, log_gamma_square, c=-1) -> None:
10
+ self.c = torch.tensor([c], device=means.device)
11
+ self.alpha = means[..., 0]
12
+ self.log_beta_square = means[..., 1]
13
+ self.log_gamma_square = log_gamma_square
14
+
15
+ self.normal_mu = self.alpha
16
+ self.normal_logvar = self.log_beta_square + self.log_gamma_square
17
+ self.base1 = Normal(
18
+ self.normal_mu,
19
+ (0.5 * self.normal_logvar).exp()
20
+ )
21
+
22
+ self.gamma_a = (-self.log_gamma_square).exp() / (4 * -self.c) + 1
23
+ self.log_gamma_b = -self.normal_logvar - log(4 * -self.c)
24
+ self.base2 = Gamma(
25
+ self.gamma_a,
26
+ self.log_gamma_b.exp()
27
+ )
28
+
29
+ def log_prob(self, z):
30
+ target_mu, target_logvar = z[..., 0], z[..., 1]
31
+
32
+ kl = euclidean_kl_div(
33
+ sqrt(2 * -self.c) * target_mu,
34
+ target_logvar,
35
+ sqrt(2 * -self.c) * self.alpha,
36
+ self.log_beta_square
37
+ )
38
+
39
+ log_prob = -kl / (2 * -self.c * self.log_gamma_square.exp()) + 1.5 * (target_logvar - self.log_beta_square)
40
+ gamma_factor = (-self.log_gamma_square).exp() / (4 * -self.c)
41
+ log_prob = log_prob - 0.5 * self.log_gamma_square
42
+ log_prob = log_prob - torch.lgamma(gamma_factor)
43
+ log_prob = log_prob - gamma_factor
44
+ log_prob = log_prob - gamma_factor * ((4 * -self.c).log() + self.log_gamma_square)
45
+ return log_prob
46
+
47
+ def rsample(self, N):
48
+ sample_mean = self.base1.rsample([N])
49
+ sample_shape = torch.Size([N]) + self.gamma_a.shape
50
+ sample_logvar = torch._standard_gamma(self.gamma_a[None].expand(sample_shape)).log() - self.log_gamma_b[None]
51
+ return torch.stack([sample_mean, sample_logvar], dim=-1)
52
+
53
+ def sample(self, N):
54
+ with torch.no_grad():
55
+ return self.rsample(N)
56
+
57
+ def kl_div(self, target_dist):
58
+ kl1 = euclidean_kl_div(
59
+ self.normal_mu,
60
+ self.normal_logvar,
61
+ target_dist.normal_mu,
62
+ target_dist.normal_logvar
63
+ )
64
+ kl2 = gamma_kl_div(
65
+ self.gamma_a,
66
+ self.log_gamma_b,
67
+ target_dist.gamma_a,
68
+ target_dist.log_gamma_b
69
+ )
70
+ return kl1 + kl2
71
+
@@ -0,0 +1,82 @@
1
+ import torch
2
+ import geoopt
3
+ from torch import nn
4
+ from torch.nn import functional as F
5
+
6
+
7
+ class EncoderLayer(nn.Module):
8
+ def __init__(self, args, feature_dim) -> None:
9
+ super().__init__()
10
+
11
+ self.latent_dim = args.latent_dim
12
+ self.feature_dim = feature_dim
13
+
14
+ self.variational = nn.Linear(
15
+ self.feature_dim,
16
+ 3 * self.latent_dim
17
+ )
18
+
19
+ def forward(self, feature):
20
+ feature = self.variational(feature)
21
+ alpha, beta, gamma = torch.split(
22
+ feature,
23
+ [self.latent_dim, self.latent_dim, self.latent_dim],
24
+ dim=-1
25
+ )
26
+
27
+ return torch.stack([alpha, beta], dim=-1), gamma
28
+
29
+
30
+ class VanillaEncoderLayer(nn.Module):
31
+ def __init__(self, args, feature_dim) -> None:
32
+ super().__init__()
33
+
34
+ self.encoder = EncoderLayer(args, feature_dim)
35
+
36
+ def forward(self, feature):
37
+ return self.encoder(feature)
38
+
39
+
40
+ class GeoEncoderLayer(nn.Module):
41
+ def __init__(self, args, feature_dim) -> None:
42
+ super().__init__()
43
+
44
+ self.c = torch.tensor([args.c], device=args.device)
45
+ self.encoder = EncoderLayer(args, feature_dim)
46
+ self.manifold = geoopt.manifolds.Lorentz(-1 / args.c)
47
+
48
+ def forward(self, feature):
49
+ mean, gamma = self.encoder(feature)
50
+ mean = self.manifold.expmap0(F.pad(mean, (1, 0)))
51
+ mean = lorentz2halfplane(mean, self.c, log=torch.Tensor([True]))
52
+ mean = torch.stack([
53
+ mean[..., 0],
54
+ mean[..., 1] * 2
55
+ ], dim=-1)
56
+
57
+ return mean, gamma
58
+
59
+
60
+ class VanillaDecoderLayer(nn.Module):
61
+ def __init__(self, args) -> None:
62
+ super().__init__()
63
+
64
+ def forward(self, z):
65
+ z = z.reshape(*z.shape[:-2], -1)
66
+ return z
67
+
68
+
69
+ class GeoDecoderLayer(nn.Module):
70
+ def __init__(self, args) -> None:
71
+ super().__init__()
72
+
73
+ self.c = torch.tensor([args.c], device=args.device)
74
+ self.manifold = geoopt.manifolds.Lorentz(-1 / args.c)
75
+
76
+ def forward(self, z):
77
+ a, b = z[..., 0], (z[..., 1] * 0.5).exp()
78
+ z = torch.stack([a, b], dim=-1)
79
+ z = halfplane2lorentz(z, self.c)
80
+ z = self.manifold.logmap0(z)[..., 1:]
81
+ return z.reshape(*z.shape[:-2], -1)
82
+
@@ -0,0 +1,18 @@
1
+ import torch
2
+
3
+ from .distribution import Distribution
4
+
5
+
6
+ def get_prior(args):
7
+ mean = torch.zeros(
8
+ [1, args.latent_dim, 2],
9
+ device=args.device
10
+ )
11
+ covar = torch.zeros(
12
+ [1, args.latent_dim],
13
+ device=args.device
14
+ )
15
+
16
+ prior = Distribution(mean, covar, args.c)
17
+ return prior
18
+
@@ -0,0 +1,16 @@
1
+ """
2
+ Poincaré Normal Distribution
3
+ Wrapped normal distribution on the Poincaré ball model of hyperbolic space
4
+ """
5
+
6
+ from .distribution import Distribution
7
+ from .layers import VanillaEncoderLayer, VanillaDecoderLayer
8
+ from .prior import get_prior
9
+
10
+
11
+ __all__ = [
12
+ 'Distribution',
13
+ 'VanillaEncoderLayer',
14
+ 'VanillaDecoderLayer',
15
+ 'get_prior',
16
+ ]
@@ -0,0 +1,4 @@
1
+ def add_distribution_args(parser):
2
+ group = parser.add_argument_group('PoincareNormal')
3
+ group.add_argument('--layer', type=str, choices=['Vanilla', 'Geo'], default='Vanilla')
4
+
@@ -0,0 +1,133 @@
1
+ import torch
2
+
3
+ infty = torch.tensor(float('Inf'))
4
+
5
+ def diff(x):
6
+ return x[:, 1:] - x[:, :-1]
7
+
8
+ class ARS():
9
+ '''
10
+ This class implements the Adaptive Rejection Sampling technique of Gilks and Wild '92.
11
+ Where possible, naming convention has been borrowed from this paper.
12
+ The PDF must be log-concave.
13
+ Currently does not exploit lower hull described in paper- which is fine for drawing
14
+ only small amount of samples at a time.
15
+ '''
16
+
17
+ def __init__(self, logpdf, grad_logpdf, device, xi, lb=-infty, ub=infty, use_lower=False, ns=50, **fargs):
18
+ '''
19
+ initialize the upper (and if needed lower) hulls with the specified params
20
+ Parameters
21
+ ==========
22
+ f: function that computes log(f(u,...)), for given u, where f(u) is proportional to the
23
+ density we want to sample from
24
+ fprima: d/du log(f(u,...))
25
+ xi: ordered vector of starting points in wich log(f(u,...) is defined
26
+ to initialize the hulls
27
+ use_lower: True means the lower sqeezing will be used; which is more efficient
28
+ for drawing large numbers of samples
29
+ lb: lower bound of the domain
30
+ ub: upper bound of the domain
31
+ ns: maximum number of points defining the hulls
32
+ fargs: arguments for f and fprima
33
+ '''
34
+ self.device = device
35
+
36
+ self.lb = lb
37
+ self.ub = ub
38
+
39
+ self.logpdf = logpdf
40
+ self.grad_logpdf = grad_logpdf
41
+ self.fargs = fargs
42
+
43
+ #set limit on how many points to maintain on hull
44
+ self.ns = ns
45
+ self.xi = xi.to(self.device) # initialize x, the vector of absicassae at which the function h has been evaluated
46
+ self.B, self.K = self.xi.size() # hull size
47
+ self.h = torch.zeros(self.B, ns).to(self.device)
48
+ self.hprime = torch.zeros(self.B, ns).to(self.device)
49
+ self.x = torch.zeros(self.B, ns).to(self.device)
50
+ self.h[:, :self.K] = self.logpdf(self.xi, **self.fargs)
51
+ self.hprime[:, :self.K] = self.grad_logpdf(self.xi, **self.fargs)
52
+ self.x[:, :self.K] = self.xi
53
+ # Avoid under/overflow errors. the envelope and pdf are only
54
+ # proportional to the true pdf, so can choose any constant of proportionality.
55
+ self.offset = self.h.max(-1)[0].view(-1, 1)
56
+ self.h = self.h - self.offset
57
+
58
+ # Derivative at first point in xi must be > 0
59
+ # Derivative at last point in xi must be < 0
60
+ if not (self.hprime[:, 0] > 0).all(): raise IOError('initial anchor points must span mode of PDF (left)')
61
+ if not (self.hprime[:, self.K-1] < 0).all(): raise IOError('initial anchor points must span mode of PDF (right)')
62
+ self.insert()
63
+
64
+
65
+ def sample(self, shape=torch.Size()):
66
+ '''
67
+ Draw N samples and update upper and lower hulls accordingly
68
+ '''
69
+ shape = shape if isinstance(shape, torch.Size) else torch.Size([shape])
70
+ samples = torch.ones(self.B, *shape).to(self.device)
71
+ bool_mask = (torch.ones(self.B, *shape) == 1).to(self.device)
72
+ count = 0
73
+ while bool_mask.sum() != 0:
74
+ count += 1
75
+ xt, i = self.sampleUpper(shape)
76
+ ht = self.logpdf(xt, **self.fargs)
77
+ # hprimet = self.grad_logpdf(xt, **self.fargs)
78
+ ht = ht - self.offset
79
+ ut = self.h.gather(1, i) + (xt - self.x.gather(1, i)) * self.hprime.gather(1, i)
80
+
81
+ # Accept sample?
82
+ u = torch.rand(shape).to(self.device)
83
+ accept = u < torch.exp(ht - ut)
84
+ reject = ~accept
85
+ samples[bool_mask * accept] = xt[bool_mask * accept]
86
+ bool_mask[bool_mask * accept] = reject[bool_mask * accept]
87
+ # Update hull with new function evaluations
88
+ # if self.K < self.ns:
89
+ # nb_insert = self.ns - self.K
90
+ # self.insert(nb_insert, xt[:, :nb_insert], ht[:, :nb_insert], hprimet[:, :nb_insert])
91
+
92
+ return samples.t().unsqueeze(-1)
93
+
94
+
95
+ def insert(self, nbnew=0, xnew=None, hnew=None, hprimenew=None):
96
+ '''
97
+ Update hulls with new point(s) if none given, just recalculate hull from existing x,h,hprime
98
+ # '''
99
+ # if xnew is not None:
100
+ # self.x[:, self.K:self.K+nbnew] = xnew
101
+ # self.x, idx = self.x.sort()
102
+ # self.h[:, self.K:self.K+nbnew] = hnew
103
+ # self.h = self.h.gather(1, idx)
104
+ # self.hprime[:, self.K:self.K+nbnew] = hprimenew
105
+ # self.hprime = self.hprime.gather(1, idx)
106
+
107
+ # self.K += xnew.size(-1)
108
+
109
+ self.z = torch.zeros(self.B, self.K + 1).to(self.device)
110
+ self.z[:, 0] = self.lb; self.z[:, self.K] = self.ub
111
+ self.z[:, 1:self.K] = (diff(self.h[:, :self.K]) - diff(self.x[:, :self.K] * self.hprime[:, :self.K])) / -diff(self.hprime[:, :self.K])
112
+ idx = [0]+list(range(self.K))
113
+ self.u = self.h[:, idx] + self.hprime[:, idx] * (self.z-self.x[:, idx])
114
+
115
+ self.s = diff(torch.exp(self.u)) / self.hprime[:, :self.K]
116
+ self.s[self.hprime[:, :self.K] == 0.] = 0. # should be 0 when gradient is 0
117
+ self.cs = torch.cat((torch.zeros(self.B, 1).to(self.device), torch.cumsum(self.s, dim=-1)), dim=-1)
118
+ self.cu = self.cs[:, -1]
119
+
120
+ def sampleUpper(self, shape=torch.Size()):
121
+ '''
122
+ Return a single value randomly sampled from the upper hull and index of segment
123
+ '''
124
+
125
+ u = torch.rand(self.B, *shape).to(self.device)
126
+ i = (self.cs/self.cu.unsqueeze(-1)).unsqueeze(-1) <= u.unsqueeze(1).expand(*self.cs.shape, *shape)
127
+ idx = i.sum(1) - 1
128
+
129
+ xt = self.x.gather(1, idx) + (-self.h.gather(1, idx) + torch.log(self.hprime.gather(1, idx)*(self.cu.unsqueeze(-1)*u - self.cs.gather(1, idx)) +
130
+ torch.exp(self.u.gather(1, idx)))) / self.hprime.gather(1, idx)
131
+
132
+ return xt, idx
133
+
@@ -0,0 +1,58 @@
1
+ import torch
2
+ import geoopt
3
+
4
+ from .hyperbolic_radius import HyperbolicRadius
5
+ from .hyperbolic_uniform import HypersphericalUniform
6
+
7
+ MIN_NORM = 1e-15
8
+
9
+
10
+ def expmap_polar(c, x, u, r, dim: int = -1):
11
+ m = geoopt.manifolds.PoincareBall(1.0)
12
+ sqrt_c = c.sqrt()
13
+ u_norm = u.norm(dim=dim, p=2, keepdim=True).clamp_min(MIN_NORM)
14
+ second_term = (
15
+ (sqrt_c / 2 * r).tanh()
16
+ * u
17
+ / (sqrt_c * u_norm)
18
+ )
19
+
20
+ gamma_1 = m.mobius_add(x, second_term)
21
+ return gamma_1
22
+
23
+
24
+ class Distribution():
25
+ def __init__(self, mean, sigma) -> None:
26
+ self.mean = mean
27
+ # Clamp sigma to prevent numerical overflow in hyperbolic radius sampling
28
+ # Keep consistent with encoder bounds (min 0.01 for stability, max 10 to prevent overflow)
29
+ self.sigma = sigma.clamp(min=0.01, max=10.0)
30
+
31
+ self.manifold = geoopt.manifolds.PoincareBall(1.0)
32
+ self.radius = HyperbolicRadius(2, self.manifold.c, self.sigma.view(-1, 1))
33
+ self.direction = HypersphericalUniform(1, device=mean.device)
34
+ self.kl_div = None
35
+
36
+ def log_prob(self, z):
37
+ mean = self.mean[None].expand(z.shape)
38
+ radius_sq = self.manifold.dist(mean, z, keepdim=True).pow(2)
39
+ log_prob_z = - radius_sq / 2 / self.sigma.pow(2)[None] - self.direction._log_normalizer() - self.radius.log_normalizer.view([*self.mean.shape[:-1], -1])
40
+ log_prob_z = log_prob_z.sum(dim=-1)
41
+
42
+ return log_prob_z
43
+
44
+ def rsample(self, N):
45
+ fixed_shape = self.mean.shape[:-1]
46
+ mean = self.mean.view(-1, 2)
47
+ shape = mean[None].expand([N, *mean.shape]).size()
48
+ alpha = self.direction.sample(torch.Size([*shape[:-1]]))
49
+ radius = self.radius.rsample(torch.Size([N]))
50
+
51
+ z = expmap_polar(self.manifold.c, mean[None], alpha, radius)
52
+ z = z.reshape([N, *fixed_shape, -1])
53
+ return z
54
+
55
+ def sample(self, N):
56
+ with torch.no_grad():
57
+ return self.rsample(N)
58
+