Skip to content

orcalib.torch_layers.embedding_similarity#

EmbeddingSimilarity #

Bases: Module, ABC

Abstract class for computing similarity between input and memory embeddings.

forward abstractmethod #

forward(input_embedding, memories_embedding)

Compute similarity scores between the given input and memory embeddings

Parameters:

  • input_embedding (Tensor) –

    input embeddings, float tensor of shape batch_size x embedding_dim

  • memories_embedding (Tensor) –

    memory embeddings, float tensor of shape batch_size (x num_memories) x embedding_dim

Returns:

  • Tensor

    similarity scores between 0 and 1 for each memory in each batch, float tensor of shape batch_size (x num_memories)

FeedForwardSimilarity #

FeedForwardSimilarity(embedding_dim)

Bases: EmbeddingSimilarity

Module to compute the similarity between input and memory embeddings using a feedforward network with two hidden layers and an output layer that returns sigmoid activated scores between 0 and 1.

Warning

Unlike other similarity heads, this layer has trainable parameters and will not output meaningful similarity scores unless trained on a dataset of input-memory pairs.

CosineSimilarity #

CosineSimilarity()

Bases: EmbeddingSimilarity

A shallow wrapper around torch.nn.CosineSimilarity that supports scoring multiple memories at once.

InnerProductSimilarity #

InnerProductSimilarity()

Bases: EmbeddingSimilarity

Module to compute the inner product between input and memory embeddings.

Note

Inner product is equivalent to cosine similarity when the input embeddings are normalized. It will only output scores between 0 and 1 when the input embeddings are normalized.