explain this in plain english with numerical examples: Improving training time with…
- User
explain this in plain english with numerical examples:
Improving training time with packing When building this model, we used a batch size of 1. This is not a very efficient way to train a model. What happens if you change the batch size to 2? Go on, try it. You should get an error message. The problem is that each name is a different length, so by default, it is very hard to represent as a tensor. Tensors need all dimensions to be consistent and the same, but our time dimension (in this case, how many characters long each name is) is not consistent. This is what causes the error: we have two different sequences with different lengths. We saw in chapter 2 that training on batches of data can reduce the training time significantly by making better use of our GPU’s compute, so we have a real reason to increase the batch size. Yet we appear to be stuck using an inefficient batch size of 1 due to inputs with differing sizes. This is an abstraction problem because fundamentally, nothing prevents an RNN from working with inputs of varying lengths of time. To fix this, PyTorch provides the packed sequence abstraction (http://mng.bz/4KYV). All types of RNNs available in PyTorch support working on this class. Figure 4.13 shows conceptually what is happening when we pack six different sequences of varying lengths. PyTorch organizes them by length and begins by including the first time step of all sequences in one batch over time. As time progresses, the shorter sequences reach their end, and the batch size is reduced to the number of sequences that have not reached their end.134 CHAPTER 4 Recurrent neural networks b e n s a m e d w a r d f r a n l u c y k Batch of 5 Batch of 3 Batch of 2 Batch of 1 PAD PAD PAD PAD PAD PAD PAD PAD PAD Batch of 5 items and 6 steps, but not everything has the same length After 3 steps, the shortest 2 sequences are done. So we stop processing them, “shrinking” the batch size. Figure 4.13 Example of packing a batch of five items with sequence lengths of 3, 3, 4, 5, and 6. For the first three steps, we process all five sequences. On the fourth step t = 4, two of the sequences have run out, so we drop them and continue the batch on only the sequences that have a length ≥ 4. Packing organizes the data by length to make this fast and efficient. Let’s talk through what is happening in this example. We are trying to train on a batch of five names: “ben,” “sam,” “lucy,” “frank,” and “edward.” The packing process has sorted them from shortest to longest. There will be a total of T = 6 steps in this sequence because “edward” is the longest name at six characters. This means our RNN will perform six iterations in total. Now let’s see what happens at each step: 1 In the first iteration, all five items are processed together in one large batch. 2 All five items are again processed together in one large batch. 3 All five items are again processed, but we have reached the end of the first two items, “ben” and “sam.” We record their final hidden states from this step h3, since that’s when they were finished being processed. 4 A batch of only three items is processed, containing “lucy,” “frank,” and “edward,” because the first two items are done. PyTorch adaptively shrinks the effective batch size to what remains. “lucy” is finished, and its final state saved as h4. 5 A batch of only two items is processed. “frank” is finished after this step, so h5 is saved for “frank.” 6 The last step processes only one item, “edward,” bringing us to the end of the batch and the final hidden state: h6.Improving training time with packing 135 Processing this way ensures that we get the correct hidden state hT for each item even though they have different lengths. It also makes PyTorch run faster. We do more efficient batched computation as much as possible and shrink that batch as needed so we only work on the still-valid data. 4.3.1 Pad and pack Packing actually involves two steps: padding (make everything the same length) and packing (store information about how much padding was used). To implement both padding and packing, we need to override the collate function used by the DataLoader. This function’s job is to collate lots of independent data points into one larger batch of items. The default collate function can handle tensors as long as all tensors have the same shape. Our data comes in as a tuple (xi; yi) with shapes (Ti) and (1), respectively. Ti can be different for each item, which is normally a problem, so we need to define a new function called pad_and_pack. It’s a two-step process that is outlined in figure 4.14. def pad_and_pack(batch): List of tuples where
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.