# Module `models.rnn`

Reccurent Neural Network for Shakespeare Dataset

Expand source code

    #!/usr/bin/env python3

    """
    Reccurent Neural Network for Shakespeare Dataset
    """

    # Import PyTorch root package import torch
    import torch

    import torch.nn as nn

    class RNN(nn.Module):

        def __init__(self, vocab_size=90, embedding_dim=8, hidden_dim=512, num_layers=2):
            super(RNN, self).__init__()

            # set class variables
            self.hidden_dim = hidden_dim
            self.vocab_size = vocab_size

            # embedding and LSTM layers
            self.embedding = nn.Embedding(vocab_size, embedding_dim)
            self.num_lstm_layers = num_layers
            self.lstm = nn.LSTM(embedding_dim, hidden_dim, batch_first=True, num_layers=num_layers)

            # linear and sigmoid layers
            self.fc = nn.Linear(hidden_dim, vocab_size)

        def forward(self, input, hidden):
            embeds = self.embedding(input)
            lstm_out, hidden = self.lstm1(embeds, hidden)
            out = self.fc(lstm_out)
            # flatten the output
            out = out.reshape(-1, self.vocab_size)
            return out, hidden

        def init_hidden(self, batch_size, device):
            hidden = (torch.zeros(self.num_lstm_layers, batch_size, self.hidden_dim).to(device = device),
                      torch.zeros(self.num_lstm_layers, batch_size, self.hidden_dim).to(device = device))
            return hidden

    # TODO: Any way we can actually have an useful pretrained argument here?
    def rnn(pretrained=False, num_classes=90):
        return RNN(vocab_size=num_classes)

    def minirnn(pretrained=False, num_classes=90):
        return RNN(vocab_size=num_classes, hidden_dim=128)

## Functions

`def minirnn(pretrained=False, num_classes=90)`
  Expand source code

      def minirnn(pretrained=False, num_classes=90):
          return RNN(vocab_size=num_classes, hidden_dim=128)

`def rnn(pretrained=False, num_classes=90)`
  Expand source code

      def rnn(pretrained=False, num_classes=90):
          return RNN(vocab_size=num_classes)

## Classes

`class RNN (vocab_size=90, embedding_dim=8, hidden_dim=512, num_layers=2)`

  Base class for all neural network modules.

  Your models should also subclass this class.

  Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::

      import torch.nn as nn
      import torch.nn.functional as F

      class Model(nn.Module):
          def __init__(self):
              super(Model, self).__init__()
              self.conv1 = nn.Conv2d(1, 20, 5)
              self.conv2 = nn.Conv2d(20, 20, 5)

          def forward(self, x):
              x = F.relu(self.conv1(x))
              return F.relu(self.conv2(x))

  Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:`to`, etc.

  :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

  Initializes internal Module state, shared by both nn.Module and ScriptModule.

  Expand source code

      class RNN(nn.Module):

          def __init__(self, vocab_size=90, embedding_dim=8, hidden_dim=512, num_layers=2):
              super(RNN, self).__init__()

              # set class variables
              self.hidden_dim = hidden_dim
              self.vocab_size = vocab_size

              # embedding and LSTM layers
              self.embedding = nn.Embedding(vocab_size, embedding_dim)
              self.num_lstm_layers = num_layers
              self.lstm = nn.LSTM(embedding_dim, hidden_dim, batch_first=True, num_layers=num_layers)

              # linear and sigmoid layers
              self.fc = nn.Linear(hidden_dim, vocab_size)

          def forward(self, input, hidden):
              embeds = self.embedding(input)
              lstm_out, hidden = self.lstm1(embeds, hidden)
              out = self.fc(lstm_out)
              # flatten the output
              out = out.reshape(-1, self.vocab_size)
              return out, hidden

          def init_hidden(self, batch_size, device):
              hidden = (torch.zeros(self.num_lstm_layers, batch_size, self.hidden_dim).to(device = device),
                        torch.zeros(self.num_lstm_layers, batch_size, self.hidden_dim).to(device = device))
              return hidden

  ### Ancestors

  - torch.nn.modules.module.Module

  ### Class variables

  `var dump_patches : bool`
  `var training : bool`

  ### Methods

  `def forward(self, input, hidden) ‑> Callable[..., Any]`

    Defines the computation performed at every call.

    Should be overridden by all subclasses.

    Note

    Although the recipe for forward pass needs to be defined within this function, one should call the :class:`Module` instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

    Expand source code

        def forward(self, input, hidden):
            embeds = self.embedding(input)
            lstm_out, hidden = self.lstm1(embeds, hidden)
            out = self.fc(lstm_out)
            # flatten the output
            out = out.reshape(-1, self.vocab_size)
            return out, hidden

  `def init_hidden(self, batch_size, device)`
    Expand source code

        def init_hidden(self, batch_size, device):
            hidden = (torch.zeros(self.num_lstm_layers, batch_size, self.hidden_dim).to(device = device),
                      torch.zeros(self.num_lstm_layers, batch_size, self.hidden_dim).to(device = device))
            return hidden