Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
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
- Page 654 and 655: • For r=0 and z=0, the cell becom
- Page 656 and 657: In code, we can use split() to get
- Page 658 and 659: Let’s pause for a moment here. Fi
- Page 660 and 661: Square Model II — The QuickeningT
- Page 662 and 663: Outputtensor([[53, 53],[75, 75]])Th
- Page 664 and 665: Figure 8.22 - Transforming the hidd
- Page 666 and 667: Equation 8.9 - LSTM—candidate hid
- Page 668 and 669: Now, let’s visualize the internal
- Page 670 and 671: OutputOrderedDict([('weight_ih', te
- Page 672 and 673: def forget_gate(h, x):thf = f_hidde
- Page 674 and 675: Outputtensor([[-5.4936e-02, -8.3816
- Page 676 and 677: 1 First change: from RNN to LSTM2 S
- Page 678 and 679: Like the GRU, the LSTM presents fou
- Page 680 and 681: Output-----------------------------
- Page 682 and 683: Before moving on to packed sequence
- Page 684 and 685: column-wise fashion, from top to bo
- Page 686 and 687: does match the last output.• No,
- Page 688 and 689: So, to actually get the last output
- Page 690 and 691: Data Preparation1 class CustomDatas
- Page 692 and 693: OutputPackedSequence(data=tensor([[
- Page 694 and 695: Model Configuration & TrainingWe ca
- Page 696 and 697: size = 5weight = torch.ones(size) *
- Page 698 and 699: torch.manual_seed(17)conv_seq = nn.
- Page 700 and 701: Figure 8.32 - Applying dilated filt
- Page 702 and 703: Model Configuration1 torch.manual_s
- Page 706 and 707: Data Preparation1 def pack_collate(
- Page 708 and 709: and variable-length sequences.Model
- Page 710 and 711: • generating variable-length sequ
- Page 712 and 713: import copyimport numpy as npimport
- Page 714 and 715: Figure 9.3 - Sequence datasetThe co
- Page 716 and 717: coordinates of a "perfect" square a
- Page 718 and 719: Let’s pretend for a moment that t
- Page 720 and 721: to initialize the hidden state and
- Page 722 and 723: predictions in previous steps have
- Page 724 and 725: the second set of predicted coordin
- Page 726 and 727: Let’s create an instance of the m
- Page 728 and 729: Model Configuration & TrainingThe m
- Page 730 and 731: Sure, we can!AttentionHere is a (no
- Page 732 and 733: based on "the" and "zone," I’ve j
- Page 734 and 735: Figure 9.12 - Matching a query to t
- Page 736 and 737: Outputtensor([[[ 0.0832, -0.0356],[
- Page 738 and 739: utmost importance for the correct i
- Page 740 and 741: Its formula is:Equation 9.3 - Cosin
- Page 742 and 743: second hidden state contributes to
- Page 744 and 745: Outputtensor([[[ 0.5475, 0.0875, -1
- Page 746 and 747: alphas = F.softmax(scaled_products,
- Page 748 and 749: Outputtensor([[[ 0.2138, -0.3175]]]
- Page 750 and 751: Attention Mechanism1 class Attentio
- Page 752 and 753: "Why would I want to force it to do
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