Tous les défis
Moyen 0 résolus 0 % de réussite

Sinusoidal Positional Encoding

Compute the sine and cosine position vectors Transformers add to token embeddings.

Ce que vous allez apprendre

  • Compute sine and cosine position vectors
  • Use different frequencies per dimension
  • Understand how Transformers know word order

Problème

Afficher la traduction

Attention ignores word order, so the original Transformer adds a positional encoding to each token embedding. For position pos and dimension j (both starting at 0) in a model of size d_model:

  • even j: PE[pos][j] = sin(pos / 10000^(j / d_model))
  • odd j: PE[pos][j] = cos(pos / 10000^((j - 1) / d_model))

So dimensions 0 and 1 share one frequency, dimensions 2 and 3 the next, and so on.

Implement positional_encoding(length, d_model).

Input

length: The number of positions (0 to length - 1).

d_model: The embedding size.

Output

Return a length × d_model matrix.

Contraintes

1 ≤ length ≤ 200, 1 ≤ d_model ≤ 128

Exemples

Exemple 1
Entrée :
length = 2
d_model = 4

Sortie attendue :
expected = [
    [0.0, 1.0, 0.0, 1.0],
    [0.841471, 0.540302, 0.01, 0.99995]
]
Entrée :
long long length = 2;
long long d_model = 4;

Sortie attendue :
std::vector<std::vector<double>> expected = {
    {0.0, 1.0, 0.0, 1.0},
    {0.841471, 0.540302, 0.01, 0.99995}
};
Entrée :
long length = 2;
long d_model = 4;

Sortie attendue :
double[][] expected = new double[][] {
    new double[] {0.0, 1.0, 0.0, 1.0},
    new double[] {0.841471, 0.540302, 0.01, 0.99995}
};

Explication

Position 0 is sin(0) = 0 and cos(0) = 1 for every pair: [0, 1, 0, 1]. Position 1 uses frequencies 1 and 1 / 10000^(2/4) = 0.01: [sin 1, cos 1, sin 0.01, cos 0.01] ≈ [0.841471, 0.540302, 0.01, 0.99995].

Exemple 2
Entrée :
length = 3
d_model = 1

Sortie attendue :
expected = [
    [0.0],
    [0.841471],
    [0.909297]
]
Entrée :
long long length = 3;
long long d_model = 1;

Sortie attendue :
std::vector<std::vector<double>> expected = {
    {0.0},
    {0.841471},
    {0.909297}
};
Entrée :
long length = 3;
long d_model = 1;

Sortie attendue :
double[][] expected = new double[][] {
    new double[] {0.0},
    new double[] {0.841471},
    new double[] {0.909297}
};

Explication

With one dimension only sin(pos) is used: [[0], [0.841471], [0.909297]].

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Each column belongs to a frequency pair, with even and odd dimensions using sine and cosine of the same angle.

Indice 2

Build the matrix by iterating positions and dimensions, computing the angle from the position and the dimension pair.

Indice 3

Be careful with integer division and zero-based indexing so the first row at position 0 becomes the expected starting pattern.

Complexité attendue

O(length * d_model) time, O(length * d_model) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.