Batch with Basic RNN

Hi everyone ! Can someone explain me how use batch with RNN ?
It is basic model from documentation:

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()

        self.hidden_size = hidden_size

        self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
        self.i2o = nn.Linear(input_size + hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, input, hidden):
        combined = torch.cat((input, hidden), 1)
        hidden = self.i2h(combined)
        output = self.i2o(combined)
        output = self.softmax(output)
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, self.hidden_size)

train function:

def train():
    hidden = model.initHidden()
    for batch_idx, (data, target) in enumerate(train_loader):
        optimizer.zero_grad()
        data = data.to(device)
        target = target.to(device)


        loss = F.nll_loss(output, target)
        
        loss.backward()
        optimizer.step()

What should I do to send batch (for example [4,2,5] ) to model and how make correct training whit this?

This RNN tutorial might be a good starter as it walks you through a classification use case.

This doesn’t show how to batch!!

1 Like