def _trim_and_pad(t, length):
  """Trim/pad to the first axis of t to be of size length.

  Args:
    t: a tf.Tensor
    length: an integer
  Returns:
    a tf.Tensor
  """
  t = t[:length]
  paddings = [[0, length - tf.shape(t)[0]]] + [[0, 0]]*(t.get_shape().ndims - 1)
  t = tf.pad(t, paddings)
  return t