Tất cả thử thách
Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Sinusoidal Positional Encoding

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ length ≤ 200, 1 ≤ d_model ≤ 128

Ví dụ

Ví dụ 1
Đầu vào:
length = 2
d_model = 4

Kết quả mong đợi:
expected = [
    [0.0, 1.0, 0.0, 1.0],
    [0.841471, 0.540302, 0.01, 0.99995]
]
Đầu vào:
long long length = 2;
long long d_model = 4;

Kết quả mong đợi:
std::vector<std::vector<double>> expected = {
    {0.0, 1.0, 0.0, 1.0},
    {0.841471, 0.540302, 0.01, 0.99995}
};
Đầu vào:
long length = 2;
long d_model = 4;

Kết quả mong đợi:
double[][] expected = new double[][] {
    new double[] {0.0, 1.0, 0.0, 1.0},
    new double[] {0.841471, 0.540302, 0.01, 0.99995}
};

Giải thích

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].

Ví dụ 2
Đầu vào:
length = 3
d_model = 1

Kết quả mong đợi:
expected = [
    [0.0],
    [0.841471],
    [0.909297]
]
Đầu vào:
long long length = 3;
long long d_model = 1;

Kết quả mong đợi:
std::vector<std::vector<double>> expected = {
    {0.0},
    {0.841471},
    {0.909297}
};
Đầu vào:
long length = 3;
long d_model = 1;

Kết quả mong đợi:
double[][] expected = new double[][] {
    new double[] {0.0},
    new double[] {0.841471},
    new double[] {0.909297}
};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.