Skip to main content

Positional Embedding

Function Signature

def embed_position(sequence: np.ndarray) -> np.ndarray:

Parameters

  • sequence (np.ndarray): A 2D numpy array where the first dimension represents the sequence length and the second represents the embedding dimension.

Return Value

Returns a 2D numpy array with positional embeddings applied to the input sequence.

Description

The embed_position function applies positional embedding to a given sequence. The positional embedding is computed using a Cython implementation, which is expected to be faster than a pure Python implementation. The purpose of this embedding is to provide the model with information about the position of elements in the sequence, which can be crucial for certain tasks such as sequence-to-sequence modeling.

Examples

Here's a basic example to demonstrate how to use the embed_position function:

import numpy as np
from deeprai.embedding.positional_embedding import embed_position

# Create a mock sequence
sequence = np.array([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]])

# Apply positional embedding
embedded_sequence = embed_position(sequence)

print(embedded_sequence)