How LSTM deal with variable length sequence

后端 未结 2 1532
野的像风
野的像风 2021-01-01 01:09

I found a piece of code in Chapter 7,Section 1 of deep Deep Learning with Python as follow:

from keras.models import Model
from keras import layers
from kera         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 01:33

    How does it deal with units?

    Units are totally independend of length, so, there is nothing special being done.

    Length only increases the "recurrent steps", but recurrent steps use always the same cells over and over.

    The number of cells is fixed and defined by the user:

    • the first LSTM has 32 cells/units
    • the second LSTM has 16 cells/units

    How to deal with variable length?

    • Approach 1: create separate batches of 1 sequence, each batch with its own length. Feed each batch to the model individually. Methods train_on_batch and predict_on_batch inside a manual loop are the easiest form.
      • Ideally, separate batches per length, each batch collects all sequences with same length
    • Approach 2: create a fixed length batch, fill the unused tail lenght of each sequence with 0, use the parameter mask_zero=True in the embedding layers.
      • Be careful not to use 0 as an actual word or meaningful data in the inputs of the embeddings.

提交回复
热议问题