0

I've found two possible solution of handling with variable-size input sequences for RNN in Keras. The solution one:

input = Input(shape=(None, num_classes))

then I can put any sequence size as an input for both training and validation.

The solution two:

input = Input(shape=(max_seq_length, num_classes))
...
pad_sequences(input_data, maxlen=max_seq_length, padding='post')

Which solution is recommended?

I consider benefits of these two. What I can see in the solution two is kind of validation of input size. The input cannot be larger than max_seq_size, moreover I can decide of type of padding (pre/post) and the same for timing of too large sequence.

What kind of padding and trimming is done using the solution one? Default parameters of pad_sequence?

I've benchmarked the time of training model for both solution and it's roughly the same time. I guess, that under the hood it's the same, like the max_seq_length is calculated from max length of training sequence, am I right?

Thank you for any clarification!

1 Answer 1

2

There is simply no padding or trimming in solution one. It takes the sequence as is and processes it. The model is totally independent of sequence length.

In solution two, the best to do is add a Masking layer. It will simply skip processing the padded values.

Sign up to request clarification or add additional context in comments.

1 Comment

is it a good solution to use "0" padding with masking layer. Will not reduce the accuracy of the model ? What about use them for MLPs models ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.