hyperbolic-math 0.1.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Data Mining and Machine Learning - University of Vienna
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.4
2
+ Name: hyperbolic_math
3
+ Version: 0.1.0
4
+ Summary: Hyperbolic Mathematics
5
+ Author-email: Thomas Lang <thomas.lang@univie.ac.at>, Kevin Sidak <kevin.sidak@univie.ac.at>
6
+ Requires-Python: <3.11,>=3.8
7
+ License-File: LICENSE
8
+ Requires-Dist: matplotlib
9
+ Requires-Dist: numpy
10
+ Requires-Dist: scikit-learn
11
+ Requires-Dist: torch
12
+ Provides-Extra: test
13
+ Requires-Dist: pytest>=8.3.3; extra == "test"
14
+ Provides-Extra: dev
15
+ Requires-Dist: pytest>=8.3.3; extra == "dev"
16
+ Dynamic: license-file
@@ -0,0 +1,292 @@
1
+ # Hyperbolic Math
2
+
3
+ A PyTorch-based library for hyperbolic geometry and deep learning in hyperbolic spaces. This library provides implementations of various hyperbolic manifolds, neural network layers, visualization techniques and Riemannian optimizers for working with hyperbolic embeddings.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Features](#features)
8
+ - [Installation](#installation)
9
+ - [Quick Start](#quick-start)
10
+ - [Testing](#testing)
11
+ - [Citation](#citation)
12
+ - [License](#license)
13
+
14
+
15
+ ## Features
16
+
17
+ ### Manifolds
18
+ - **Poincaré Ball**: Poincaré model of hyperbolic space with constraint $x_1^2 + x_1^2 + \ldots + x_d^2 < 1/c$
19
+ - **Hyperboloid**: Lorentz model of hyperbolic space with constraint $-x_0^2 + x_1^2 + \ldots + x_d^2 = -1/c, x_0 > 0$
20
+ - **Euclidean**: Standard Euclidean space for comparison and baseline experiments
21
+ - **Trainable curvature**: Support for learnable curvature parameters via `trainable_c` flag
22
+ - **Precision control**: Fully configurable manifold precision (`float32` or `float64`) for optimal performance-accuracy trade-offs
23
+ - **Comprehensive operations**: Exponential/logarithmic/retraction maps, parallel transport, geodesic distances, scalar multiplication, manifold conversion maps, etc.
24
+
25
+ ### Neural Network Layers
26
+ - **Standard hyperbolic layers**: Expmap, Logmap, Retraction, Activation modules for flexible network architectures
27
+ - **Model-specific layers**: Specialized linear and regression layers for both Poincaré and Hyperboloid models
28
+ - **PyTorch integration**: Seamless integration with PyTorch's `nn.Module` for easy model building
29
+
30
+ ### Riemannian Optimizers
31
+ - **Riemannian SGD**: Stochastic gradient descent with momentum and Nesterov acceleration on manifolds
32
+ - **Riemannian Adam**: Adaptive moment estimation with AMSGrad variant for Riemannian optimization
33
+ - **Flexible updates**: Support for both exponential map and retraction-based parameter updates
34
+ - **PyTorch-compatible API**: Drop-in replacement for standard PyTorch optimizers
35
+
36
+ ### Numerical Stability & Testing
37
+ - **Fine-tuned precision handling**: Carefully calibrated numerical thresholds for stable computations
38
+ - **Comprehensive test suite**: Unit tests covering geometric properties, numerical stability, and edge cases
39
+ - **Debugging support**: Extensive assertions and validation to catch errors early
40
+
41
+ ### Utilities
42
+ - **Math utilities**: Stable implementations of hyperbolic functions (acosh, asinh, atanh, etc.)
43
+ - **Horospherical PCA++**: Stable dimensionality reduction for hyperbolic data
44
+ - **Visualization tools**: Functions for plotting and analyzing hyperbolic embeddings
45
+
46
+
47
+ ## Installation
48
+
49
+ ### From Source
50
+
51
+ ```bash
52
+ git clone https://github.com/univie-dm/hyperbolic-math.git
53
+ cd hyperbolic-math
54
+ pip install -e .
55
+ ```
56
+
57
+ Alternatively for development and testing:
58
+ ```bash
59
+ pip install -e ".[dev]"
60
+ ```
61
+
62
+ ### Requirements
63
+
64
+ - Python >= 3.8, < 3.11
65
+ - PyTorch
66
+ - NumPy
67
+ - Matplotlib
68
+ - Scikit-learn
69
+ - Pytest (for testing)
70
+
71
+
72
+ ## Quick Start
73
+
74
+ ### Basic Manifold Operations
75
+
76
+ ```python
77
+ import torch
78
+ from src.manifolds import PoincareBall, Hyperboloid
79
+
80
+ # Initialize a Poincaré ball manifold (with curvature c=1 with operations performed in float64)
81
+ poincare = PoincareBall(c=torch.tensor([1.0]), dtype="float64")
82
+
83
+ # Create random points in the tangent space at the manifold's origin
84
+ u = torch.randn(10, 5, dtype=torch.float64) * 0.1
85
+ v = torch.randn(10, 5, dtype=torch.float64) * 0.1
86
+
87
+ # Map the 5-dimensional points to the manifold using the exponential map
88
+ x = poincare.expmap_0(u, axis=-1)
89
+ y = poincare.expmap_0(v, axis=-1)
90
+
91
+ # Compute the distances between the points
92
+ distances = poincare.dist(x, y, axis=-1)
93
+ print(f"Distances: {distances}")
94
+
95
+
96
+ # Hyperboloid manifold
97
+ hyperboloid = Hyperboloid(c=torch.tensor([1.0]), dtype="float64")
98
+
99
+ # Create random points in the tangent space at the manifold's origin
100
+ u_hyp = torch.randn(10, 5, dtype=torch.float64) * 0.1
101
+ u_hyp = torch.cat([torch.zeros_like(u_hyp[..., :1]), u_hyp], dim=-1)
102
+
103
+ # Map to the manifold using the exponential map
104
+ x_hyp = hyperboloid.expmap_0(u_hyp, axis=-1)
105
+ print(f"Points are in the manifold: {hyperboloid.is_in_manifold(x_hyp)}")
106
+
107
+ # Compute the distances of the points from the manifold's origin
108
+ distances_hyp = hyperboloid.dist_0(x_hyp, axis=-1)
109
+ print(f"Distances from the origin: {distances_hyp}")
110
+ ```
111
+
112
+ ### Building Hyperbolic Neural Networks
113
+
114
+ ```python
115
+ import torch
116
+ import torch.nn as nn
117
+ from src.manifolds import PoincareBall
118
+ from src.nn_layers import Expmap_0, Logmap_0, HyperbolicLinearPoincarePP
119
+
120
+ # Define a simple hyperbolic neural network
121
+ class HyperbolicNet(nn.Module):
122
+ def __init__(self, input_dim, hidden_dim, output_dim, manifold):
123
+ super().__init__()
124
+ self.manifold = manifold
125
+
126
+ # Map to manifold
127
+ self.expmap_0 = Expmap_0(manifold)
128
+
129
+ # Hyperbolic linear layer
130
+ self.hyp_linear = HyperbolicLinearPoincarePP(
131
+ manifold, input_dim, hidden_dim, hyperbolic_axis=-1
132
+ )
133
+
134
+ # Map back to tangent space for output
135
+ self.logmap_0 = Logmap_0(manifold)
136
+
137
+ # Euclidean output layer
138
+ self.output = nn.Linear(hidden_dim, output_dim)
139
+
140
+ def forward(self, x):
141
+ # x is in tangent space
142
+ x = self.expmap_0(x) # Map to manifold
143
+ x = self.hyp_linear(x) # Hyperbolic linear transformation
144
+ x = self.logmap_0(x) # Map back to tangent space
145
+ x = self.output(x) # Euclidean output
146
+ return x
147
+
148
+ # Initialize the network
149
+ poincare = PoincareBall(c=torch.tensor([1.0]))
150
+ model = HyperbolicNet(input_dim=10, hidden_dim=20, output_dim=5, manifold=poincare)
151
+
152
+ # Example tangent vector input
153
+ u = torch.randn(32, 10)
154
+ output = model(u)
155
+ print(f"Output shape: {output.shape}")
156
+ ```
157
+
158
+ ### Using Riemannian Optimizers
159
+
160
+ ```python
161
+ import torch
162
+ from src.manifolds import PoincareBall, ManifoldParameter
163
+ from src.optim import RiemannianAdam
164
+
165
+ # Create hyperbolic parameters
166
+ poincare = PoincareBall(c=torch.tensor([1.0]))
167
+ hyperbolic_weights = ManifoldParameter(
168
+ data=torch.randn(100, 50),
169
+ requires_grad=True,
170
+ manifold=poincare
171
+ )
172
+
173
+ # Initialize the Riemannian Adam optimizer with exponential map updates
174
+ optimizer = RiemannianAdam(
175
+ [hyperbolic_weights],
176
+ lr=1e-3,
177
+ expmap_update=True,
178
+ hyperbolic_axis=-1
179
+ )
180
+
181
+ # Training loop example
182
+ for epoch in range(10):
183
+ optimizer.zero_grad()
184
+
185
+ # Your loss computation here
186
+ # loss = compute_loss(hyperbolic_weights)
187
+
188
+ # loss.backward()
189
+ # optimizer.step()
190
+ ```
191
+
192
+
193
+ ## Testing
194
+
195
+ The library includes comprehensive unit tests for all manifold operations and optimizers.
196
+
197
+ ### Running Tests
198
+
199
+ ```bash
200
+ # Run all tests
201
+ pytest
202
+
203
+ # Run a specific test file
204
+ pytest tests/test_manifolds.py
205
+
206
+ # Run a specific test
207
+ pytest tests/test_manifolds.py::test_scalar_mul
208
+ ```
209
+
210
+ ### Test Coverage
211
+
212
+ Tests include:
213
+ - Geometric properties verification (triangle inequality, identity elements, etc.)
214
+ - Numerical stability tests
215
+ - Manifold-specific operations
216
+ - Optimizer convergence tests
217
+
218
+
219
+ ## Citation
220
+ TODO:
221
+ If you use this library in your research, please cite:
222
+
223
+ ```bibtex
224
+ @software{hyperbolic_math_2024,
225
+ title = {Hyperbolic Math: A PyTorch Library for Hyperbolic Deep Learning},
226
+ author = {Lang, Thomas and Sidak, Kevin},
227
+ year = {2024},
228
+ institution = {University of Vienna},
229
+ url = {https://github.com/univie-dm/hyperbolic-math}
230
+ }
231
+ ```
232
+
233
+ ### Related Publications
234
+
235
+ This library builds upon research in hyperbolic geometry and deep learning, including:
236
+
237
+ - Aaron Lou, et al. "Differentiating through the fréchet mean."
238
+ *International conference on machine learning (2020)*.
239
+ - Abraham Ungar. "A gyrovector space approach to hyperbolic geometry."
240
+ *Springer Nature (2022)*.
241
+ - Ahmad Bdeir, Kristian Schwethelm, and Niels Landwehr. "Fully hyperbolic convolutional neural networks for computer vision."
242
+ *arXiv preprint arXiv:2303.15919 (2023)*.
243
+ - Edoardo Cetin, et al. "Hyperbolic deep reinforcement learning."
244
+ *arXiv (2022)*.
245
+ - Ganea Octavian, Gary Bécigneul, and Thomas Hofmann. "Hyperbolic neural networks."
246
+ *Advances in neural information processing systems 31 (2018)*.
247
+ - Gary Bécigneul and Octavian Ganea. "Riemannian adaptive optimization methods."
248
+ *International Conference on Learning Representations (2019)*.
249
+ - Ines Chami, et al. "Horopca: Hyperbolic dimensionality reduction via horospherical projections."
250
+ *International Conference on Machine Learning (2021)*.
251
+ - Ines Chami, et al. "Hyperbolic graph convolutional neural networks."
252
+ *Advances in neural information processing systems 32 (2019)*.
253
+ - Max Kochurov, Rasul Karimov and Serge Kozlukov. "Geoopt: Riemannian Optimization in PyTorch."
254
+ *arXiv (2020)*.
255
+ - Maximillian Nickel and Douwe Kiela. "Learning continuous hierarchies in the lorentz model of hyperbolic geometry."
256
+ *International conference on machine learning. PMLR (2018)*.
257
+ - Maximillian Nickel and Douwe Kiela. "Poincaré embeddings for learning hierarchical representations."
258
+ *Advances in neural information processing systems 30 (2017)*.
259
+ - Marc T. Law, et al. "Lorentzian distance learning for hyperbolic representations."
260
+ *International Conference on Machine Learning (2019)*.
261
+ - Sashank J. Reddi, Satyen Kale, and Sanjiv Kumar. "On the convergence of adam and beyond."
262
+ *arXiv preprint arXiv:1904.09237 (2019)*.
263
+ - Shimizu Ryohei, Yusuke Mukuta, and Tatsuya Harada. "Hyperbolic neural networks++."
264
+ *arXiv preprint arXiv:2006.08210 (2020)*.
265
+ - Silvere Bonnabel. "Stochastic gradient descent on Riemannian manifolds."
266
+ *IEEE Transactions on Automatic Control 58.9 (2013): 2217-2229*.
267
+ - Weize Chen, et al. "Fully hyperbolic neural networks."
268
+ *arXiv preprint arXiv:2105.14686 (2021)*.
269
+
270
+
271
+ ## License
272
+ MIT License
273
+
274
+ Copyright (c) 2025 Data Mining and Machine Learning - University of Vienna
275
+
276
+ Permission is hereby granted, free of charge, to any person obtaining a copy
277
+ of this software and associated documentation files (the "Software"), to deal
278
+ in the Software without restriction, including without limitation the rights
279
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
280
+ copies of the Software, and to permit persons to whom the Software is
281
+ furnished to do so, subject to the following conditions:
282
+
283
+ The above copyright notice and this permission notice shall be included in all
284
+ copies or substantial portions of the Software.
285
+
286
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
287
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
288
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
289
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
290
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
291
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
292
+ SOFTWARE.
@@ -0,0 +1,38 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "hyperbolic_math"
7
+ version = "0.1.0"
8
+ authors = [
9
+ { name="Thomas Lang", email="thomas.lang@univie.ac.at" },
10
+ { name="Kevin Sidak", email="kevin.sidak@univie.ac.at" }
11
+ ]
12
+ description = "Hyperbolic Mathematics"
13
+ requires-python = ">=3.8,<3.11"
14
+ dependencies = [
15
+ "matplotlib",
16
+ "numpy",
17
+ "scikit-learn",
18
+ "torch",
19
+ ]
20
+
21
+ [project.optional-dependencies]
22
+ test = ["pytest>=8.3.3"]
23
+ dev = ["pytest>=8.3.3"]
24
+
25
+ [tool.setuptools.packages.find]
26
+ where = ["src"]
27
+ include = ["hyperbolic_test*", "manifolds*", "utils*"]
28
+
29
+ [tool.pytest.ini_options]
30
+ addopts = "-ra -q --log-cli-level=DEBUG"
31
+ testpaths = ["tests"]
32
+ pythonpath = "src"
33
+
34
+ [dependency-groups]
35
+ dev = [
36
+ "ipdb>=0.13.13",
37
+ "pytest>=8.3.3"
38
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.4
2
+ Name: hyperbolic_math
3
+ Version: 0.1.0
4
+ Summary: Hyperbolic Mathematics
5
+ Author-email: Thomas Lang <thomas.lang@univie.ac.at>, Kevin Sidak <kevin.sidak@univie.ac.at>
6
+ Requires-Python: <3.11,>=3.8
7
+ License-File: LICENSE
8
+ Requires-Dist: matplotlib
9
+ Requires-Dist: numpy
10
+ Requires-Dist: scikit-learn
11
+ Requires-Dist: torch
12
+ Provides-Extra: test
13
+ Requires-Dist: pytest>=8.3.3; extra == "test"
14
+ Provides-Extra: dev
15
+ Requires-Dist: pytest>=8.3.3; extra == "dev"
16
+ Dynamic: license-file
@@ -0,0 +1,20 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/hyperbolic_math.egg-info/PKG-INFO
5
+ src/hyperbolic_math.egg-info/SOURCES.txt
6
+ src/hyperbolic_math.egg-info/dependency_links.txt
7
+ src/hyperbolic_math.egg-info/requires.txt
8
+ src/hyperbolic_math.egg-info/top_level.txt
9
+ src/manifolds/__init__.py
10
+ src/manifolds/euclidean.py
11
+ src/manifolds/hyperboloid.py
12
+ src/manifolds/manifold.py
13
+ src/manifolds/poincare.py
14
+ src/utils/__init__.py
15
+ src/utils/helpers.py
16
+ src/utils/horo_pca.py
17
+ src/utils/math_utils.py
18
+ src/utils/vis_utils.py
19
+ tests/test_manifolds.py
20
+ tests/test_optimizers.py
@@ -0,0 +1,10 @@
1
+ matplotlib
2
+ numpy
3
+ scikit-learn
4
+ torch
5
+
6
+ [dev]
7
+ pytest>=8.3.3
8
+
9
+ [test]
10
+ pytest>=8.3.3
@@ -0,0 +1,2 @@
1
+ manifolds
2
+ utils
@@ -0,0 +1,5 @@
1
+ from .manifold import Manifold
2
+ from .manifold import ManifoldParameter
3
+ from .euclidean import Euclidean
4
+ from .hyperboloid import Hyperboloid
5
+ from .poincare import PoincareBall