22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• L stands for the Length of each sequence

"Wait, where is the number of features in it?"

Good point! Since 1D convolutions only move along the sequence, each feature is

considered an input channel. So, you can think of the shape as NFL or N(C/F)L if

you like.

"Really?! Yet another shape for input sequences?"

Unfortunately, yes. But I’ve built this small table to help you wrap your head

around the different shape conventions while working with sequences as inputs:

Shape

Use Case

Batch-first N, L, F Typical shape; RNNs with batch_first=True

RNN-friendly L, N, F Default for RNNs (batch_first=False)

Sequence-last N, F, L Default for 1D convolutions

Having (hopefully) cleared that up, let’s use permute to get our sequences in the

appropriate shape:

seqs = torch.as_tensor(points).float() # N, L, F

seqs_length_last = seqs.permute(0, 2, 1)

seqs_length_last.shape # N, F=C, L

Output

torch.Size([128, 2, 4])

Multiple Features or Channels

Our sequences of corners have two coordinates; that is, two features. These will

be considered (input) channels as far as the 1D convolution is concerned, so we

create the convolutional layer accordingly:

672 | Chapter 8: Sequences

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

Saved successfully!

Ooh no, something went wrong!