code: def last_activations_in_sequence(activations, sequence_lengths=None):
      """Selects the nth set of activations for each n in `sequence_length`.
  
      Returns a `Tensor` of shape `[batch_size, k]`. If `sequence_length` is not
      `None`, then `output[i, :] = activations[i, sequence_length[i] - 1, :]`. If
      `sequence_length` is `None`, then `output[i, :] = activations[i, -1, :]`.
  
      Args:
        activations: A `Tensor` with shape `[batch_size, padded_length, k]`.
        sequence_lengths: A `Tensor` with shape `[batch_size]` or `None`.
  
      Returns:
        A `Tensor` of shape `[batch_size, k]`.
      """
      activations_shape = tf.shape(input=activations)
      batch_size = activations_shape[0]
      padded_length = activations_shape[1]
      input_dim = activations_shape[2]
      if sequence_lengths is None:
          sequence_lengths = padded_length
      else:
          sequence_lengths = tf.cast(sequence_lengths, dtype=tf.int32)
      reshaped_activations = tf.reshape(activations, [-1, input_dim])
      indices = (
          tf.range(batch_size) * padded_length + sequence_lengths - 1
      )
      last_activations = tf.gather(reshaped_activations, indices)
      last_activations.set_shape(
          [activations.get_shape()[0], activations.get_shape()[2]]
      )
  
      return last_activations
  
label_desc: Use image node_id 1 for calling a global function with example usage: last_activations_in_sequence(activations, sequence_lengths) and returns: last_activations