Is it an NLP problem?  Input shall have the same width in a batch. You can use pack_padded_sequence 12 to pad your input to the same size or handle it with your own code(seems you have done it with using 0s padding).
Maybe you want reshape y and y_pred to calculate loss as below
loss = criterion(y_pred.view(-1,4), y.view(-1,1))

Sorry If I get wrong, I am not familiar with such problem.