v9.1.1
Menu

Token Embedding Lookup

easyFREEEmbeddings & Positional EncodingLLM 1: Foundations

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.

Function: token_embedding_lookup(V, D, token_ids)

  • V: vocab size (int)
  • D: embedding dimension (int)
  • token_ids: list of token IDs

Output: A numpy array of shape (len(token_ids), D) with the embedding row for each input token, in input order. The tests print each row with values rounded to 4 decimal places.

Example

Input
5 3
0 2 4
Output
[0.4967 -0.1383 0.6477]
[1.5792 0.7674 -0.4695]
[0.2420 -1.9133 -1.7249]
Reasoning
  • Inputs: V=5V = 5 vocabulary entries, each embedded in D=3D = 3 dimensions, and token_ids holds the 3 IDs 0,2,40, 2, 4, each in [0,V−1][0, V-1].
  • The embedding table EE is a 5×35 \times 3 matrix of standard-normal numbers generated with seed 42, identical on every call. Row ii is the vector for token ii; for example E1=[1.5230,−0.2342,−0.2341]E_1 = [1.5230, -0.2342, -0.2341] and E3=[0.5426,−0.4634,−0.4657]E_3 = [0.5426, -0.4634, -0.4657].
  • Each ID tkt_k is replaced by its row EtkE_{t_k}: token 0 gives E0=[0.4967,−0.1383,0.6477]E_0 = [0.4967, -0.1383, 0.6477], token 2 gives E2=[1.5792,0.7674,−0.4695]E_2 = [1.5792, 0.7674, -0.4695], token 4 gives E4=[0.2420,−1.9133,−1.7249]E_4 = [0.2420, -1.9133, -1.7249].
  • Output: these rows stacked in input order, a 3×33 \times 3 matrix: one row per token, DD columns, shown to 4 decimal places.
  • Notice that rows 1 and 3 never appear: the row count follows the sequence length, not VV; a repeated ID would yield the same row twice. The random draws fill the table row by row, so with D=2D = 2 instead, E1E_1 would hold different numbers.

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

Learn this in

More from LLM 1: Foundations

solution.py

Test results

Sign in to run your code against the test cases.