class_docstr: Reverb ReplayBuffer exposed as a TF-Agents replay buffer.
class_docstr_tok: ["Reverb","ReplayBuffer","exposed","as","a","TF-Agents","replay","buffer","."]
docstr: Creates and returns a dataset that returns entries from the buffer.
  
  *NOTE*: If `num_steps` does not match the `sequence_length` used in the
  observer (or if `sequence_length is None` varies from episode to episode),
  then individual sequences will be truncated to the highest possible multiple
  of `num_steps` before being split.  In this case, **data loss may occur**.
  For example, in sparse reward cases where there's a reward only at the final
  frame and the sequence length is not a multiple of `num_steps`, the
  reward will be dropped.
  
  When using sparse rewards at the end of sequences, the user should prefer
  to either request full episodes (e.g., `num_steps=None` and
  `sample_batch_size=None` (for variable-length episodes)); or use
  `sequence_preprocess_fn` to first propagate the rewards from the end
  of the sequence through to the earlier frames using e.g.
  temporal discounting.
  
  Args:
    sample_batch_size: (Optional.) An optional batch_size to specify the
      number of items to return. If None (default), a single item is returned
      which matches the data_spec of this class (without a batch dimension).
      Otherwise, a batch of sample_batch_size items is returned, where each
      tensor in items will have its first dimension equal to sample_batch_size
      and the rest of the dimensions match the corresponding data_spec.
    num_steps: (Optional.)  Optional way to specify that sub-episodes are
      desired. If None (default), a batch of single items is returned.
      Otherwise, a batch of sub-episodes is returned, where a sub-episode is a
      sequence of consecutive items in the replay_buffer. The returned tensors
      will have first dimension equal to sample_batch_size (if
      sample_batch_size is not None), subsequent dimension equal to num_steps,
      and remaining dimensions which match the data_spec of this class.
    sequence_preprocess_fn: (Optional) fn for preprocessing the collected data
      before it is split into subsequences of length `num_steps`. Defined in
      `TFAgent.preprocess_sequence`. Defaults to pass through.
    num_parallel_calls: (Optional) Used to parallelize unpacking samples.
  
  Returns:
    A dataset of type tf.data.Dataset, elements of which are 2-tuples of:
      - An item or sequence of items or batch thereof
      - Auxiliary info for the items (i.e. ids, probs).
