@hzzzm thanks! I assume that 2227 is the sequence length and you only have 1 sequence in your batch, i.e., batch_size = 1. After out = out[:, -1, :], the shape is (2227, 40) which would suddenly mean a batch size of 2227.
out = out[-1] will yield a shape if (1, 40) representing (batch_size, hidden_size) which I strongly assume is what you want.