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

import copyimport numpy as npimport torchimport torch.optim as optimimport torch.nn as nnimport torch.nn.functional as Ffrom torch.utils.data import DataLoader, Dataset, random_split, \TensorDatasetfrom data_generation.square_sequences import generate_sequencesfrom stepbystep.v4 import StepByStepSequence-to-SequenceSequence-to-sequence problems are more complex than those we handled in thelast chapter. There are two sequences now: the source and the target. We use theformer to predict the latter, and they may even have different lengths.A typical example of a sequence-to-sequence problem is translation: A sentencegoes in (a sequence of words in English), and another sentence comes out (asequence of words in French). This problem can be tackled using an encoderdecoderarchitecture, first described in the "Sequence to Sequence Learning withNeural Networks" [138] paper by Sutskever, I., et al.Translating languages is an obviously difficult task, so we’re falling back to a muchsimpler problem to illustrate how the encoder-decoder architecture works.Data GenerationWe’ll keep drawing the same squares as before, but this time we’ll draw the firsttwo corners ourselves (the source sequence) and ask our model to predict thenext two corners (the target sequence). As with every sequence-related problem,the order is important, so it is not enough to get the corners' coordinates right;they should follow the same direction (clockwise or counterclockwise).Sequence-to-Sequence | 687

Figure 9.1 - Drawing first two corners, starting at A and moving toward either D or BSince there are four corners to start from and two directions to follow, there areeffectively eight possible sequences (solid colors indicate the corners in the sourcesequence, semi-transparent colors, the target sequence).Figure 9.2 - Possible sequences of cornersSince the desired output of our model is a sequence of coordinates (x 0 , x 1 ), we’redealing with a regression problem now. Therefore, we’ll be using a typical meansquared error loss to compare the predicted and actual coordinates for the twopoints in the target sequence.Let’s generate 256 random noisy squares:Data Generation1 points, directions = generate_sequences(n=256, seed=13)And then let’s visualize the first five squares:fig = plot_data(points, directions, n_rows=1)688 | Chapter 9 — Part I: Sequence-to-Sequence

import copy

import numpy as np

import torch

import torch.optim as optim

import torch.nn as nn

import torch.nn.functional as F

from torch.utils.data import DataLoader, Dataset, random_split, \

TensorDataset

from data_generation.square_sequences import generate_sequences

from stepbystep.v4 import StepByStep

Sequence-to-Sequence

Sequence-to-sequence problems are more complex than those we handled in the

last chapter. There are two sequences now: the source and the target. We use the

former to predict the latter, and they may even have different lengths.

A typical example of a sequence-to-sequence problem is translation: A sentence

goes in (a sequence of words in English), and another sentence comes out (a

sequence of words in French). This problem can be tackled using an encoderdecoder

architecture, first described in the "Sequence to Sequence Learning with

Neural Networks" [138] paper by Sutskever, I., et al.

Translating languages is an obviously difficult task, so we’re falling back to a much

simpler problem to illustrate how the encoder-decoder architecture works.

Data Generation

We’ll keep drawing the same squares as before, but this time we’ll draw the first

two corners ourselves (the source sequence) and ask our model to predict the

next two corners (the target sequence). As with every sequence-related problem,

the order is important, so it is not enough to get the corners' coordinates right;

they should follow the same direction (clockwise or counterclockwise).

Sequence-to-Sequence | 687

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

Saved successfully!

Ooh no, something went wrong!