docstr_tok: ["Creates","and","returns","a","dataset","that","returns","entries","from","the","buffer",".","*","NOTE","*",":","If","`","num_steps","`","does","not","match","the","`","sequence_length","`","used","in","the","observer","(","or","if","`","sequence_length","is","None","`","varies","from","episode","to","episode",")",",","then","individual","sequences","will","be","truncated","to","the","highest","possible","multiple","of","`","num_steps","`","before","being","split",".","In","this","case",",","*","*","data","loss","may","occur","*","*",".","For","example",",","in","sparse","reward","cases","where","there","'s","a","reward","only","at","the","final","frame","and","the","sequence","length","is","not","a","multiple","of","`","num_steps","`",",","the","reward","will","be","dropped",".","When","using","sparse","rewards","at","the","end","of","sequences",",","the","user","should","prefer","to","either","request","full","episodes","(","e.g.",",","`","num_steps=None","`","and","`","sample_batch_size=None","`","(","for","variable-length","episodes",")",")",";","or","use","`","sequence_preprocess_fn","`","to","first","propagate","the","rewards","from","the","end","of","the","sequence","through","to","the","earlier","frames","using","e.g",".","temporal","discounting",".","Args",":","sample_batch_size",":","(","Optional",".",")","An","optional","batch_size","to","specify","the","number","of","items","to","return",".","If","None","(","default",")",",","a","single","item","is","returned","which","matches","the","data_spec","of","this","class","(","without","a","batch","dimension",")",".","Otherwise",",","a","batch","of","sample_batch_size","items","is","returned",",","where","each","tensor","in","items","will","have","its","first","dimension","equal","to","sample_batch_size","and","the","rest","of","the","dimensions","match","the","corresponding","data_spec",".","num_steps",":","(","Optional",".",")","Optional","way","to","specify","that","sub-episodes","are","desired",".","If","None","(","default",")",",","a","batch","of","single","items","is","returned",".","Otherwise",",","a","batch","of","sub-episodes","is","returned",",","where","a","sub-episode","is","a","sequence","of","consecutive","items","in","the","replay_buffer",".","The","returned","tensors","will","have","first","dimension","equal","to","sample_batch_size","(","if","sample_batch_size","is","not","None",")",",","subsequent","dimension","equal","to","num_steps",",","and","remaining","dimensions","which","match","the","data_spec","of","this","class",".","sequence_preprocess_fn",":","(","Optional",")","fn","for","preprocessing","the","collected","data","before","it","is","split","into","subsequences","of","length","`","num_steps","`",".","Defined","in","`","TFAgent.preprocess_sequence","`",".","Defaults","to","pass","through",".","num_parallel_calls",":","(","Optional",")","Used","to","parallelize","unpacking","samples",".","Returns",":","A","dataset","of","type","tf.data.Dataset",",","elements","of","which","are","2-tuples","of",":","-","An","item","or","sequence","of","items","or","batch","thereof","-","Auxiliary","info","for","the","items","(","i.e",".","ids",",","probs",")","."]
code: def _as_dataset(
      self,
      sample_batch_size,
      num_steps,
      sequence_preprocess_fn,
      num_parallel_calls,
  ):
      """Creates and returns a dataset that returns entries from the buffer.
  
      *NOTE*: If `num_steps` does not match the `sequence_length` used in the
      observer (or if `sequence_length is None` varies from episode to episode),
      then individual sequences will be truncated to the highest possible multiple
      of `num_steps` before being split.  In this case, **data loss may occur**.
      For example, in sparse reward cases where there's a reward only at the final
      frame and the sequence length is not a multiple of `num_steps`, the
      reward will be dropped.
  
      When using sparse rewards at the end of sequences, the user should prefer
      to either request full episodes (e.g., `num_steps=None` and
      `sample_batch_size=None` (for variable-length episodes)); or use
      `sequence_preprocess_fn` to first propagate the rewards from the end
      of the sequence through to the earlier frames using e.g.
      temporal discounting.
  
      Args:
        sample_batch_size: (Optional.) An optional batch_size to specify the
          number of items to return. If None (default), a single item is returned
          which matches the data_spec of this class (without a batch dimension).
          Otherwise, a batch of sample_batch_size items is returned, where each
          tensor in items will have its first dimension equal to sample_batch_size
          and the rest of the dimensions match the corresponding data_spec.
        num_steps: (Optional.)  Optional way to specify that sub-episodes are
          desired. If None (default), a batch of single items is returned.
          Otherwise, a batch of sub-episodes is returned, where a sub-episode is a
          sequence of consecutive items in the replay_buffer. The returned tensors
          will have first dimension equal to sample_batch_size (if
          sample_batch_size is not None), subsequent dimension equal to num_steps,
          and remaining dimensions which match the data_spec of this class.
        sequence_preprocess_fn: (Optional) fn for preprocessing the collected data
          before it is split into subsequences of length `num_steps`. Defined in
          `TFAgent.preprocess_sequence`. Defaults to pass through.
        num_parallel_calls: (Optional) Used to parallelize unpacking samples.
  
      Returns:
        A dataset of type tf.data.Dataset, elements of which are 2-tuples of:
          - An item or sequence of items or batch thereof
          - Auxiliary info for the items (i.e. ids, probs).
      """
      self._verify_num_steps(num_steps)
  
      if (
          num_parallel_calls
          and sample_batch_size
          and num_parallel_calls > sample_batch_size
      ):
          raise ValueError(
              "num_parallel_calls cannot be bigger than sample_batch_size "
              "{} > {}".format(num_parallel_calls, sample_batch_size)
          )
      num_parallel_calls = (
          num_parallel_calls or tf.data.experimental.AUTOTUNE
      )
      total_batch_size = sample_batch_size or 1
      # This determines how many parallel Reverb dataset pipelines we create -
      # aka "how many interleaves."
      cycle_length = min(total_batch_size, self._max_cycle_length)
      batch_size_per_interleave = total_batch_size // cycle_length
      # Recomended buffer_size per connection is ~2-3x the batch size.
      dataset_buffer_size = (
          self._dataset_buffer_size or 3 * batch_size_per_interleave
      )
      # Set a maximum number of workers per iterator due to interleave
      num_workers_per_iterator = min(
          self._num_workers_per_iterator, batch_size_per_interleave
      )
  
      def per_sequence_fn(sample):
          # At this point, each sample data contains a sequence of trajectories.
          data, info = sample.data, sample.info
          if sequence_preprocess_fn:
              data = sequence_preprocess_fn(data)
          return data, info
  
      def dataset_transformation(dataset):
          if num_steps and num_steps != self._sequence_length:
              dataset = (
                  dataset
                  # Truncate and reshape elements to [X, num_steps, ...]
                  .map(
                      lambda *s: truncate_reshape_rows_by_num_steps(
                          s, num_steps
                      )
                  )
                  # Unbatch to get elements shaped [num_steps, ...]; each element
                  # contains non-overlapping time steps.
                  .unbatch()
              )
              shuffle_size = 100
              if self._sequence_length:
                  shuffle_size = self._sequence_length // num_steps
              # We will receive batches from interleaves of size
              # cycle_length and batching them to size sample_batch_size.
              # To try and ensure i.i.d. samples in each minibatch, make the shuffle
              # buffer larger.
              shuffle_size *= batch_size_per_interleave
              dataset = dataset.shuffle(shuffle_size)
          return dataset
  
      dataset = make_reverb_dataset(
          self._server_address,
          self._table_name,
          data_spec=self._data_spec,
          max_in_flight_samples_per_worker=dataset_buffer_size,
          batch_size=sample_batch_size,
          sequence_length=self._sequence_length,
          cycle_length=cycle_length,
          num_parallel_calls=num_parallel_calls,
          per_sequence_fn=per_sequence_fn,
          dataset_transformation=dataset_transformation,
          num_workers_per_iterator=num_workers_per_iterator,
          rate_limiter_timeout_ms=self._rate_limiter_timeout_ms,
      )
  
      return dataset
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import gin
  import tensorflow.compat.v2
  from tf_agents.replay_buffers import replay_buffer
  from tf_agents.typing import types
  from tf_agents.utils import lazy_loader
label_desc: Use image node_id 9 for calling the ReverbReplayBuffer obj's underlying member method code with example usage: obj._as_dataset(sample_batch_size, num_steps, sequence_preprocess_fn, num_parallel_calls) and returns: dataset, data,  info, dataset