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

We can actually find an expression to compute them as a weighted sum of thecoordinates for both the first (x 1st ) and the second (x 2nd ) corners included in theregion being convolved:Equation 8.17 - Equation for "edge feature"From the expression above, and given that the coordinates' values are close to one(in absolute value), the only way for the edge feature to have a positive value is forx 1st 1 and x 2nd 0 to be approximately -1 and 1, respectively. This is the case for twoedges only, AD and DC:Equation 8.18 - Detected edgesEvery other edge will return a negative value and thus be clipped at zero by theReLU activation function. Our model learned to choose two edges with the samedirection to perform the classification."Why two edges? Shouldn’t a single edge suffice?"It should if our sequences actually had four edges … but they don’t. We do havefour corners, but we can only build three edges out of it because we’re missing theedge connecting the last and the first corners. So, any model that relies on a singleedge will likely fail in those cases where that particular edge is the missing one.Thus, the model needs to correctly classify at least two edges.Putting It All TogetherIn this chapter, we’ve used different recurrent neural networks, plain-vanillaRNNs, GRUs, and LSTMs, to produce a hidden state representing each sequencethat can be used for sequence classification. We used both fixed- and variablelengthsequences, padding or packing them with the help of a collate function, andbuilt models that ensured the right shape of the data.Fixed-Length DatasetFor fixed-length sequences, the data preparation was as usual:Putting It All Together | 679

Data Generation & Preparation1 points, directions = generate_sequences(n=128, seed=13)2 train_data = TensorDataset(3 torch.as_tensor(points).float(),4 torch.as_tensor(directions).view(-1, 1).float()5 )6 train_loader = DataLoader(7 train_data, batch_size=16, shuffle=True8 )Variable-Length DatasetFor variable-length sequences, though, we built a custom dataset and a collatefunction to pack the sequences:Data Generation1 var_points, var_directions = generate_sequences(variable_len=True)Data Preparation1 class CustomDataset(Dataset):2 def __init__(self, x, y):3 self.x = [torch.as_tensor(s).float() for s in x]4 self.y = torch.as_tensor(y).float().view(-1, 1)56 def __getitem__(self, index):7 return (self.x[index], self.y[index])89 def __len__(self):10 return len(self.x)1112 train_var_data = CustomDataset(var_points, var_directions)680 | Chapter 8: Sequences

Data Generation & Preparation

1 points, directions = generate_sequences(n=128, seed=13)

2 train_data = TensorDataset(

3 torch.as_tensor(points).float(),

4 torch.as_tensor(directions).view(-1, 1).float()

5 )

6 train_loader = DataLoader(

7 train_data, batch_size=16, shuffle=True

8 )

Variable-Length Dataset

For variable-length sequences, though, we built a custom dataset and a collate

function to pack the sequences:

Data Generation

1 var_points, var_directions = generate_sequences(variable_len=True)

Data Preparation

1 class CustomDataset(Dataset):

2 def __init__(self, x, y):

3 self.x = [torch.as_tensor(s).float() for s in x]

4 self.y = torch.as_tensor(y).float().view(-1, 1)

5

6 def __getitem__(self, index):

7 return (self.x[index], self.y[index])

8

9 def __len__(self):

10 return len(self.x)

11

12 train_var_data = CustomDataset(var_points, var_directions)

680 | Chapter 8: Sequences

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

Saved successfully!

Ooh no, something went wrong!