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.

• For r=0 and z=0, the cell becomes equivalent to a linear layer followed by a

TanH activation function (in other words, the old hidden state [h] does not have

any effect).

Now, let’s see how a GRU cell works in code. We’ll create one using PyTorch’s own

nn.GRUCell and disassemble it into its components to manually reproduce all the

steps involved in updating the hidden state. To create a cell, we need to tell it the

input_size (number of features in our data points) and the hidden_size (the size of

the vector representing the hidden state), exactly the same as in the RNN cell. The

nonlinearity is fixed, though, as the hyperbolic tangent.

n_features = 2

hidden_dim = 2

torch.manual_seed(17)

gru_cell = nn.GRUCell(input_size=n_features, hidden_size=hidden_dim)

gru_state = gru_cell.state_dict()

gru_state

Output

OrderedDict([('weight_ih', tensor([[-0.0930, 0.0497],

[ 0.4670, -0.5319],

[-0.6656, 0.0699],

[-0.1662, 0.0654],

[-0.0449, -0.6828],

[-0.6769, -0.1889]])),

('weight_hh', tensor([[-0.4167, -0.4352],

[-0.2060, -0.3989],

[-0.7070, -0.5083],

[ 0.1418, 0.0930],

[-0.5729, -0.5700],

[-0.1818, -0.6691]])),

('bias_ih',

tensor([-0.4316, 0.4019, 0.1222, -0.4647, -0.5578,

0.4493])),

('bias_hh',

tensor([-0.6800, 0.4422, -0.3559, -0.0279, 0.6553,

0.2918]))])

Gated Recurrent Units (GRUs) | 629

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

Saved successfully!

Ooh no, something went wrong!