code: def _sliced_fn(f, n_slices):
      """Divide function f's inputs into several slices.
  
      Evaluate f on those slices, and then average the result. It is useful when
      memory is not enough to process all data at once.
      Assume:
      1. each of f's inputs is iterable and composed of multiple "samples"
      2. outputs can be averaged over "samples"
  
      Args:
          f (Callable): Function to evaluate.
          n_slices (int): Number of slices to evaluate over.
  
      Returns:
          Callable: Sliced version of f.
  
      """
  
      def _sliced_f(
          sliced_inputs, non_sliced_inputs=None
      ):  # pylint: disable=missing-return-doc, missing-return-type-doc # noqa: E501
          if non_sliced_inputs is None:
              non_sliced_inputs = []
  
          if isinstance(non_sliced_inputs, tuple):
              non_sliced_inputs = list(non_sliced_inputs)
  
          n_paths = len(sliced_inputs[0])
          slice_size = max(1, n_paths // n_slices)
          ret_vals = None
          for start in range(0, n_paths, slice_size):
              inputs_slice = [
                  v[start : start + slice_size] for v in sliced_inputs
              ]
              slice_ret_vals = f(*(inputs_slice + non_sliced_inputs))
  
              if not isinstance(slice_ret_vals, (tuple, list)):
                  slice_ret_vals_as_list = [slice_ret_vals]
              else:
                  slice_ret_vals_as_list = slice_ret_vals
  
              scaled_ret_vals = [
                  np.asarray(v) * len(inputs_slice[0])
                  for v in slice_ret_vals_as_list
              ]
  
              if ret_vals is None:
                  ret_vals = scaled_ret_vals
              else:
                  ret_vals = [
                      x + y for x, y in zip(ret_vals, scaled_ret_vals)
                  ]
  
          ret_vals = [v / n_paths for v in ret_vals]
          if not isinstance(slice_ret_vals, (tuple, list)):
              ret_vals = ret_vals[0]
          elif isinstance(slice_ret_vals, tuple):
              ret_vals = tuple(ret_vals)
  
          return ret_vals
  
      return _sliced_f
  
imports: import abc
  from dowel import logger
  import numpy
  import tensorflow
  from garage.tf import compile_function, flatten_tensor_variables, new_tensor_like
  from garage.tf.optimizers._dtypes import LazyDict
label_desc: Use image node_id 2 for calling a global function with example usage: _sliced_fn(f, n_slices) and returns: _sliced_f, ret_vals