Skip to main content

Library that contains implementations of machine learning components in the hyperbolic space

Project description

HyperLib: Deep learning in the Hyperbolic space

PyPI version

Background

This library implements common Neural Network components in the hyperbolic space (using the Poincare model). The implementation of this library uses Tensorflow as a backend and can easily be used with Keras and is meant to help Data Scientists, Machine Learning Engineers, Researchers and others to implement hyperbolic neural networks.

You can also use this library for uses other than neural networks by using the mathematical functions available in the Poincare class. In the future we may implement components that can be used in models other than neural networks. You can learn more about Hyperbolic networks here, and in the references[^1] [^2] [^3] [^4].

Install

The recommended way to install is with pip

pip install hyperlib

To build from source, you need to compile the pybind11 extensions.
For example to build on linux:

conda -n hyperlib python=3.8 gxx_linux-64 pybind11
python setup.py install

Hyperlib works with python>=3.8 and tensorflow>=2.0.

Example Usage

Creating a hyperbolic neural network using Keras:

import tensorflow as tf
from tensorflow import keras
from hyperlib.nn.layers.lin_hyp import LinearHyperbolic
from hyperlib.nn.optimizers.rsgd import RSGD
from hyperlib.manifold.poincare import Poincare

# Create layers
hyperbolic_layer_1 = LinearHyperbolic(32, Poincare(), 1)
hyperbolic_layer_2 = LinearHyperbolic(32, Poincare(), 1)
output_layer = LinearHyperbolic(10, Poincare(), 1)

# Create optimizer
optimizer = RSGD(learning_rate=0.1)

# Create model architecture
model = tf.keras.models.Sequential([
  hyperbolic_layer_1,
  hyperbolic_layer_2,
  output_layer
])

# Compile the model with the Riemannian optimizer            
model.compile(
    optimizer=optimizer,
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)

Using math functions on the Poincare ball:

import tensorflow as tf
from hyperlib.manifold.poincare import Poincare

p = Poincare()

# Create two matrices
a = tf.constant([[5.0,9.4,3.0],[2.0,5.2,8.9],[4.0,7.2,8.9]])
b = tf.constant([[4.8,1.0,2.3]])

# Matrix multiplication on the Poincare ball
curvature = 1
p.mobius_matvec(a, b, curvature)

Embeddings

A big advantage of hyperbolic space is its ability to represent hierarchical data. There are several techniques for embedding data in hyperbolic space; the most common is gradient methods [^6].

If your data has a natural metric you can also use TreeRep[^5]. Input a symmetric distance matrix, or a compressed distance matrix

import numpy as np
from hyperlib.embedding.treerep import treerep
from hyperlib.embedding.sarkar import sarkar_embedding 

# Example: immunological distances between 8 mammals by Sarich
compressed_metric = np.array([ 
        32.,  48.,  51.,  50.,  48.,  98., 148.,  
        26.,  34.,  29.,  33., 84., 136.,  
        42.,  44.,  44.,  92., 152.,  
        44.,  38.,  86., 142.,
        42.,  89., 142.,  
        90., 142., 
        148.
    ])

# outputs a weighted networkx Graph
tree = treerep(compressed_metric, return_networkx=True)

# embed the tree in 2D hyperbolic space
root = 0
embedding = sarkar_embedding(tree, root, tau=0.5)

Please see the examples directory for complete examples.

TODO:

  • Implement an Attention Mechanism
  • Implement a Riemannian Adam Optimizer
  • Lorentz Model
  • Embeddings
  • Remove casting of layer variables to tf.float64

References

[^1]: Chami, I., Ying, R., Ré, C. and Leskovec, J. Hyperbolic Graph Convolutional Neural Networks. NIPS 2019.

[^2]: Nickel, M. and Kiela, D. Poincaré embeddings for learning hierarchical representations. NIPS 2017.

[^3]: Khrulkov, Mirvakhabova, Ustinova, Oseledets, Lempitsky. Hyperbolic Image Embeddings.

[^4]: Wei Peng, Varanka, Mostafa, Shi, Zhao. Hyperbolic Deep Neural Networks: A Survey.

[^5]: Rishi Sonthalia and Anna Gilbert. Tree! I am no Tree! I am a Low Dimensional Hyperbolic Embedding
[^6]: De Sa et. al. Representation Tradeoffs for Hyperbolic Embeddings

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hyperlib-0.0.4.tar.gz (21.3 kB view hashes)

Uploaded Source

Built Distributions

hyperlib-0.0.4-cp39-cp39-win_amd64.whl (96.5 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

hyperlib-0.0.4-cp39-cp39-win32.whl (86.7 kB view hashes)

Uploaded CPython 3.9 Windows x86

hyperlib-0.0.4-cp39-cp39-musllinux_1_1_x86_64.whl (661.7 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

hyperlib-0.0.4-cp39-cp39-musllinux_1_1_i686.whl (721.0 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

hyperlib-0.0.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (143.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

hyperlib-0.0.4-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (150.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

hyperlib-0.0.4-cp39-cp39-macosx_10_9_x86_64.whl (107.7 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

hyperlib-0.0.4-cp38-cp38-win_amd64.whl (97.4 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

hyperlib-0.0.4-cp38-cp38-win32.whl (86.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

hyperlib-0.0.4-cp38-cp38-musllinux_1_1_x86_64.whl (661.5 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

hyperlib-0.0.4-cp38-cp38-musllinux_1_1_i686.whl (720.4 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

hyperlib-0.0.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (143.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

hyperlib-0.0.4-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (150.4 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

hyperlib-0.0.4-cp38-cp38-macosx_10_9_x86_64.whl (107.5 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

hyperlib-0.0.4-cp37-cp37m-win_amd64.whl (98.3 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

hyperlib-0.0.4-cp37-cp37m-win32.whl (87.6 kB view hashes)

Uploaded CPython 3.7m Windows x86

hyperlib-0.0.4-cp37-cp37m-musllinux_1_1_x86_64.whl (664.5 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

hyperlib-0.0.4-cp37-cp37m-musllinux_1_1_i686.whl (722.9 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

hyperlib-0.0.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (146.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

hyperlib-0.0.4-cp37-cp37m-manylinux_2_17_i686.manylinux2014_i686.whl (152.8 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686

hyperlib-0.0.4-cp37-cp37m-macosx_10_9_x86_64.whl (106.7 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page