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

Model Configuration & TrainingWe can use our data loader that outputs packed sequences (train_var_loader) tofeed our SquareModelPacked model and train it in the usual way:Model Configuration1 torch.manual_seed(21)2 model = SquareModelPacked(n_features=2, hidden_dim=2, n_outputs=1)3 loss = nn.BCEWithLogitsLoss()4 optimizer = optim.Adam(model.parameters(), lr=0.01)Model Training1 sbs_packed = StepByStep(model, loss, optimizer)2 sbs_packed.set_loaders(train_var_loader)3 sbs_packed.train(100)fig = sbs_packed.plot_losses()Figure 8.29 - Losses—SquareModelPackedStepByStep.loader_apply(train_var_loader, sbs_packed.correct)Variable-Length Sequences | 669

Outputtensor([[66, 66],[62, 62]])1D ConvolutionsIn Chapter 5, we learned about convolutions, their kernels and filters, and how toperform a convolution by repeatedly applying a filter to a moving region over theimage. Those were 2D convolutions, though, meaning that the filter was moving intwo dimensions, both along the width (left to right), and the height (top to bottom)of the image.Guess what 1D convolutions do? They move the filter in one dimension, from leftto right. The filter works like a moving window, performing a weighted sum of thevalues in the region it has moved over. Let’s use a sequence of temperature valuesover thirteen days as an example:temperatures = np.array([5, 11, 15, 6, 5, 3, 3, 0, 0, 3, 4, 2, 1])Figure 8.30 - Moving window over series of temperaturesThen, let’s use a window (filter) of size five, like in the figure above. In its first step,the window is over days one to five. In the next step, since it can only move to theright, it will be over days two to six. By the way, the size of our movement to theright is, once again, known as the stride.Now, let’s assign the same value (0.2) for every weight in our filter and usePyTorch’s F.conv1d() to convolve the filter with our sequence (don’t mind theshape just yet; we’ll get back to it in the next section):670 | Chapter 8: Sequences

Output

tensor([[66, 66],

[62, 62]])

1D Convolutions

In Chapter 5, we learned about convolutions, their kernels and filters, and how to

perform a convolution by repeatedly applying a filter to a moving region over the

image. Those were 2D convolutions, though, meaning that the filter was moving in

two dimensions, both along the width (left to right), and the height (top to bottom)

of the image.

Guess what 1D convolutions do? They move the filter in one dimension, from left

to right. The filter works like a moving window, performing a weighted sum of the

values in the region it has moved over. Let’s use a sequence of temperature values

over thirteen days as an example:

temperatures = np.array([5, 11, 15, 6, 5, 3, 3, 0, 0, 3, 4, 2, 1])

Figure 8.30 - Moving window over series of temperatures

Then, let’s use a window (filter) of size five, like in the figure above. In its first step,

the window is over days one to five. In the next step, since it can only move to the

right, it will be over days two to six. By the way, the size of our movement to the

right is, once again, known as the stride.

Now, let’s assign the same value (0.2) for every weight in our filter and use

PyTorch’s F.conv1d() to convolve the filter with our sequence (don’t mind the

shape just yet; we’ll get back to it in the next section):

670 | Chapter 8: Sequences

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

Saved successfully!

Ooh no, something went wrong!