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

linear_input = nn.Linear(n_features, hidden_dim)linear_hidden = nn.Linear(hidden_dim, hidden_dim)with torch.no_grad():linear_input.weight = nn.Parameter(rnn_state['weight_ih'])linear_input.bias = nn.Parameter(rnn_state['bias_ih'])linear_hidden.weight = nn.Parameter(rnn_state['weight_hh'])linear_hidden.bias = nn.Parameter(rnn_state['bias_hh'])Now, let’s work our way through the mechanics of the RNN cell! It all starts withthe initial hidden state representing the empty sequence:initial_hidden = torch.zeros(1, hidden_dim)initial_hiddenOutputtensor([[0., 0.]])Then, we use the two blue neurons, the linear_hidden layer, to transform thehidden state:th = linear_hidden(initial_hidden)thOutputtensor([[-0.3565, -0.2904]], grad_fn=<AddmmBackward>)Cool! Now, let’s take look at a sequence of data points from our dataset:X = torch.as_tensor(points[0]).float()XRecurrent Neural Networks (RNNs) | 597

Outputtensor([[ 1.0349, 0.9661],[ 0.8055, -0.9169],[-0.8251, -0.9499],[-0.8670, 0.9342]])As expected, four data points, two coordinates each. The first data point, [1.0349,0.9661], corresponding to the top-right corner of the square, is going to betransformed by the linear_input layers (the two red neurons):tx = linear_input(X[0:1])txOutputtensor([[0.7712, 1.4310]], grad_fn=<AddmmBackward>)There we go: We got both t x and t h . Let’s add them together:adding = th + txaddingOutputtensor([[0.4146, 1.1405]], grad_fn=<AddBackward0>)The effect of adding t x is similar to the effect of adding the bias: It is effectivelytranslating the transformed hidden state to the right (by 0.7712) and up (by1.4310).Finally, the hyperbolic tangent activation function "compresses" the feature spaceback into the (-1, 1) interval:torch.tanh(adding)598 | Chapter 8: Sequences

linear_input = nn.Linear(n_features, hidden_dim)

linear_hidden = nn.Linear(hidden_dim, hidden_dim)

with torch.no_grad():

linear_input.weight = nn.Parameter(rnn_state['weight_ih'])

linear_input.bias = nn.Parameter(rnn_state['bias_ih'])

linear_hidden.weight = nn.Parameter(rnn_state['weight_hh'])

linear_hidden.bias = nn.Parameter(rnn_state['bias_hh'])

Now, let’s work our way through the mechanics of the RNN cell! It all starts with

the initial hidden state representing the empty sequence:

initial_hidden = torch.zeros(1, hidden_dim)

initial_hidden

Output

tensor([[0., 0.]])

Then, we use the two blue neurons, the linear_hidden layer, to transform the

hidden state:

th = linear_hidden(initial_hidden)

th

Output

tensor([[-0.3565, -0.2904]], grad_fn=<AddmmBackward>)

Cool! Now, let’s take look at a sequence of data points from our dataset:

X = torch.as_tensor(points[0]).float()

X

Recurrent Neural Networks (RNNs) | 597

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

Saved successfully!

Ooh no, something went wrong!