title: How to make mask for variable length sequences, which are then padded, in tensorflow2 for RNN
body: <p>Trying implement a mask for a sequence of timeperiods, with zero-padding, to an LSTM network.
  Each sequence of timeperiods is of varying length, hence requiring padding &amp; masking.
  I am trying to model sequences of length 96(timeperiods), and features=33. Simplified data (7 timeperiods and 3 features) are shown:</p>
  <p>example state at a timeperiod = [4, 2, 9] at time0(t0)</p>
  <p>example sequence = [[2, 3, 6], [1, 6, 8], [2, 9, 4], [2, 7, 3]] at t(0), t(1), t(2), t(3)</p>
  <p>example_padded1 = [[2, 3, 6], [1, 6, 8], [2, 9, 4], [2, 7, 3], 0, 0, 0] at t(0) to t(6)</p>
  <p>example_padded2 = [[2, 6, 0], [1, 6, 3], [2, 9, 7], [2, 7, 3], 0., 0., 0.]</p>
  <p>example_padded3 = [[2, 6, 0], [5, 8, 3], [9, 4, 7], [2, 5, 3], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]</p>
  <p>Submitting each example sequence to:</p>
  <pre><code>seq = example_padded1
  masking = layers.Masking(mask_value=0)
  masked_output = masking(seq)
  print(masked_output._keras_mask)
  </code></pre>
  <p>Gives Errors:</p>
  <p>padded1 error: InvalidArgumentError: cannot compute Pack as input #2(zero-based) was expected to be a
  float tensor but is a int32 tensor [Op:Pack] name: packed</p>
  <p>padded2 error: InvalidArgumentError: Shapes of all inputs must match:
  values[0].shape = [3] != values[2].shape = [] [Op:Pack] name: packed</p>
  <p>padded3 error: Error: 'list' object has no attribute 'dtype' when
  when checking for mask_value = 0</p>
  <p>I then added an input layer to define shape of a sequence:</p>
  <pre><code>seq_len, n_features = 7, 3
  inp = Input(shape=(seq_len, n_features))
  masking = layers.Masking(mask_value=0, input_shape=inp)
  masked_output2 = masking(seq)
  print(masked_output2._keras_mask)
  </code></pre>
  <p>But got error:
  TypeError: Cannot iterate over a tensor with unknown first dimension.
  (Python 3.8, TF2)</p>
  <p>Have also been trying Embedding, but that seems even more problematical</p>
  <p>How to implement a mask for variable length sequences, which are then padded?</p>