Example 1
Arguments: [
[
[
2
]
],
[
0
],
[
[
1
]
]
]
Expected result: [
[
4.000000000026205
]
]The only logit is e²=4, whose derivative at e=2 is 4.
Tensor and numerical foundations · Hard
Problem 10 of 50 · Python and NumPy
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):Arguments: [
[
[
2
]
],
[
0
],
[
[
1
]
]
]
Expected result: [
[
4.000000000026205
]
]The only logit is e²=4, whose derivative at e=2 is 4.
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.