tfga package¶
Submodules¶
tfga.blades module¶
Blade-related definitions and functions used across the library.
- class tfga.blades.BladeKind(value)[source]¶
Bases:
Enum
Kind of blade depending on its degree.
- BIVECTOR = 'bivector'¶
- EVEN = 'even'¶
- MV = 'mv'¶
- ODD = 'odd'¶
- PSEUDOBIVECTOR = 'pseudobivector'¶
- PSEUDOSCALAR = 'pseudoscalar'¶
- PSEUDOTRIVECTOR = 'pseudotrivector'¶
- PSEUDOVECTOR = 'pseudovector'¶
- SCALAR = 'scalar'¶
- TRIVECTOR = 'trivector'¶
- VECTOR = 'vector'¶
- tfga.blades.get_blade_indices_from_names(blade_names: List[str], all_blade_names: List[str]) Tensor [source]¶
Finds blade signs and indices for given blade names in a list of blade names. Blade names can be unnormalized and their correct sign will be returned.
- Args:
blade_names: Blade names to return indices for. May be unnormalized. all_blade_names: Blade names to use as index
- Returns:
blade_signs: signs for the passed blades in same order as passed blade_indices: blade indices in the same order as passed
- tfga.blades.get_blade_of_kind_indices(blade_degrees: Tensor, kind: BladeKind, max_degree: int, invert: bool = False) Tensor [source]¶
Finds a boolean mask for whether blades are of a given kind.
- Args:
blade_degrees: List of blade degrees kind: kind of blade for which the mask will be true max_degree: maximum blade degree in the algebra invert: whether to invert the result
- Returns:
boolean mask for whether blades are of a given kind
- tfga.blades.get_blade_repr(blade_name: str) str [source]¶
Returns the representation to use for a given blade.
Examples: - “12” -> “e_12” - “” -> “1”
- Args:
blade_name: name of the blade in the algebra (eg. “12”)
- Returns:
Representation to use for a given blade
- tfga.blades.get_normal_ordered(blade_name: str) Tuple[int, str] [source]¶
Returns the normal ordered blade name and its sign. Example: 21 => -1, 12
- Args:
blade_name: Blade name for which to return normal ordered name and sign
- Returns:
sign: sign of the blade blade_name: normalized name of the blade
- tfga.blades.invert_blade_indices(num_blades: int, blade_indices: Tensor) Tensor [source]¶
Returns all blade indices except for the given ones.
- Args:
num_blades: Total number of blades in the algebra blade_indices: blade indices to exclude
- Returns:
All blade indices except for the given ones
- tfga.blades.is_blade_kind(blade_degrees: Tensor, kind: BladeKind | str, max_degree: int) Tensor [source]¶
Finds a boolean mask for whether blade degrees are of a given kind.
- Args:
blade_degrees: list of blade degrees kind: kind of blade to check for max_degree: maximum blade degree in the algebra
- Returns:
boolean mask for whether blade degrees are of a given kind
tfga.cayley module¶
Operations for constructing the cayley 3-tensor needed for the geometric product. Used internally.
tfga.layers module¶
Provides Geometric Algebra Keras layers.
- class tfga.layers.GeometricAlgebraExp(*args, **kwargs)[source]¶
Bases:
GeometricAlgebraLayer
Calculates the exponential function of the input. Input must square to a scalar.
- Args:
algebra: GeometricAlgebra instance to use square_scalar_tolerance: Tolerance to use for the square scalar check or None if the check should be skipped
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_output_shape(input_shape)[source]¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Args:
- input_shape: Shape tuple (tuple of integers) or tf.TensorShape,
or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- class tfga.layers.GeometricAlgebraLayer(*args, **kwargs)[source]¶
Bases:
Layer
- classmethod from_config(config)[source]¶
Creates a layer from its config.
This method is the reverse of get_config, capable of instantiating the same layer from the config dictionary. It does not handle layer connectivity (handled by Network), nor weights (handled by set_weights).
- Args:
- config: A Python dictionary, typically the
output of get_config.
- Returns:
A layer instance.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- class tfga.layers.GeometricProductConv1D(*args, **kwargs)[source]¶
Bases:
GeometricAlgebraLayer
Analagous to Keras’ Conv1D layer but using multivector-valued kernels instead of scalar ones and geometric product instead of standard multiplication.
- Args:
algebra: GeometricAlgebra instance to use for the parameters filters: How many channels the output will have kernel_size: Size for the convolution kernel stride: Stride to use for the convolution padding: “SAME” (zero-pad input length so output
length == input length / stride) or “VALID” (no padding)
blade_indices_kernel: Blade indices to use for the kernel parameter blade_indices_bias: Blade indices to use for the bias parameter (if used)
- build(input_shape: TensorShape)[source]¶
Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().
This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).
- Args:
- input_shape: Instance of TensorShape, or list of instances of
TensorShape if the layer expects a list of inputs (one instance per input).
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- class tfga.layers.GeometricProductDense(*args, **kwargs)[source]¶
Bases:
GeometricAlgebraLayer
Analagous to Keras’ Dense layer but using multivector-valued matrices instead of scalar ones and geometric multiplication instead of standard multiplication.
- Args:
algebra: GeometricAlgebra instance to use for the parameters blade_indices_kernel: Blade indices to use for the kernel parameter blade_indices_bias: Blade indices to use for the bias parameter (if used)
- build(input_shape: TensorShape)[source]¶
Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().
This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).
- Args:
- input_shape: Instance of TensorShape, or list of instances of
TensorShape if the layer expects a list of inputs (one instance per input).
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_output_shape(input_shape)[source]¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Args:
- input_shape: Shape tuple (tuple of integers) or tf.TensorShape,
or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- class tfga.layers.GeometricProductElementwise(*args, **kwargs)[source]¶
Bases:
GeometricAlgebraLayer
Performs the elementwise geometric product with a list of multivectors with as many elements as there are input units.
- Args:
algebra: GeometricAlgebra instance to use for the parameters blade_indices_kernel: Blade indices to use for the kernel parameter blade_indices_bias: Blade indices to use for the bias parameter (if used)
- build(input_shape: TensorShape)[source]¶
Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().
This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).
- Args:
- input_shape: Instance of TensorShape, or list of instances of
TensorShape if the layer expects a list of inputs (one instance per input).
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_output_shape(input_shape)[source]¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Args:
- input_shape: Shape tuple (tuple of integers) or tf.TensorShape,
or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- class tfga.layers.GeometricSandwichProductDense(*args, **kwargs)[source]¶
Bases:
GeometricProductDense
Analagous to Keras’ Dense layer but using multivector-valued matrices instead of scalar ones and geometric sandwich multiplication instead of standard multiplication.
- Args:
algebra: GeometricAlgebra instance to use for the parameters blade_indices_kernel: Blade indices to use for the kernel parameter blade_indices_bias: Blade indices to use for the bias parameter (if used)
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- class tfga.layers.GeometricSandwichProductElementwise(*args, **kwargs)[source]¶
Bases:
GeometricProductElementwise
Performs the elementwise geometric sandwich product with a list of multivectors with as many elements as there are input units.
- Args:
algebra: GeometricAlgebra instance to use for the parameters blade_indices_kernel: Blade indices to use for the kernel parameter blade_indices_bias: Blade indices to use for the bias parameter (if used)
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- class tfga.layers.GeometricToTensor(*args, **kwargs)[source]¶
Bases:
GeometricAlgebraLayer
Layer for extracting given blades from geometric algebra tensors.
- Args:
algebra: GeometricAlgebra instance to use blade_indices: blade indices to extract
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_output_shape(input_shape)[source]¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Args:
- input_shape: Shape tuple (tuple of integers) or tf.TensorShape,
or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- class tfga.layers.GeometricToTensorWithKind(*args, **kwargs)[source]¶
Bases:
GeometricToTensor
Layer for extracting blades of a kind from geometric algebra tensors.
- Args:
algebra: GeometricAlgebra instance to use kind: blade indices of kind to extract
- class tfga.layers.TensorToGeometric(*args, **kwargs)[source]¶
Bases:
GeometricAlgebraLayer
Layer for converting tensors with given blade indices to geometric algebra tensors.
- Args:
algebra: GeometricAlgebra instance to use blade_indices: blade indices to interpret the last axis of the input tensor as
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_output_shape(input_shape)[source]¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Args:
- input_shape: Shape tuple (tuple of integers) or tf.TensorShape,
or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- class tfga.layers.TensorWithKindToGeometric(*args, **kwargs)[source]¶
Bases:
GeometricAlgebraLayer
Layer for converting tensors with given blade kind to geometric algebra tensors.
- Args:
algebra: GeometricAlgebra instance to use kind: blade kind indices to interpret the last axis of the input tensor as
- call(inputs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Args:
- inputs: Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args: Additional positional arguments. May contain tensors, although
this is not recommended, for the reasons above.
- **kwargs: Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_output_shape(input_shape)[source]¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Args:
- input_shape: Shape tuple (tuple of integers) or tf.TensorShape,
or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
tfga.mv module¶
Defines the MultiVector class which is used as a convenience wrapper for GeometricAlgebra operations.
- class tfga.mv.MultiVector(blade_values: Tensor, algebra: GeometricAlgebra)[source]¶
Bases:
object
Wrapper for geometric algebra tensors using GeometricAlgebra operations in a less verbose way using operators.
- property algebra¶
GeometricAlgebra instance this multivector belongs to.
- approx_exp(order: int = 50) MultiVector [source]¶
Approximate exponential. See GeometricAlgebra.approx_exp().
- approx_log(order: int = 50) MultiVector [source]¶
Approximate logarithm. See GeometricAlgebra.approx_log().
- property batch_shape¶
Batch shape of the multivector (ie. the shape of all axes except for the last one in the geometric algebra tensor).
- conjugation() MultiVector [source]¶
Conjugation. See GeometricAlgebra.conjugation().
- dual() MultiVector [source]¶
Dual. See GeometricAlgebra.dual().
- exp(square_scalar_tolerance: float | None = 0.0001) MultiVector [source]¶
Exponential. See GeometricAlgebra.exp().
- geom_conv1d(kernel: MultiVector, stride: int, padding: str, dilations: int | None = None) MultiVector [source]¶
1D convolution. See GeometricAlgebra.geom_conv1d().
- grade_automorphism() MultiVector [source]¶
Grade automorphism. See GeometricAlgebra.grade_automorphism().
- inverse() MultiVector [source]¶
Inverse. See GeometricAlgebra.inverse().
- simple_inverse() MultiVector [source]¶
Simple inverse. See GeometricAlgebra.simple_inverse().
- property tensor¶
Geometric algebra tensor holding the values of this multivector.
tfga.mv_ops module¶
Operations on geometric algebra tensors used internally.
- tfga.mv_ops.mv_conv1d(a_blade_values: Tensor, k_blade_values: Tensor, cayley: Tensor, stride: int, padding: str, dilations: int | None = None) Tensor [source]¶
tfga.tfga module¶
Provides classes and operations for performing geometric algebra with TensorFlow.
The GeometricAlgebra class is used to construct the algebra given a metric. It exposes methods for operating on tf.Tensor instances where their last axis is interpreted as blades of the algebra.
- class tfga.tfga.GeometricAlgebra(metric: List[float])[source]¶
Bases:
object
Class used for performing geometric algebra operations on tf.Tensor instances. Exposes methods for operating on tf.Tensor instances where their last axis is interpreted as blades of the algebra. Holds the metric and other quantities derived from it.
- approx_exp(a: Tensor, order: int = 50) Tensor [source]¶
Returns an approximation of the exponential using a centered taylor series.
- Args:
a: Geometric algebra tensor to return exponential for order: order of the approximation
- Returns:
Approximation of exp(a)
- approx_log(a: Tensor, order: int = 50) Tensor [source]¶
Returns an approximation of the natural logarithm using a centered taylor series. Only converges for multivectors where ||mv - 1|| < 1.
- Args:
a: Geometric algebra tensor to return logarithm for order: order of the approximation
- Returns:
Approximation of log(a)
- property basis_mvs: Tensor¶
List of basis vectors as tf.Tensor.
- property blade_degrees: Tensor¶
List of blade-degree for each blade in the algebra.
- property blade_mvs: Tensor¶
List of all blade tensors in the algebra.
- property blades: List[str]¶
List of all blade names.
Blades are all possible independent combinations of basis vectors. Basis vectors are named starting from “0” and counting up. The scalar blade is the empty string “”.
Example - Bases: [“0”, “1”, “2”] - Blades: [“”, “0”, “1”, “2”, “01”, “02”, “12”, “012”]
- property cayley: Tensor¶
MxMxM tensor where M is the number of basis blades in the algebra. Used for calculating the geometric product:
a_i, b_j, cayley_ijk -> c_k
- property cayley_inner: Tensor¶
Analagous to cayley but for inner product.
- property cayley_outer: Tensor¶
Analagous to cayley but for outer product.
- conjugation(tensor: Tensor) Tensor [source]¶
Combines reversion and grade automorphism. See https://en.wikipedia.org/wiki/Paravector#Clifford_conjugation.
- Args:
tensor: Geometric algebra tensor to return conjugate for
- Returns:
Geometric algebra tensor after reversion() and grade_automorphism()
- dual(tensor: Tensor) Tensor [source]¶
Returns the dual of the geometric algebra tensor.
- Args:
tensor: Geometric algebra tensor to return dual for
- Returns:
Dual of the geometric algebra tensor
- property dual_blade_indices: Tensor¶
Indices of the dual blades for each blade.
- property dual_blade_signs: Tensor¶
Signs of the dual blades for each blade.
- e(*blades: List[str]) Tensor [source]¶
Returns a geometric algebra tf.Tensor with the given blades set to 1.
- Args:
blades: list of blade names, can be unnormalized
- Returns:
tf.Tensor with blades set to 1
- exp(a: Tensor, square_scalar_tolerance: float | None = 0.0001) Tensor [source]¶
Returns the exponential of the passed geometric algebra tensor. Only works for multivectors that square to scalars.
- Args:
a: Geometric algebra tensor to return exponential for square_scalar_tolerance: Tolerance to use for the square scalar check
or None if the check should be skipped
- Returns:
exp(a)
- ext_prod(a: Tensor, b: Tensor) Tensor [source]¶
Returns the exterior product of two geometric algebra tensors.
- Args:
a: Geometric algebra tensor on the left hand side of the exterior product b: Geometric algebra tensor on the right hand side of the exterior product
- Returns:
exterior product of a and b
- from_scalar(scalar: Number) Tensor [source]¶
Creates a geometric algebra tf.Tensor with scalar elements.
- Args:
scalar: Elements to be used as scalars
- Returns:
Geometric algebra tf.Tensor from scalars
- from_tensor(tensor: Tensor, blade_indices: Tensor) Tensor [source]¶
Creates a geometric algebra tf.Tensor from a tf.Tensor and blade indices. The blade indices have to align with the last axis of the tensor.
- Args:
tensor: tf.Tensor to take as values for the geometric algebra tensor blade_indices: Blade indices corresponding to the tensor. Can be obtained from blade names eg. using get_kind_blade_indices() or as indices from the blades list property.
- Returns:
Geometric algebra tf.Tensor from tensor and blade indices
- from_tensor_with_kind(tensor: Tensor, kind: BladeKind) Tensor [source]¶
Creates a geometric algebra tf.Tensor from a tf.Tensor and a kind. The kind’s blade indices have to align with the last axis of the tensor.
- Args:
tensor: tf.Tensor to take as values for the geometric algebra tensor kind: Kind corresponding to the tensor
- Returns:
Geometric algebra tf.Tensor from tensor and kind
- geom_conv1d(a: Tensor, k: Tensor, stride: int, padding: str, dilations: int | None = None) Tensor [source]¶
Returns the 1D convolution of a sequence with a geometric algebra tensor kernel. The convolution is performed using the geometric product.
- Args:
- a: Input geometric algebra tensor of shape
[…, Length, ChannelsIn, Blades]
- k: Geometric algebra tensor for the convolution kernel of shape
[KernelSize, ChannelsIn, ChannelsOut, Blades]
stride: Stride to use for the convolution padding: “SAME” (zero-pad input length so output
length == input length / stride) or “VALID” (no padding)
- Returns:
Geometric algbra tensor of shape […, OutputLength, ChannelsOut, Blades] representing a convolved with k
- geom_prod(a: Tensor, b: Tensor) Tensor [source]¶
Returns the geometric product of two geometric algebra tensors.
- Args:
a: Geometric algebra tensor on the left hand side of the geometric product b: Geometric algebra tensor on the right hand side of the geometric product
- Returns:
geometric product of a and b
- get_blade_indices_of_degree(degree: int) Tensor [source]¶
Find all indices of blades of the given degree.
- Args:
degree: degree to return blades for
- Returns:
indices of blades with the given degree in the algebra
- get_kind_blade_indices(kind: BladeKind, invert: bool = False) Tensor [source]¶
Find all indices of blades of a given kind in the algebra.
- Args:
kind: kind of blade to give indices for invert: whether to return all blades not of the kind
- Returns:
indices of blades of a given kind in the algebra
- grade_automorphism(tensor: Tensor) Tensor [source]¶
Returns the geometric algebra tensor with odd grades negated. See https://en.wikipedia.org/wiki/Paravector#Grade_automorphism.
- Args:
tensor: Geometric algebra tensor to return grade automorphism for
- Returns:
Geometric algebra tensor with odd grades negated
- inner_prod(a: Tensor, b: Tensor) Tensor [source]¶
Returns the inner product of two geometric algebra tensors.
- Args:
a: Geometric algebra tensor on the left hand side of the inner product b: Geometric algebra tensor on the right hand side of the inner product
- Returns:
inner product of a and b
- int_pow(a: Tensor, n: int) Tensor [source]¶
Returns the geometric algebra tensor to the power of an integer using repeated multiplication.
- Args:
a: Geometric algebra tensor to raise n: integer power to raise the multivector to
- Returns:
a to the power of n
- inverse(a: Tensor) Tensor [source]¶
Returns the inverted geometric algebra tensor X^-1 such that X * X^-1 = 1.
Using Shirokov’s inverse algorithm that works in arbitrary dimensions, see https://arxiv.org/abs/2005.04015 Theorem 4.
- Args:
a: Geometric algebra tensor to return inverse for
- Returns:
inverted geometric algebra tensor
- is_pure(tensor: Tensor, blade_indices: Tensor) bool [source]¶
Returns whether the given tensor is purely of the given blades and has no non-zero values for blades not in the given blades.
- Args:
tensor: tensor to check purity for blade_indices: blade indices to check purity for
- Returns:
Whether the tensor is purely of the given blades and has no non-zero values for blades not in the given blades
- is_pure_kind(tensor: Tensor, kind: BladeKind) bool [source]¶
Returns whether the given tensor is purely of a given kind and has no non-zero values for blades not of the kind.
- Args:
tensor: tensor to check purity for kind: kind of blade to check purity for
- Returns:
Whether the tensor is purely of a given kind and has no non-zero values for blades not of the kind
- keep_blades(a: Tensor, blade_indices: List[int]) Tensor [source]¶
Takes a geometric algebra tensor and returns it with only the given blade_indices as non-zeros.
- Args:
a: Geometric algebra tensor to copy blade_indices: Indices for blades to keep
- Returns:
a with only blade_indices components as non-zeros
- keep_blades_with_name(a: Tensor, blade_names: List[str] | str) Tensor [source]¶
Takes a geometric algebra tensor and returns it with only the given blades as non-zeros.
- Args:
a: Geometric algebra tensor to copy blade_names: Blades to keep
- Returns:
a with only blade_names components as non-zeros
- property max_degree: int¶
Highest blade degree in the algebra.
- property metric: Tensor¶
Metric list which contains the number that each basis vector in the algebra squares to (ie. the diagonal of the metric tensor).
- mv_repr(a: Tensor) str [source]¶
Returns a string representation for the given geometric algebra tensor.
- Args:
a: Geometric algebra tensor to return the representation for
- Returns:
string representation for a
- property num_blades: int¶
Total number of blades in the algebra.
- print(*args, **kwargs)[source]¶
Same as the default print function but formats tf.Tensor instances that have as many elements on their last axis as the algebra has blades using mv_repr().
- reg_prod(a: Tensor, b: Tensor) Tensor [source]¶
Returns the regressive product of two geometric algebra tensors.
- Args:
a: Geometric algebra tensor on the left hand side of the regressive product b: Geometric algebra tensor on the right hand side of the regressive product
- Returns:
regressive product of a and b
- reversion(tensor: Tensor) Tensor [source]¶
Returns the grade-reversed geometric algebra tensor. See https://en.wikipedia.org/wiki/Paravector#Reversion_conjugation.
- Args:
tensor: Geometric algebra tensor to return grade-reversion for
- Returns:
Grade-reversed geometric algebra tensor
- select_blades(a: Tensor, blade_indices: List[int]) Tensor [source]¶
Takes a geometric algebra tensor and returns a tf.Tensor with the blades in blade_indices on the last axis.
- Args:
a: Geometric algebra tensor to copy blade_indices: Indices for blades to select
- Returns:
tf.Tensor based on a with blade_indices on last axis.
- select_blades_with_name(a: Tensor, blade_names: List[str] | str) Tensor [source]¶
Takes a geometric algebra tensor and returns a tf.Tensor with the blades in blade_names on the last axis.
- Args:
a: Geometric algebra tensor to copy blade_names: Blades to keep
- Returns:
tf.Tensor based on a with blade_names on last axis.