Give Variable Length input to LSTM

2

My input data consist of list of list. Both list have dynamic length for every example like below.

X[0] = [[0, 1, 3, 5, 8, 26], [3, 4, 5, 7, 9, 15, 26, 30, 38, 39, 40]]
X[1] = [[1, 2, 4, 16, 19, 29, 48]]
..

My target data consist of dynamic length list like below:

y[0] = [5, 7, 8, 12, 15, 17, 29]
y[1] = [2, 4, 6, 8, 10, 11, 16, 18, 19, 29, 30, 33, 35]
..

I try to train LSTM model with given input X, predict Y.

If I concat each X list to one list and make padding operation LSTM accepts that data type. My example code is below:

X_train = sequence.pad_sequences(X_train, maxlen=padding_size)
X_test = sequence.pad_sequences(X_test, maxlen=padding_size)

model = Sequential()
model.add(Embedding(50, 10, input_length=X_train.shape[1], mask_zero=True))

if isBidirectional:
    model.add(Bidirectional(LSTM(lstm_layer_number)))
else:
    model.add(LSTM(lstm_layer_number))

if isDropout:
    model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # binary crossentropy yields the best results

The problem is that I don't want to give input as a fixed size vector. Instead of given fixed size vector, I want to give input as a dynamic size and list by list.

How can I give these input as sequence(list by list) to LSTM ?

Any help is appreciated.

Batuhan B

Posted 2018-11-04T15:21:12.363

Reputation: 216

This answer could help you with variable-length inputs to LSTM. – Esmailian – 2019-05-02T23:17:23.037

Answers

1

You can try padding the inputs with zero that is of lesser in length.

Bharath Kumar L

Posted 2018-11-04T15:21:12.363

Reputation: 363