Skip to content

vmfstream.projection

From sentence embeddings to points on the unit sphere.

Classes

NameDescription
ProjectionCentre, keep the leading principal directions, and scale to unit length.

Projectionclass#

class Projection(dim: int = 256)

Centre, keep the leading principal directions, and scale to unit length.

Sentence embeddings share a large common component that would put every text on the same side of the sphere; removing the mean of a reference sample takes it away. The projection is fitted once, by principal components, on data that is not the stream to be read.

Parameters

NameTypeDefaultDescription
dimint256Number of principal directions kept, the dimension d of the sphere.

Attributes

NameTypeDescription
meannp.ndarray | NoneMean of the reference sample, [D].
componentsnp.ndarray | NoneThe kept principal directions, leading first, [d, D].
explainednp.ndarray | NoneShare of the sample’s variance along each kept direction, [d].

componentsattributeinstance attribute#

components: np.ndarray | None = None

dimattributeinstance attribute#

dim = dim

explainedattributeinstance attribute#

explained: np.ndarray | None = None

meanattributeinstance attribute#

mean: np.ndarray | None = None

fittedproperty#

fitted: bool

fitmethod#

def fit(embeddings: np.ndarray) -> Projection

Fit the mean and the dim leading principal directions of a sample.

Parameters

NameTypeDescription
embeddingsnp.ndarrayReference sample [n, D], for instance a development stream.

loadmethodclassmethod#

def load(path: str | Path) -> Projection

savemethod#

def save(path: str | Path) -> None

transformmethod#

def transform(embeddings: np.ndarray) -> np.ndarray

Project embeddings [n, D] (or one [D]) to unit vectors [n, d] (or [d]).