Hello PyTorch experts:

Sentences and documents both can be variable length.

Lets say, we have following 2 docs:


(Here, each tensor is a sentence and each number in the tensor is an index of embedding matrix)



This will throw an error because the length of the longest sentence in the first document is greater than the length of the longest sentence in the second sentence.

So, we need to pad each sentence to the longest sentence of the batch.

One solution would be, find the length of the longest sentence in each batch and pad each sentence to that fixed length. But the pad_sequence function of PyTorch does not support that.

Am I missing something? Is there any other PyTorch way to do this?


1 Answer 1


I solved this problem by using a keras function:



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.