Start learning free

LESSON 150 OF 165 · MACHINE LEARNING: BUILD YOUR OWN LLM

Inputs and Targets

Practice

A language model learns one job: predict the next character. To train it, you need questions with their answers, and the text already holds both. Take a window of 8 ids as the input x, and the same window moved one place to the right as the target y: at every position, the target is the id that comes next.

First the whole of names.txt becomes one long row of ids: torch.tensor(list) gives back a tensor holding the list's numbers. Then get_batch cuts 32 windows from random places. torch.randint(high, (n,), generator=g) gives back n random whole numbers from 0 to high - 1; drawn with the seeded generator g, they are the same on every run. .tolist() turns a tensor into a plain Python list, and torch.stack, already in the code, piles the 32 windows into one tensor of shape [32, 8].

Here is the shift on a short list of ids, with windows of 3:

ids = [5, 2, 9, 4, 0, 7]
x = ids[1:4]   # [2, 9, 4]: the input
y = ids[2:5]   # [9, 4, 0]: the next id at each position

A window of 8 asks 8 questions, and a batch of 32 windows asks 256. Once your code works, the check prints:

data: [3360] the first ten: [8, 11, 18, 1, 0, 9, 6, 13, 11, 15]
x: [32, 8] y: [32, 8]
x[0]: [7, 4, 8, 11, 8, 11, 0, 13] 'kelolo\nr'
y[0]: [4, 8, 11, 8, 11, 0, 13, 6] 'elolo\nri'
after 'k' comes 'e'
after 'ke' comes 'l'
after 'kel' comes 'o'
after 'kelo' comes 'l'
after 'kelol' comes 'o'
after 'kelolo' comes '\n'
after 'kelolo\n' comes 'r'
after 'kelolo\nr' comes 'i'
y is x moved by one in every row: True
questions in one batch: 256
the next batch is new windows: True

Your task: the vocabulary, encode and decode from the earlier lessons are in the starting code. Turn the whole text into the tensor data. Then finish get_batch: draw batch_size random start positions, and for each one append the input window to xs and the window one id later to ys.


Assignment
Follow the Coding Tutorial to cut a batch of input windows and their target windows from the names.


Hint
The target window starts and ends one place later than the input window. Draw the starts below len(data) - block_size, so every window still has room for its last target.


STEP 1 OF 5
    Ctrl + Enter to check