Masking excludes designated sequence positions from downstream neural-network computations. In padded text sequences, it prevents [PAD] tokens from influencing the recurrent representation used for prediction. section 14.5.3
Mask padded tokens in an LSTM
In Keras, set mask_zero = TRUE on the embedding layer when zero represents [PAD]. The mask is passed to the LSTM, which returns the last non-masked representation.\n\ninputs <- keras_input(shape = c(max_length), dtype = "int32")\noutputs <- inputs |>\n layer_embedding(input_dim = max_tokens, output_dim = 64L, mask_zero = TRUE) |>\n layer_bidirectional(layer_lstm(units = 64L)) |>\n layer_dense(1, activation = "sigmoid") section 14.5.3






