RNN to predict label from sequence in pytorch

Each training example is a sequence of how 8 input variables vary across 5 timesteps

I.e Input is [ip0_0, ip1_0,...,ip4_0], [ip0_1, ip1_1,...,ip4_1]..., [ip0_1, ip1_1,...,ip4_1]

Each training example has a label 0 or 1

I want to create a RNN that predicts the label from the inputs.

I see two ways of doing it

See RNNModelMultiforward below. High level idea is

  1. Have a single torch.RNN()
  2. Initialize hidden state to 0
  3. Run the following 5 times
    out, h = RNN([ip0_i,...,ip4_i], h), where i = 0,...,4
  4. Run a feedforward layer that predicts the label from the final hidden state h

Is this the right way to do it or should I use a torch.RNN with num_layers = 5 and run it once to get the output: I.e hn = RNN([[ip0_0,...,ip4_0],.....,[ip0_4,...,ip4_4]], h0) (see RNNModelMultilayer below)

RNNModel multiforward

# Create RNN Model
class RNNModelMultiforward(nn.Module):
    def __init__(self, input_dim, hidden_dim, layer_dim, output_dim, num_epochs, act_fn='relu'):
        super(RNNModelMultiforward, self).__init__()
        
        # Number of hidden dimensions
        self.hidden_dim = hidden_dim
        
        # Number of hidden layers
        self.layer_dim = layer_dim

        #Number of times the RNN will be run (the input should be num_epochs X input_dim in size)
        self.num_epochs = num_epochs
        
        # RNN
        self.rnn = nn.RNN(input_dim, hidden_dim, layer_dim, batch_first=True, nonlinearity=act_fn)
        
        # Readout layer
        self.fc = nn.Linear(hidden_dim, output_dim)
    
    def forward(self, x):
        # Initialize hidden state with zeros
        h = Variable(torch.zeros(self.layer_dim, self.hidden_dim))
    
        for ts in range(0, self.num_epochs):
            out, h = self.rnn(x[ts], h)
        out = self.fc(h) 
        return out

RNNModel multilayer

# Create RNN Model
class RNNModelMultilayer(nn.Module):
    def __init__(self, input_dim, hidden_dim, layer_dim, output_dim, act_fn='relu'):
        super(RNNModelMultilayer, self).__init__()
        
        # Number of hidden dimensions
        self.hidden_dim = hidden_dim
        
        # Number of hidden layers
        self.layer_dim = layer_dim
        
        # RNN
        self.rnn = nn.RNN(input_dim, hidden_dim, layer_dim, batch_first=True, nonlinearity=act_fn)
        
        # Readout layer
        self.fc = nn.Linear(hidden_dim, output_dim)
    
    def forward(self, x):
        # Initialize hidden state with zeros
        h0 = Variable(torch.zeros(self.layer_dim, self.hidden_dim))
    
        out, hn = self.rnn(x, h0)
        out = self.fc(hn[4]) 
        return out