code: def slice_to(tensor, axis, end):
      """Slice a composite tensor along `axis` from 0 to `end`.
  
      Examples:
  
      ```python
      slice_to(tensor, 2, -1) === tensor[:, :, :-1]
      sparse_to_dense(slice_to(sparse_tensor, 2, -1))
        === sparse_to_dense(sparse_tensor)[:, :, :-1]
      ```
  
      Args:
        tensor: A `Tensor` or `SparseTensor`.
        axis: A python integer.
        end: A `0D` scalar.
  
      Returns:
        The sliced composite tensor.
      """
      if isinstance(tensor, tf.SparseTensor):
          if not tf.is_tensor(end) and end < 0:
              end = tensor.dense_shape[axis] + end
          all_but_first = tf.reshape(
              tf.where(tensor.indices[:, axis] < end), [-1]
          )
          new_shape = tf.unstack(tensor.dense_shape)
          new_shape[axis] = end
          return tf.SparseTensor(
              indices=tf.gather(tensor.indices, all_but_first),
              values=tf.gather(tensor.values, all_but_first),
              dense_shape=tf.stack(new_shape),
          )
      else:
          ndims = len(tensor.shape)
          if ndims is None:
              raise ValueError(
                  "Unable to slice a tensor with unknown rank: {}".format(
                      tensor
                  )
              )
          slices = tuple(
              [slice(None)] * axis
              + [slice(None, end)]
              + [slice(None)] * (ndims - axis - 1)
          )
          return tensor[slices]
  
file_path: agents/tf_agents/utils/composite.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import tensorflow
label_desc: Use image node_id 6 for calling a global function with example usage: slice_to(tensor, axis, end) and returns: tf, tensor