()
return tl.Fn('Interleave', interleave)
Layer that interleaves and flattens two serialized sequences.

  The first sequence can be longer by 1 than the second one. This is so we can
  interleave sequences of observations and actions, when there's 1 extra
  observation at the end.

  For serialized sequences [[x_1_1, ..., x_1_R1], ..., [x_L1_1, ..., x_L1_R1]]
  and [[y_1_1, ..., y_1_R2], ..., [y_L2_1, ..., y_L2_R2]], where L1 = L2 + 1,
  the result is [x_1_1, ..., x_1_R1, y_1_1, ..., y_1_R2, ..., x_L2_1, ...,
  x_L2_R1, y_L2_1, ..., y_L2_R2, x_L1_1, ..., x_L1_R1] (batch dimension omitted
  for clarity).

  The layer inputs are a sequence pair of shapes (B, L1, R1) and (B, L2, R2),
  where B is batch size, L* is the length of the sequence and R* is the
  representation length of each element in the sequence.

  Returns:
    Layer that interleaves sequence of shape (B, L1 * R1 + L2 * R2).
Layer that interleaves and flattens two serialized sequences.
Layer that interleaves and flattens two serialized sequences .
def Interleave():
  """Layer that interleaves and flattens two serialized sequences.

  The first sequence can be longer by 1 than the second one. This is so we can
  interleave sequences of observations and actions, when there's 1 extra
  observation at the end.

  For serialized sequences [[x_1_1, ..., x_1_R1], ..., [x_L1_1, ..., x_L1_R1]]
  and [[y_1_1, ..., y_1_R2], ..., [y_L2_1, ..., y_L2_R2]], where L1 = L2 + 1,
  the result is [x_1_1, ..., x_1_R1, y_1_1, ..., y_1_R2, ..., x_L2_1, ...,
  x_L2_R1, y_L2_1, ..., y_L2_R2, x_L1_1, ..., x_L1_R1] (batch dimension omitted
  for clarity).

  The layer inputs are a sequence pair of shapes (B, L1, R1) and (B, L2, R2),
  where B is batch size, L* is the length of the sequence and R* is the
  representation length of each element in the sequence.

  Returns:
    Layer that interleaves sequence of shape (B, L1 * R1 + L2 * R2).
  """
  def interleave(x, y):
    (batch_size, _, _) = x.shape
    (_, length, _) = y.shape
    assert x.shape[1] in (length, length + 1)

    reprs = jnp.concatenate((x[:, :length], y), axis=2)
    reprs = jnp.reshape(reprs, (batch_size, -1))
    remainder = jnp.reshape(x[:, length:], (batch_size, -1))
    return jnp.concatenate((reprs, remainder), axis=1)
  return tl.Fn('Interleave', interleave)
def Interleave ( ) : def interleave ( x , y ) : ( batch_size , _ , _ ) = x . shape ( _ , length , _ ) = y . shape assert x . shape [ 1 ] in ( length , length + 1 ) reprs = jnp . concatenate ( ( x [ : , : length ] , y ) , axis = 2 ) reprs = jnp . reshape ( reprs , ( batch_size , - 1 ) ) remainder = jnp . reshape ( x [ : , length : ] , ( batch_size , - 1 ) ) return jnp . concatenate ( ( reprs , remainder ) , axis = 1 ) return tl . Fn ( 'Interleave' , interleave )