Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
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)But this is not enough; if we create a data loader for our custom dataset and try toretrieve a mini-batch out of it, it will raise an error:train_var_loader = DataLoader(train_var_data, batch_size=16, shuffle=True)next(iter(train_var_loader))Output-----------------------------------------------------------------RuntimeErrorTraceback (most recent call last)
the padded data points, as we’ve already discussed.We can do better than that: We can pack our mini-batches using a collate function.Collate FunctionThe collate function takes a list of tuples (sampled from a dataset using its__getitem__()) and collates them into a batch that’s being returned by the dataloader. It gives you the ability to manipulate the sampled data points in any wayyou want to make them into a mini-batch.In our case, we’d like to get all sequences (the first item in every tuple) and packthem. Besides, we can get all labels (the second item in every tuple) and make theminto a tensor that’s in the correct shape for our binary classification task:Data Preparation1 def pack_collate(batch):2 X = [item[0] for item in batch]3 y = [item[1] for item in batch]4 X_pack = rnn_utils.pack_sequence(X, enforce_sorted=False)56 return X_pack, torch.as_tensor(y).view(-1, 1)Let’s see the function in action by creating a dummy batch of two elements andapplying the function to it:# list of tuples returned by the datasetdummy_batch = [train_var_data[0], train_var_data[1]]dummy_x, dummy_y = pack_collate(dummy_batch)dummy_x666 | Chapter 8: Sequences
- Page 640 and 641: For bidirectional RNNs, the last el
- Page 642 and 643: Model Configuration1 class SquareMo
- Page 644 and 645: StepByStep.loader_apply(test_loader
- Page 646 and 647: Figure 8.14 - Final hidden states f
- Page 648 and 649: Figure 8.16 - Transforming the hidd
- Page 650 and 651: Since the RNN cell has both of them
- Page 652 and 653: Every gate worthy of its name will
- 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 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 704 and 705: We can actually find an expression
- 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
the padded data points, as we’ve already discussed.
We can do better than that: We can pack our mini-batches using a collate function.
Collate Function
The collate function takes a list of tuples (sampled from a dataset using its
__getitem__()) and collates them into a batch that’s being returned by the data
loader. It gives you the ability to manipulate the sampled data points in any way
you want to make them into a mini-batch.
In our case, we’d like to get all sequences (the first item in every tuple) and pack
them. Besides, we can get all labels (the second item in every tuple) and make them
into a tensor that’s in the correct shape for our binary classification task:
Data Preparation
1 def pack_collate(batch):
2 X = [item[0] for item in batch]
3 y = [item[1] for item in batch]
4 X_pack = rnn_utils.pack_sequence(X, enforce_sorted=False)
5
6 return X_pack, torch.as_tensor(y).view(-1, 1)
Let’s see the function in action by creating a dummy batch of two elements and
applying the function to it:
# list of tuples returned by the dataset
dummy_batch = [train_var_data[0], train_var_data[1]]
dummy_x, dummy_y = pack_collate(dummy_batch)
dummy_x
666 | Chapter 8: Sequences