code: def flatten(tensor, preserve_dims, flattened_dim=None):
      """Flattens a tensor whiling keeping several leading dimensions.
  
      :attr:`preserve_dims` must < tensor's rank
  
      Args:
          tensor: A Tensor to flatten.
          preserve_dims (int): The number of leading dimensions to preserve.
          flatterned_dim (int, optional): The size of the resulting flattened
              dimension. If not given, infer automatically, which can cause
              a statically unknown dimension size.
  
      Returns:
          A Tensor with rank :attr:`perserve_dims` + 1.
  
      Example:
          .. code-block:: python
  
              x = tf.ones(shape=[d_1, d_2, d_3, d_4])
              y = flatten(x, 2) # y.shape == [d_1, d_2, d_3 * d_4]
      """
      if flattened_dim is None:
          flattened_dim = -1
      shape = tf.concat(
          [tf.shape(tensor)[:preserve_dims], [flattened_dim]], axis=0
      )
      tensor_ = tf.reshape(tensor, shape)
      return tensor_
  
file_path: texar/texar/tf/utils/shapes.py
imports: import numpy
  import tensorflow
  from tensorflow.python.util import nest
  from tensorflow.python.ops import rnn
  from tensorflow.python.framework import ops
label_desc: Use image node_id 8 for calling a global function with example usage: flatten(tensor, preserve_dims, flattened_dim) and returns: tensor_