Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
Figure 10.18 - Losses - PyTorch’s TransformerOnce again, the validation loss is significantly lower than the training loss. Nosurprises here since it is roughly the same model.Visualizing PredictionsLet’s plot the predicted coordinates and connect them using dashed lines, whileusing solid lines to connect the actual coordinates, just like before.Figure 10.19 - PredictionsOnce again, looking good, right?The PyTorch Transformer | 845
Vision TransformerThe Transformer architecture is fairly flexible, and, although it was devised tohandle NLP tasks in the first place, it is already starting to spread to different areas,including computer vision. Let’s take a look at one of the latest developments in thefield: the Vision Transformer (ViT). It was introduced by Dosovitskiy, A., et al. intheir paper "An Image is Worth 16x16 Words: Transformers for Image Recognitionat Scale." [152]"Cool, but I thought the Transformer handled sequences, not images."That’s a fair point. The answer is deceptively simple: Let’s break an image into asequence of patches.Data Generation & PreparationFirst, let’s bring back our multiclass classification problem from Chapter 5. We’regenerating a synthetic dataset of images that are going to have either a diagonal ora parallel line, and labeling them according to the table below:LineLabel/Class IndexParallel (Horizontal OR Vertical) 0Diagonal, Tilted to the Right 1Diagonal, Tilted to the Left 2Data Generation1 images, labels = generate_dataset(img_size=12, n_images=1000,2 binary=False, seed=17)Each image, like the example below, is 12x12 pixels in size and has a single channel:img = torch.as_tensor(images[2]).unsqueeze(0).float()/255.846 | Chapter 10: Transform and Roll Out
- Page 820 and 821: sequential order of the data• fig
- Page 822 and 823: following imports:import copyimport
- Page 824 and 825: Figure 10.2 - Chunking: the wrong a
- Page 826 and 827: chunks to compute the other half of
- Page 828 and 829: 67 # N, L, n_heads, d_k68 context =
- Page 830 and 831: dummy_points = torch.randn(16, 2, 4
- Page 832 and 833: Stacking Encoders and DecodersLet
- Page 834 and 835: "… with great depth comes great c
- Page 836 and 837: Transformer EncoderWe’ll be repre
- Page 838 and 839: Let’s see it in code, starting wi
- Page 840 and 841: Transformer Encoder1 class EncoderT
- Page 842 and 843: of the encoder-decoder (or Transfor
- Page 844 and 845: In PyTorch, the decoder "layer" is
- Page 846 and 847: In PyTorch, the decoder is implemen
- Page 848 and 849: Equation 10.7 - Data points' means
- Page 850 and 851: layer_norm = nn.LayerNorm(d_model)n
- Page 852 and 853: Figure 10.10 - Layer norm vs batch
- Page 854 and 855: Outputtensor([[[ 1.4636, 2.3663],[
- Page 856 and 857: The TransformerLet’s start with t
- Page 858 and 859: "values") in the decoder.• decode
- Page 860 and 861: Data Preparation1 # Generating trai
- Page 862 and 863: Figure 10.15 - Losses—Transformer
- Page 864 and 865: • First, and most important, PyTo
- Page 866 and 867: decode(), with a single one, encode
- Page 868 and 869: 46 for i in range(self.target_len):
- Page 872 and 873: Figure 10.20 - Sample image—label
- Page 874 and 875: 4041 # Builds a weighted random sam
- Page 876 and 877: Figure 10.23 - Sample image—split
- Page 878 and 879: Einops"There is more than one way t
- Page 880 and 881: Figure 10.26 - Two patch embeddings
- Page 882 and 883: Now each sequence has ten elements,
- Page 884 and 885: It takes an instance of a Transform
- Page 886 and 887: Putting It All TogetherIn this chap
- Page 888 and 889: 1. Encoder-DecoderThe encoder-decod
- Page 890 and 891: This is the actual encoder-decoder
- Page 892 and 893: 3. DecoderThe Transformer decoder h
- Page 894 and 895: 5. Encoder "Layer"The encoder "laye
- Page 896 and 897: 7. "Sub-Layer" WrapperThe "sub-laye
- Page 898 and 899: 8. Multi-Headed AttentionThe multi-
- Page 900 and 901: Model Configuration & TrainingModel
- Page 902 and 903: • training the Transformer to tac
- Page 904 and 905: Part IVNatural Language Processing|
- Page 906 and 907: Additional SetupThis is a special c
- Page 908 and 909: "Down the Yellow Brick Rabbit Hole"
- Page 910 and 911: The actual texts of the books are c
- Page 912 and 913: "What is this punkt?"That’s the P
- Page 914 and 915: 14 # If there is a configuration fi
- Page 916 and 917: Sentence Tokenization in spaCyBy th
- Page 918 and 919: AttributesThe Dataset has many attr
Vision Transformer
The Transformer architecture is fairly flexible, and, although it was devised to
handle NLP tasks in the first place, it is already starting to spread to different areas,
including computer vision. Let’s take a look at one of the latest developments in the
field: the Vision Transformer (ViT). It was introduced by Dosovitskiy, A., et al. in
their paper "An Image is Worth 16x16 Words: Transformers for Image Recognition
at Scale." [152]
"Cool, but I thought the Transformer handled sequences, not images."
That’s a fair point. The answer is deceptively simple: Let’s break an image into a
sequence of patches.
Data Generation & Preparation
First, let’s bring back our multiclass classification problem from Chapter 5. We’re
generating a synthetic dataset of images that are going to have either a diagonal or
a parallel line, and labeling them according to the table below:
Line
Label/Class Index
Parallel (Horizontal OR Vertical) 0
Diagonal, Tilted to the Right 1
Diagonal, Tilted to the Left 2
Data Generation
1 images, labels = generate_dataset(img_size=12, n_images=1000,
2 binary=False, seed=17)
Each image, like the example below, is 12x12 pixels in size and has a single channel:
img = torch.as_tensor(images[2]).unsqueeze(0).float()/255.
846 | Chapter 10: Transform and Roll Out