PythiloLesson 6 of 8 · progress kept in this browser

Shift language-model targets instead of predicting the present

A next-token objective pairs the logits at position t with the token at position t+1. Scoring a position against its own input can produce a deceptively easy task and a misleading loss. The final input position has no next token in the provided sequence and must be excluded.

This exercise computes actual cross-entropy on shifted tensors. It leaves attention construction outside the contract: a correct target shift is still invalid if the model's attention can see future tokens. Sequence packing also needs boundary masks so an unrelated document is not silently treated as the continuation of another.

import torch
ids=torch.tensor([[2,4,1]])
print(ids[:,:-1])
print(ids[:,1:])
This example uses torch. Run it in the Pythilo app, where these features are available.
Your task

Implement next_token_loss(logits,tokens), logits [B,T,C], tokens [B,T], T>=2. Score logits[:,:-1] against tokens[:,1:] with mean cross entropy. Reject T<2.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.