Shouldn’t  net.init_hidden(batch_size) not be inside the inner loop? I thought you have to initialize the hidden state before/after each batch since you do a forward and backward for batch. After all, you do zero_grad() and step() in the inner loop.
This should also get rid of your error. Even if the last batch is not “full”. Currently, you set the size of the hidden state according to some fixed predefined value batch_size that never changes.
So either you ignore the last batch that is smaller than batch_size as suggested by @ptrblck or you do the following:

Put net.init_hidden(batch_size) into the inner loop (I still think that’s a must either way)
Before calling init_hidden() you first do batch_size = inputs.shape[0], assuming that the shape of inputs is (batch_size, ...)