Either you guarantee that all batches have indeed the same size, or you adjust the hidden state accordingly for each batch. I assume that in your training code you re-initialize the hidden state with zeros (a common practice). You often find a method called init_hidden() in example code. You can simply extend this method to init_hidden(batch_size) and return the zero-ed hidden state with the respective dimensions.