Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Like the GRU, the LSTM presents four distinct groups of sequences correspondingto the different starting corners. Moreover, it is able to classify most sequencescorrectly after seeing only three points.Variable-Length SequencesSo far, we’ve been working with full, regular sequences of four data points each,and that’s nice. But what do you do if you get variable-length sequences, like theones below:x0 = points[0] # 4 data pointsx1 = points[1][2:] # 2 data pointsx2 = points[2][1:] # 3 data pointsx0.shape, x1.shape, x2.shapeOutput((4, 2), (2, 2), (3, 2))The answer: You pad them!"Could you please remind me again what padding is?"Sure! Padding means stuffing with zeros. We’ve seen padding in Chapter 5 already:We used it to stuff an image with zeros around it in order to preserve its originalsize after being convolved.Variable-Length Sequences | 653

Padding in Computer VisionPadding an image simply means adding zeros around it. An image is worth athousand words in this case.By adding columns and rows of zeros around it, we expand the input imagesuch that the gray region starts centered in the actual top-left corner of theinput image. This simple trick can be used to preserve the original size of theimage.PaddingNow, we’ll stuff sequences with zeros so they all have matching sizes. Simpleenough, right?"OK, it is simple, but why are we doing it?"We need to pad the sequences because we cannot create a tensor out of a list ofelements with different sizes:all_seqs = [s0, s1, s2]torch.as_tensor(all_seqs)654 | Chapter 8: Sequences

Like the GRU, the LSTM presents four distinct groups of sequences corresponding

to the different starting corners. Moreover, it is able to classify most sequences

correctly after seeing only three points.

Variable-Length Sequences

So far, we’ve been working with full, regular sequences of four data points each,

and that’s nice. But what do you do if you get variable-length sequences, like the

ones below:

x0 = points[0] # 4 data points

x1 = points[1][2:] # 2 data points

x2 = points[2][1:] # 3 data points

x0.shape, x1.shape, x2.shape

Output

((4, 2), (2, 2), (3, 2))

The answer: You pad them!

"Could you please remind me again what padding is?"

Sure! Padding means stuffing with zeros. We’ve seen padding in Chapter 5 already:

We used it to stuff an image with zeros around it in order to preserve its original

size after being convolved.

Variable-Length Sequences | 653

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!