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

Data Preparation1 def pack_collate(batch):2 X = [item[0] for item in batch]3 y = [item[1] for item in batch]4 X_pack = rnn_utils.pack_sequence(X, enforce_sorted=False)56 return X_pack, torch.as_tensor(y).view(-1, 1)78 train_var_loader = DataLoader(train_var_data,9 batch_size=16,10 shuffle=True,11 collate_fn=pack_collate)There Can Be Only ONE … ModelWe’ve developed many models throughout this chapter, depending both on thetype of recurrent layer that was used (RNN, GRU, or LSTM) and on the type ofsequence (packed or not). The model below, though, is able to handle differentconfigurations:• Its rnn_layer argument allows you to use whichever recurrent layer youprefer.• The **kwargs argument allows you to further configure the recurrent layer(using num_layers and bidirectional arguments, for example).• The output dimension of the recurrent layer is automatically computed tobuild a matching linear layer.• If the input is a packed sequence, it handles the unpacking and fancy indexingto retrieve the actual last hidden state.Model Configuration1 class SquareModelOne(nn.Module):2 def __init__(self, n_features, hidden_dim, n_outputs,3 rnn_layer=nn.LSTM, **kwargs):4 super(SquareModelOne, self).__init__()5 self.hidden_dim = hidden_dim6 self.n_features = n_features7 self.n_outputs = n_outputs8 self.hidden = NonePutting It All Together | 681

9 self.cell = None10 self.basic_rnn = rnn_layer(self.n_features,11 self.hidden_dim,12 batch_first=True, **kwargs)13 output_dim = (self.basic_rnn.bidirectional + 1) * \14 self.hidden_dim15 # Classifier to produce as many logits as outputs16 self.classifier = nn.Linear(output_dim, self.n_outputs)1718 def forward(self, X):19 is_packed = isinstance(X, nn.utils.rnn.PackedSequence)20 # X is a PACKED sequence, there is no need to permute2122 rnn_out, self.hidden = self.basic_rnn(X)23 if isinstance(self.basic_rnn, nn.LSTM):24 self.hidden, self.cell = self.hidden2526 if is_packed:27 # unpack the output28 batch_first_output, seq_sizes = \29 rnn_utils.pad_packed_sequence(rnn_out,30 batch_first=True)31 seq_slice = torch.arange(seq_sizes.size(0))32 else:33 batch_first_output = rnn_out34 seq_sizes = 0 # so it is -1 as the last output35 seq_slice = slice(None, None, None) # same as ':'3637 # only last item in sequence (N, 1, H)38 last_output = batch_first_output[seq_slice, seq_sizes-1]3940 # classifier will output (N, 1, n_outputs)41 out = self.classifier(last_output)4243 # final output is (N, n_outputs)44 return out.view(-1, self.n_outputs)Model Configuration & TrainingThe model below uses a bidirectional LSTM and already achieves a 100% accuracyon the training set. Feel free to experiment with different recurrent layers, thenumber of layers, single or bidirectional, as well as with switching between fixed-682 | Chapter 8: Sequences

Data Preparation

1 def pack_collate(batch):

2 X = [item[0] for item in batch]

3 y = [item[1] for item in batch]

4 X_pack = rnn_utils.pack_sequence(X, enforce_sorted=False)

5

6 return X_pack, torch.as_tensor(y).view(-1, 1)

7

8 train_var_loader = DataLoader(train_var_data,

9 batch_size=16,

10 shuffle=True,

11 collate_fn=pack_collate)

There Can Be Only ONE … Model

We’ve developed many models throughout this chapter, depending both on the

type of recurrent layer that was used (RNN, GRU, or LSTM) and on the type of

sequence (packed or not). The model below, though, is able to handle different

configurations:

• Its rnn_layer argument allows you to use whichever recurrent layer you

prefer.

• The **kwargs argument allows you to further configure the recurrent layer

(using num_layers and bidirectional arguments, for example).

• The output dimension of the recurrent layer is automatically computed to

build a matching linear layer.

• If the input is a packed sequence, it handles the unpacking and fancy indexing

to retrieve the actual last hidden state.

Model Configuration

1 class SquareModelOne(nn.Module):

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

3 rnn_layer=nn.LSTM, **kwargs):

4 super(SquareModelOne, self).__init__()

5 self.hidden_dim = hidden_dim

6 self.n_features = n_features

7 self.n_outputs = n_outputs

8 self.hidden = None

Putting It All Together | 681

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

Saved successfully!

Ooh no, something went wrong!