22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Square Model II — The Quickening

This model is pretty much the same as the original "Square Model," except for one

difference: Its recurrent neural network is not a plain RNN anymore, but a GRU.

Everything else stays exactly the same.

Model Configuration

1 class SquareModelGRU(nn.Module):

2 def __init__(self, n_features, hidden_dim, n_outputs):

3 super(SquareModelGRU, self).__init__()

4 self.hidden_dim = hidden_dim

5 self.n_features = n_features

6 self.n_outputs = n_outputs

7 self.hidden = None

8 # Simple GRU

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

10 self.hidden_dim,

11 batch_first=True) 1

12 # Classifier to produce as many logits as outputs

13 self.classifier = nn.Linear(self.hidden_dim,

14 self.n_outputs)

15

16 def forward(self, X):

17 # X is batch first (N, L, F)

18 # output is (N, L, H)

19 # final hidden state is (1, N, H)

20 batch_first_output, self.hidden = self.basic_rnn(X)

21

22 # only last item in sequence (N, 1, H)

23 last_output = batch_first_output[:, -1]

24 # classifier will output (N, 1, n_outputs)

25 out = self.classifier(last_output)

26

27 # final output is (N, n_outputs)

28 return out.view(-1, self.n_outputs)

1 The ONLY change in the code: from nn.RNN to nn.GRU

We’ll be using the same data loaders again, so we’re going directly to the model

configuration and training.

Gated Recurrent Units (GRUs) | 635

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

Saved successfully!

Ooh no, something went wrong!