Tensor and numerical foundations · Hard

Gradients for Tied Token Embeddings

Problem 10 of 50 · Python and NumPy

Problem

A tiny tied language model looks up hidden = embedding[token_ids], then computes logits = hidden @ embedding.T. Given dlogits, return the total derivative of the scalar loss with respect to embedding.

The same matrix participates in both the input lookup and output projection. Include both contributions, and accumulate correctly when token_ids contains repeats. No softmax or transformer is part of this problem.

def tied_embedding_gradient(embedding, token_ids, dlogits):

Constraints

  • embedding has shape V × D, token_ids length T, dlogits shape T × V.
  • 1 ≤ V,D,T ≤ 8; every token ID is in [0,V).
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      2
    ]
  ],
  [
    0
  ],
  [
    [
      1
    ]
  ]
]
Expected result: [
  [
    4.000000000026205
  ]
]

The only logit is e²=4, whose derivative at e=2 is 4.

Example 2

Arguments: [
  [
    [
      1
    ],
    [
      2
    ]
  ],
  [
    0,
    0
  ],
  [
    [
      1,
      0
    ],
    [
      0,
      1
    ]
  ]
]
Expected result: [
  [
    4.000000000004
  ],
  [
    1.0000000000065512
  ]
]

Token 0 is looked up twice; both lookup derivatives must be added, alongside the output projection gradient.

Sign in to solve this problem

Browse all coding problems · Learn the foundations