22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The decoder model is actually quite similar to the models we developed in Chapter

8:

Decoder

1 class Decoder(nn.Module):

2 def __init__(self, n_features, hidden_dim):

3 super().__init__()

4 self.hidden_dim = hidden_dim

5 self.n_features = n_features

6 self.hidden = None

7 self.basic_rnn = nn.GRU(self.n_features,

8 self.hidden_dim,

9 batch_first=True)

10 self.regression = nn.Linear(self.hidden_dim,

11 self.n_features)

12

13 def init_hidden(self, hidden_seq):

14 # We only need the final hidden state

15 hidden_final = hidden_seq[:, -1:] # N, 1, H

16 # But we need to make it sequence-first

17 self.hidden = hidden_final.permute(1, 0, 2) # 1, N, H 1

18

19 def forward(self, X):

20 # X is N, 1, F

21 batch_first_output, self.hidden = \

22 self.basic_rnn(X, self.hidden) 2

23

24 last_output = batch_first_output[:, -1:]

25 out = self.regression(last_output)

26

27 # N, 1, F

28 return out.view(-1, 1, self.n_features) 3

1 Initializing decoder’s hidden state using encoder’s final hidden state.

2 The recurrent layer both uses and updates the hidden state.

3 The output has the same shape as the input (N, 1, F).

Let’s go over the differences:

• Since the initial hidden state has to come from the encoder, we need a method

694 | Chapter 9 — Part I: Sequence-to-Sequence

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

Saved successfully!

Ooh no, something went wrong!