IniziaInizia gratis

GRU network

Next to LSTMs, another popular recurrent neural network variant is the Gated Recurrent Unit, or GRU. It's appeal is in its simplicity: GRU cells require less computation than LSTM cells while often matching them in performance.

The code you are provided with is the RNN model definition that you coded previously. Your task is to adapt it such that it produces a GRU network instead. torch and torch.nn as nn have already been imported for you.

Questo esercizio fa parte del corso

Intermediate Deep Learning with PyTorch

Visualizza il corso

Istruzioni dell'esercizio

  • Update the RNN model definition in order to obtain a GRU network; assign the GRU layer to self.gru.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        # Define RNN layer
        self.rnn = nn.RNN(
            input_size=1,
            hidden_size=32,
            num_layers=2,
            batch_first=True,
        )
        self.fc = nn.Linear(32, 1)

    def forward(self, x):
        h0 = torch.zeros(2, x.size(0), 32)
        out, _ = self.rnn(x, h0)  
        out = self.fc(out[:, -1, :])
        return out
Modifica ed esegui il codice