Variable length of text with cnn pytorch
I am a newbie with pytorch and I am wondering what is the best practice for variable length sentences sequences in CNNs.

I want to use CNN for feature selection on top of word emmbeding generated by fasttext and then feed the output into LSTM .

Now I know pytorch has a dynamic graph and I was wondering if there is a way to this except padding

Although PyTorch has dynamic graph construction, it is not possible to dynamically construct different graphs for different sequences in the same batch.
As such, it is possible to perform stochastic gradient descent, using individual sequences with arbitrary length, but it is not possible to perform batch gradient descent without padding.

To make training more efficient and avoid training over padded values, PyTorch has both the torch.nn.utils.rnn.pack_padded_sequence function and the torch.nn.utils.rnn.pad_packed_sequence to remove and then replace padding for batch operations.

# pack_padded_sequence so that padded items in the sequence won't be shown to the LSTM
X = torch.nn.utils.rnn.pack_padded_sequence(x, X_lengths, batch_first=True)

# now run through LSTM
X, self.hidden = self.lstm(X, self.hidden)

# undo the packing operation
X, _ = torch.nn.utils.rnn.pad_packed_sequence(X, batch_first=True)

