LoslegenKostenlos loslegen

Creating a RNN model for text generation

At PyBooks, you've been tasked to develop an algorithm that can perform text generation. The project involves auto-completion of book names. To kickstart this project, you decide to experiment with a Recurrent Neural Network (RNN). This way, you can understand the nuances of RNNs before moving to more complex models.

The following has been imported for you: torch, torch.nn as nn.

The data variable has been initialized with an excerpt from Alice's Adventures in Wonderland by Lewis Carroll.

Diese Übung ist Teil des Kurses

Deep Learning for Text with PyTorch

Kurs anzeigen

Anleitung zur Übung

  • Include an RNN layer and linear layer in RNNmodel class
  • Instantiate the RNN model with input size as length of chars, hidden size of 16, and output size as length of chars.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Include an RNN layer and linear layer in RNNmodel class
class RNNmodel(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNNmodel, self).__init__()
        self.hidden_size = hidden_size
        self.rnn = nn.____(input_size, hidden_size, batch_first=True)
        self.fc = nn.____(hidden_size, output_size)

    def forward(self, x):
      h0 = torch.zeros(1, x.size(0), self.hidden_size)
      out, _ = self.rnn(x, h0)  
      out = self.fc(out[:, -1, :])  
      return out

# Instantiate the RNN model
model = RNNmodel(____, ____, ____)
Code bearbeiten und ausführen