📘
Token Embedding Lookup
Implement a token embedding lookup table.
Given a vocabulary size V, embedding dimension D, and a sequence of token IDs, create a random embedding matrix (using numpy seed 42) and return the embeddings for each token.
Input:
- Line 1: V D (vocab size, embedding dimension)
- Line 2: space-separated token IDs
Output: The embedding matrix for the input tokens, one row per token, values rounded to 4 decimal places.
Example:
Input:
5 3 0 2 4
Output:
[[ 0.4967 -0.1383 0.6477] [ 0.5426 -0.4634 -0.4657] [-0.2349 0.2767 -0.3539]]
Reasoning:
- We create a random embedding matrix of size V×D (5 x 3 in this case) using numpy with seed 42.
- The input token IDs are used to index into this embedding matrix: tokens 0, 2, and 4 correspond to rows 0, 2, and 4 of the matrix.
- Since token ID 4 is out of range for the given vocabulary size 5, we consider the actual indexing to be modulo V, so token ID 4 corresponds to row 4 % 5 = 4, which is the last row of the matrix.
- The embeddings for the input tokens are retrieved from the embedding matrix and rounded to 4 decimal places, resulting in the output [[ 0.4967 -0.1383 0.6477], [ 0.5426 -0.4634 -0.4657], [-0.2349 0.2767 -0.3539]].
Constraints:
- Use numpy with seed 42: np.random.seed(42); embeddings = np.random.randn(V, D)
- 1 <= V <= 100, 1 <= D <= 10
- Token IDs are in range [0, V-1]
- Round output to 4 decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.