(tensor_list, min_len_list=None, max_len_list=None)
return filtered_tensor_list
Filter tensors by their minimum or maximum length.
Filter tensors by their minimum or maximum length.
Filter tensors by their minimum or maximum length .
def filter_by_length(tensor_list, min_len_list=None, max_len_list=None):
  """Filter tensors by their minimum or maximum length."""
  if not min_len_list and not max_len_list:
    return tensor_list
  if min_len_list:
    if len(min_len_list) != len(tensor_list):
      raise ValueError("Min length list need to match size of tensor_list.")
  else:
    min_len_list = [None for _ in tensor_list]

  if max_len_list:
    if len(max_len_list) != len(tensor_list):
      raise ValueError("Max length list need to match size of tensor_list.")
  else:
    max_len_list = [None for _ in tensor_list]

  keep = tf.constant(True, dtype=tf.bool)
  for min_len, max_len, tensor in zip(min_len_list, max_len_list, tensor_list):
    if min_len and max_len and min_len >= max_len:
      raise ValueError("Invalid min max lengths.")
    if any([min_len, max_len]):
      tensor_len = tf.reduce_sum(tf.cast(tf.greater(tensor, 0), tf.int32))
      if min_len:
        keep = tf.logical_and(keep, tf.greater(tensor_len, min_len))
      if max_len:
        keep = tf.logical_and(keep, tf.less_equal(tensor_len, max_len))

  filtered_tensor_list = []
  for tensor in tensor_list:
    empty_tensor = tf.zeros(
        [0] * len(tensor.shape.as_list()), dtype=tensor.dtype)
    filtered_tensor_list.append(
        tf.cond(keep, lambda: tensor, lambda: empty_tensor))  # pylint: disable=cell-var-from-loop
  return filtered_tensor_list
def filter_by_length ( tensor_list , min_len_list = None , max_len_list = None ) : if not min_len_list and not max_len_list : return tensor_list if min_len_list : if len ( min_len_list ) != len ( tensor_list ) : raise ValueError ( "Min length list need to match size of tensor_list." ) else : min_len_list = [ None for _ in tensor_list ] if max_len_list : if len ( max_len_list ) != len ( tensor_list ) : raise ValueError ( "Max length list need to match size of tensor_list." ) else : max_len_list = [ None for _ in tensor_list ] keep = tf . constant ( True , dtype = tf . bool ) for min_len , max_len , tensor in zip ( min_len_list , max_len_list , tensor_list ) : if min_len and max_len and min_len >= max_len : raise ValueError ( "Invalid min max lengths." ) if any ( [ min_len , max_len ] ) : tensor_len = tf . reduce_sum ( tf . cast ( tf . greater ( tensor , 0 ) , tf . int32 ) ) if min_len : keep = tf . logical_and ( keep , tf . greater ( tensor_len , min_len ) ) if max_len : keep = tf . logical_and ( keep , tf . less_equal ( tensor_len , max_len ) ) filtered_tensor_list = [ ] for tensor in tensor_list : empty_tensor = tf . zeros ( [ 0 ] * len ( tensor . shape . as_list ( ) ) , dtype = tensor . dtype ) filtered_tensor_list . append ( tf . cond ( keep , lambda : tensor , lambda : empty_tensor ) ) # pylint: disable=cell-var-from-loop return filtered_tensor_list