TFGA - TensorFlow Geometric Algebra
GitHub | Docs | Benchmarks | Slides
Python package for Geometric / Clifford Algebra with TensorFlow 2.
This project is a work in progress. Its API may change and the examples aren't polished yet.
Pull requests and suggestions either by opening an issue or by sending me an email are welcome.
Installation
Install using pip: pip install tfga
Requirements:
- Python 3
- tensorflow 2
- numpy
Basic usage
There are two ways to use this library. In both ways we first create a GeometricAlgebra
instance given a metric.
Then we can either work on tf.Tensor
instances directly where the last axis is assumed to correspond to
the algebra's blades.
import tensorflow as tf
from tfga import GeometricAlgebra
ga = GeometricAlgebra(metric=[1, 1, 1])
ordinary_vector = ga.from_tensor_with_kind(tf.ones(3), kind="vector")
quaternion = ga.from_tensor_with_kind(tf.fill(dims=4, value=5), kind="even")
multivector = ordinary_vector + quaternion
ga.print(ga.inner_prod(ga.e0, ordinary_vector))
ga.print(ga.ext_prod(ga.e0, ga.e1))
ga.print(ga.reversion(quaternion))
ga.print(quaternion[0])
ga.print(ga.select_blades_with_name(quaternion, "10"))
ga.print(ga.keep_blades_with_name(quaternion, "10"))
Alternatively we can convert the geometric algebra tf.Tensor
instance to MultiVector
instances which wrap the operations and provide operator overrides for convenience.
This can be done by using the __call__
operator of the GeometricAlgebra
instance.
a = ga.e123
b = ga.e1
mv_a = ga(a)
mv_b = ga(b)
print(~mv_a)
print(mv_a * mv_b)
print(mv_a | mv_b)
print(mv_a ^ mv_b)
Keras layers
TFGA also provides Keras layers which provide
layers similar to the existing ones but using multivectors instead. For example the GeometricProductDense
layer is exactly the same as the Dense
layer but uses
multivector-valued weights and biases instead of scalar ones. The exact kind of multivector-type can be
passed too. Example:
import tensorflow as tf
from tfga import GeometricAlgebra
from tfga.layers import TensorToGeometric, GeometricToTensor, GeometricProductDense
sta = GeometricAlgebra([1, -1, -1, -1])
vector_blade_indices = sta.get_kind_blade_indices(BladeKind.VECTOR),
tensor = tf.ones([20, 6, 4])
result_indices = tf.concat([
sta.get_kind_blade_indices(BladeKind.SCALAR),
sta.get_kind_blade_indices(BladeKind.BIVECTOR)
], axis=0)
sequence = tf.keras.Sequential([
TensorToGeometric(sta, blade_indices=vector_blade_indices),
GeometricProductDense(
algebra=sta, units=8,
blade_indices_kernel=vector_blade_indices,
blade_indices_bias=result_indices
),
GeometricToTensor(sta, blade_indices=result_indices)
])
result = sequence(tensor)
Available layers
Notebooks
Generic examples
Using Keras layers to estimate triangle area
Classical Electromagnetism using Geometric Algebra
Quantum Electrodynamics using Geometric Algebra
Projective Geometric Algebra
1D Multivector-valued Convolution Example
Tests
Tests using Python's built-in unittest
module are available in the tests
directory. All tests can be run by
executing python -m unittest discover tests
from the root directory of the repository.
Citing
See our Zenodo page. For citing all versions the following BibTeX can be used
@software{python_tfga,
author = {Kahlow, Robin},
title = {TensorFlow Geometric Algebra},
publisher = {Zenodo},
doi = {10.5281/zenodo.3902404},
url = {https://doi.org/10.5281/zenodo.3902404}
}
Disclaimer
TensorFlow, the TensorFlow logo and any related marks are trademarks of Google Inc.