# tf_agents.replay_buffers.TFUniformReplayBuffer

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/replay_buffers/tf_uniform_replay_buffer.py#L46-L607) |

A TFUniformReplayBuffer with batched adds and uniform sampling.

Inherits From: [`ReplayBuffer`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/replay_buffers/replay_buffer/ReplayBuffer)

#### View aliases


**Main aliases**

[`tf_agents.replay_buffers.tf_uniform_replay_buffer.TFUniformReplayBuffer`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/replay_buffers/TFUniformReplayBuffer)

<br />

    tf_agents.replay_buffers.TFUniformReplayBuffer(
        data_spec,
        batch_size,
        max_length=1000,
        scope='TFUniformReplayBuffer',
        device='cpu:*',
        table_fn=https://www.tensorflow.org/agents/api_docs/python/tf_agents/replay_buffers/table/Table,
        dataset_drop_remainder=False,
        dataset_window_shift=None,
        stateful_dataset=False
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Replay Buffers](https://www.tensorflow.org/agents/tutorials/5_replay_buffers_tutorial) - [Checkpointer and PolicySaver](https://www.tensorflow.org/agents/tutorials/10_checkpointer_policysaver_tutorial) - [DQN C51/Rainbow](https://www.tensorflow.org/agents/tutorials/9_c51_tutorial) - [Tutorial on Multi Armed Bandits in TF-Agents](https://www.tensorflow.org/agents/tutorials/bandits_tutorial) - [A Tutorial on Multi-Armed Bandits with Per-Arm Features](https://www.tensorflow.org/agents/tutorials/per_arm_bandits_tutorial) |

<br />

<br />

<br />

| ## Args ||
|---|---|
| `data_spec` | A TensorSpec or a list/tuple/nest of TensorSpecs describing a single item that can be stored in this buffer. |
| `batch_size` | Batch dimension of tensors when adding to buffer. |
| `max_length` | The maximum number of items that can be stored in a single batch segment of the buffer. |
| `scope` | Scope prefix for variables and ops created by this class. |
| `device` | A TensorFlow device to place the Variables and ops. |
| `table_fn` | Function to create tables `table_fn(data_spec, capacity)` that can read/write nested tensors. |
| `dataset_drop_remainder` | If `True`, then when calling `as_dataset` with arguments `single_deterministic_pass=True` and `sample_batch_size is not None`, the final batch will be dropped if it does not contain exactly `sample_batch_size` items. This is helpful for static shape inference as the resulting tensors will always have leading dimension `sample_batch_size` instead of `None`. |
| `dataset_window_shift` | Window shift used when calling `as_dataset` with arguments `single_deterministic_pass=True` and `num_steps is not None`. This determines how the resulting frames are windowed. If `None`, then there is no overlap created between frames and each frame is seen exactly once. For example, if `max_length=5`, `num_steps=2`, `sample_batch_size=None`, and `dataset_window_shift=None`, then the datasets returned will have frames `{[0, 1], [2, 3], [4]}`. If `dataset_window_shift is not None`, then windows are created with a window overlap of `dataset_window_shift` and you will see each frame up to `num_steps` times. For example, if `max_length=5`, `num_steps=2`, `sample_batch_size=None`, and `dataset_window_shift=1`, then the datasets returned will have windows of shifted repeated frames: `{[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]}`. For more details, see the documentation of [`tf.data.Dataset.window`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#window), specifically for the `shift` argument. The default behavior is to not overlap frames (`dataset_window_shift=None`) but users often want to see all combinations of frame sequences, in which case `dataset_window_shift=1` is the appropriate value. |
| `stateful_dataset` | whether the dataset contains stateful ops or not. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `capacity` | Returns the capacity of the replay buffer. |
| `data_spec` | Returns the spec for items in the replay buffer. |
| `device` | <br /> <br /> |
| `scope` | <br /> <br /> |
| `stateful_dataset` | Returns whether the dataset of the replay buffer has stateful ops. |
| `table_fn` | <br /> <br /> |

<br />

## Methods

### `add_batch`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/replay_buffers/replay_buffer.py#L72-L83)

    add_batch(
        items
    )

Adds a batch of items to the replay buffer.

<br />

<br />

<br />

| Args ||
|---|---|
| `items` | An item or list/tuple/nest of items to be added to the replay buffer. `items` must match the data_spec of this class, with a batch_size dimension added to the beginning of each tensor/array. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| Adds `items` to the replay buffer. ||

<br />

### `as_dataset`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/replay_buffers/tf_uniform_replay_buffer.py#L312-L327)

    as_dataset(
        sample_batch_size=None,
        num_steps=None,
        num_parallel_calls=None,
        single_deterministic_pass=False
    )

Creates and returns a dataset that returns entries from the buffer.

A single entry from the dataset is the result of the following pipeline:

- Sample sequences from the underlying data store
- (optionally) Process them with `sequence_preprocess_fn`,
- (optionally) Split them into subsequences of length `num_steps`
- (optionally) Batch them into batches of size `sample_batch_size`.

In practice, this pipeline is executed in parallel as much as possible
if `num_parallel_calls != 1`.

#### Some additional notes:

If `num_steps is None`, different replay buffers will behave differently.
For example, `TFUniformReplayBuffer` will return single time steps without
a time dimension. In contrast, e.g., `EpisodicReplayBuffer` will return
full sequences (since each sequence may be an episode of unknown length,
the outermost shape dimension will be `None`).

If `sample_batch_size is None`, no batching is performed; and there is no
outer batch dimension in the returned Dataset entries. This setting
is useful with variable episode lengths using e.g. `EpisodicReplayBuffer`,
because it allows the user to get full episodes back, and use [`tf.data`](https://www.tensorflow.org/api_docs/python/tf/data)
to build padded or truncated batches themselves.

If `single_deterministic_pass == True`, the replay buffer will make
every attempt to ensure every time step is visited once and exactly once
in a deterministic manner (though true determinism depends on the
underlying data store). Additional work may be done to ensure minibatches
do not have multiple rows from the same episode. In some cases, this
may mean arguments like `num_parallel_calls` are ignored.

<br />

<br />

<br />

| 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. |
| `num_parallel_calls` | (Optional.) A [`tf.int32`](https://www.tensorflow.org/api_docs/python/tf#int32) scalar [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor), representing the number elements to process in parallel. If not specified, elements will be processed sequentially. |
| `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`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/agents/TFAgent#preprocess_sequence). Defaults to pass through. |
| `single_deterministic_pass` | Python boolean. If `True`, the dataset will return a single deterministic pass through its underlying data. **NOTE**: If the buffer is modified while a Dataset iterator is iterating over this data, the iterator may miss any new data or otherwise have subtly invalid data. |

<br />

<br />

<br />

| Returns ||
|---|---|
| A dataset of type tf.data.Dataset, elements of which are 2-tuples of: <br /> - An item or sequence of items or batch thereof - Auxiliary info for the items (i.e. ids, probs). ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `NotImplementedError` | If a non-default argument value is not supported. |
| `ValueError` | If the data spec contains lists that must be converted to tuples. |

<br />

### `clear`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/replay_buffers/replay_buffer.py#L261-L267)

    clear()

Resets the contents of replay buffer.

<br />

<br />

<br />

| Returns ||
|---|---|
| Clears the replay buffer contents. ||

<br />

### `gather_all`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/replay_buffers/replay_buffer.py#L245-L259)

    gather_all()

Returns all the items in buffer. (deprecated)

> [!WARNING]
> **Deprecated:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use `as_dataset(..., single_deterministic_pass=True)` instead.

<br />

<br />

<br />

| Returns ||
|---|---|
| Returns all the items currently in the buffer. Returns a tensor of shape \[B, T, ...\] where B = batch size, T = timesteps, and the remaining shape is the shape spec of the items in the buffer. ||

<br />

### `get_next`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/replay_buffers/replay_buffer.py#L85-L132)

    get_next(
        sample_batch_size=None, num_steps=None, time_stacked=True
    )

Returns an item or batch of items from the buffer. (deprecated)

> [!WARNING]
> **Deprecated:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use \`as_dataset(..., single_deterministic_pass=False) instead.

<br />

<br />

<br />

| 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. See examples below. |
| `num_steps` | (Optional.) Optional way to specify that sub-episodes are desired. If None (default), in non-episodic replay buffers, a batch of single items is returned. In episodic buffers, full episodes are returned (note that sample_batch_size must be None in that case). 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, if time_stacked=True and remaining dimensions which match the data_spec of this class. See examples below. |
| `time_stacked` | (Optional.) Boolean, when true and num_steps \> 1 it returns the items stacked on the time dimension. See examples below for details. Examples of tensor shapes returned: (B = batch size, T = timestep, D = data spec) get_next(sample_batch_size=None, num_steps=None, time_stacked=True) return shape (non-episodic): \[D\] return shape (episodic): [T, D](https://www.tensorflow.org/agents/api_docs/python/tf_agents/replay_buffers/T%20=%20full%20length%20of%20the%20episode) get_next(sample_batch_size=B, num_steps=None, time_stacked=True) return shape (non-episodic): \[B, D\] return shape (episodic): Not supported get_next(sample_batch_size=B, num_steps=T, time_stacked=True) return shape: \[B, T, D\] get_next(sample_batch_size=None, num_steps=T, time_stacked=False) return shape: (\[D\], \[D\], ..) T tensors in the tuple get_next(sample_batch_size=B, num_steps=T, time_stacked=False) return shape: (\[B, D\], \[B, D\], ..) T tensors in the tuple |

<br />

<br />

<br />

| Returns ||
|---|---|
| A 2-tuple containing: <br /> - An item or sequence of (optionally batched and stacked) items. - Auxiliary info for the items (i.e. ids, probs). ||

<br />

### `num_frames`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/replay_buffers/replay_buffer.py#L68-L70)

    num_frames()

Returns the number of frames in the replay buffer.