geometric_kernels.utils.utils¶
Convenience utilities.
Module Contents¶
- geometric_kernels.utils.utils.binary_vectors_and_subsets(d)[source]¶
Generates all possible binary vectors of size d and all possible subsets of the set \(\{0, .., d-1\}\) as a byproduct.
- Parameters:
d (int) – The dimension of binary vectors and the size of the set to take subsets of.
- Returns:
A tuple (x, combs), where x is a matrix of size (2**d, d) whose rows are all possible binary vectors of size d, and combs is a list of all possible subsets of the set \(\{0, .., d-1\}\), each subset being represented by a list of integers itself.
- geometric_kernels.utils.utils.chain(elements, repetitions)[source]¶
Repeats each element in elements by a certain number of repetitions as specified in repetitions. The length of elements and repetitions should match.
- Parameters:
elements (lab.Numeric) – An [N,]-shaped array of elements to repeat.
repetitions (beartype.typing.List[int]) – A list specifying the number of types to repeat each of the elements in elements. The length of repetitions should be equal to N.
- Returns:
An [M,]-shaped array.
- Return type:
lab.Numeric
EXAMPLE:
elements = np.array([1, 2, 3]) repetitions = [2, 1, 3] out = chain(elements, repetitions) print(out) # [1, 1, 2, 3, 3, 3]
- geometric_kernels.utils.utils.fixed_length_partitions(n, L)[source]¶
A generator for integer partitions of n into L parts, in colex order.
- Parameters:
n (int) – The number to partition.
L (int) – Size of partitions.
- Return type:
beartype.typing.Generator[beartype.typing.List[int], None, None]
Developed by D. Eppstein in 2005, taken from https://www.ics.uci.edu/~eppstein/PADS/IntegerPartitions.py The algorithm follows Knuth v4 fasc3 p38 in rough outline; Knuth credits it to Hindenburg, 1779.
- geometric_kernels.utils.utils.get_resource_file_path(filename)[source]¶
A contextmanager wrapper around impresources that supports both Python>=3.9 and Python==3.8 with a unified interface.
- Parameters:
filename (str) – The name of the file.
- geometric_kernels.utils.utils.hamming_distance(x1, x2)[source]¶
Hamming distance between two batches of boolean vectors.
- Parameters:
x1 (lab.Bool) – Array of any backend, of shape [N, D].
x2 (lab.Bool) – Array of any backend, of shape [M, D].
- Returns:
An array of shape [N, M] whose entry n, m contains the Hamming distance between x1[n, :] and x2[m, :]. It is of the same backend as x1 and x2.
- geometric_kernels.utils.utils.log_binomial(n, k)[source]¶
Compute the logarithm of the binomial coefficient.
- Parameters:
n (lab.Int) – The number of elements in the set.
k (lab.Int) – The number of elements to choose.
- Returns:
The logarithm of the binomial coefficient binom(n, k).
- Return type:
lab.Float
- geometric_kernels.utils.utils.make_deterministic(f, key)[source]¶
Returns a deterministic version of a function that uses a random number generator.
- Parameters:
f (beartype.typing.Callable) – The function to make deterministic.
key (lab.RandomState) – The key used to generate the random state.
- Returns:
A function representing the deterministic version of the input function.
Note
This function assumes that the input function has a ‘key’ argument or keyword-only argument that is used to generate random numbers. Otherwise, the function is returned as is.
- Return type:
beartype.typing.Callable
EXAMPLE:
key = tf.random.Generator.from_seed(1234) feature_map = default_feature_map(kernel=base_kernel) sample_paths = make_deterministic(sampler(feature_map), key) _, ys_train = sample_paths(xs_train, params) key, ys_test = sample_paths(xs_test, params)
- geometric_kernels.utils.utils.ordered_pairwise_differences(X)[source]¶
Compute the ordered pairwise differences between elements of a vector.
- Parameters:
X (lab.Numeric) – A […, D]-shaped array, a batch of D-dimensional vectors.
- Returns:
A […, C]-shaped array, where C = D*(D-1)//2, containing the ordered pairwise differences between the elements of X. That is, the array containing differences X[…,i] - X[…,j] for all i < j.
- Return type:
lab.Numeric
- geometric_kernels.utils.utils.partition_dominance_cone(partition)[source]¶
Calculates partitions dominated by a given one and having the same number of parts (including the zero parts of the original).
- Parameters:
partition (beartype.typing.Tuple[int, Ellipsis]) – A partition.
- Returns:
A set of partitions.
- Return type:
beartype.typing.Set[beartype.typing.Tuple[int, Ellipsis]]
- geometric_kernels.utils.utils.partition_dominance_or_subpartition_cone(partition)[source]¶
Calculates subpartitions and partitions dominated by a given one and having the same number of parts (including zero parts of the original).
- Parameters:
partition (beartype.typing.Tuple[int, Ellipsis]) – A partition.
- Returns:
A set of partitions.
- Return type:
beartype.typing.Set[beartype.typing.Tuple[int, Ellipsis]]