laft module provides essential functions for vector transformations, similarity computations, and feature space manipulations commonly used in representation learning.
inner
Projects features onto a vector or subspace defined by one or more vectors.Input features with shape
[batch_size, feature_size] to be projected.Vector(s) defining the projection subspace. Can be:
- 1D tensor of shape
[feature_size]for single vector projection - 2D tensor of shape
[num_vectors, feature_size]for subspace projection
If
True, treats vectors as an orthonormal basis. If False, computes the orthonormal basis via SVD.Projected features with shape
[batch_size, feature_size].Usage
Project features onto a concept vector:orthogonal
Removes the component of features along specified vector(s), creating an orthogonal projection.Input features with shape
[batch_size, feature_size].Vector(s) to remove from features. Shape
[feature_size] or [num_vectors, feature_size].If
True, normalizes the resulting orthogonal projection to unit length.If
True, treats vectors as an orthonormal basis. If False, computes the orthonormal basis via SVD.Features with the specified vector component(s) removed, shape
[batch_size, feature_size].Usage
Remove unwanted concepts from feature representations:cosine_similarity
Computes pairwise cosine similarity between two sets of vectors.First set of vectors. Shape
[batch_size, feature_size] for 2D or [batch_size, seq_len, feature_size] for 3D.Second set of vectors. If
None, computes similarity of x1 with itself. Must have same dimensionality as x1.Small epsilon value to prevent division by zero during normalization.
Cosine similarity matrix. Shape depends on input dimensions:
- 2D inputs:
[batch_size_x1, batch_size_x2] - 3D inputs:
[batch_size, seq_len_x1, seq_len_x2]
Usage
cosine_distance
Computes cosine distance (1 - cosine similarity) between vectors.First set of vectors.
Second set of vectors. If
None, uses x1.Small epsilon value for numerical stability.
Cosine distance matrix with same shape as cosine_similarity output.
Usage
knn
Computes k-nearest neighbor anomaly scores based on cosine distance.Training set features with shape
[num_train, feature_size].Test set features with shape
[num_test, feature_size].Number of nearest neighbors to consider. Automatically capped at
num_train if larger.Anomaly scores for test samples, shape
[num_test]. Lower scores indicate samples more similar to training data.Usage
Detect anomalies using k-nearest neighbors:pca
Computes principal components of vectors using randomized SVD.Input vectors with shape
[num_samples, feature_size].Number of principal components to compute. If
None, computes min(num_samples, feature_size) components.If
True, centers the data by subtracting the mean before computing PCA.Number of iterations for the randomized SVD algorithm.
Principal component vectors with shape
[n_components, feature_size].Usage
align_vectors
Aligns vectors to have consistent sign relative to a reference vector.Vectors to align, shape
[num_vectors, feature_size].Index of the vector to use as reference for alignment.
Aligned vectors with shape
[num_vectors, feature_size]. Vectors are flipped if they have negative cosine similarity with the reference.Usage
Ensure consistent direction across related vectors:prompt_pair
Computes pairwise differences between prompt embeddings, useful for analyzing prompt variations.One or more prompt embedding tensors:
- Single tensor: Computes all pairwise differences within one set of prompts
[num_prompts, feature_size] - Multiple tensors: Computes pairwise differences between different sets of prompts
Aligned pairwise differences between prompts. Shape
[num_pairs, feature_size] where:- Single input:
num_pairs = num_prompts * (num_prompts - 1) / 2 - Multiple inputs:
num_pairs = sum of all cross-combinations
