# Streaming batched data

**URL:** <https://discuss.huggingface.co/t/streaming-batched-data/21603>\
**Category:** 🤗Datasets\
**Created:** [August 14, 2022, 7:19am UTC](https://discuss.huggingface.co/t/streaming-batched-data/21603 "2022-08-14T07:19:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachin](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sachin/32/26534_2.png) [@sachin](https://discuss.huggingface.co/u/sachin)\
**Post date:** [August 14, 2022, 7:19am UTC](https://discuss.huggingface.co/t/streaming-batched-data/21603/1 "2022-08-14T07:19:34Z")

</div>

Hello, I’m trying to batch a streaming dataset. This is what I have done so far:

```python
coco_train = load_dataset("facebook/pmd", use_auth_token=hf_token, name="coco", cache_dir="/kaggle/working/", streaming=True, split="train")
train_ds = coco_train.map(batched=True, batch_size=32)

```

however, doing `next(iter(train_ds))` only returns one instance (and it is quite slow). Is there something I’m missing here? How do you get a batch of data?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/52888_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [August 19, 2022, 10:23am UTC](https://discuss.huggingface.co/t/streaming-batched-data/21603/2 "2022-08-19T10:23:35Z")

</div>

Hi ! `map` with `batch=True` processes examples by batch. The output of the `map` function can be a batch of arbitrary size. Therefore you can transform a batch of `batch_size` examples to a batch of 1 example:

```python
def group_batch(batch):
    return {k: [v] for k, v in batch.items()}
train_ds = coco_train.map(group_batch, batched=True, batch_size=32)

```

---

<div class="post-metadata">

**Author:** ![sachin](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sachin/32/26534_2.png) [@sachin](https://discuss.huggingface.co/u/sachin)\
**Post date:** [August 20, 2022, 9:18am UTC](https://discuss.huggingface.co/t/streaming-batched-data/21603/3 "2022-08-20T09:18:30Z")

</div>

That worked, thank you 🙏. Although I’m not sure how you got that `group_batch` function. Is there any docs I could have referred to have figured that out for myself.

Also it seems to be significantly faster to simply download the data and then iterate rather than use the streaming method. Is that generally the case? I’m running the following and it’s running at 6.83s/ it at the moment (for a batch size of 64 on kaggle).

```python
for data in tqdm(train_ds):
    continue

```

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/52888_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [August 25, 2022, 10:15am UTC](https://discuss.huggingface.co/t/streaming-batched-data/21603/4 "2022-08-25T10:15:48Z")

</div>

> That worked, thank you 🙏. Although I’m not sure how you got that group\_batch function. Is there any docs I could have referred to have figured that out for myself.

With `map` you can change the size of a batch, so you can basically group together a batch of 32 elements into 1. You can find more examples in the docs: [Process](https://huggingface.co/docs/datasets/v2.4.0/en/process#batch-processing)

> Also it seems to be significantly faster to simply download the data and then iterate rather than use the streaming method. Is that generally the case? I’m running the following and it’s running at 6.83s/ it at the moment (for a batch size of 64 on kaggle).

It depends where the data is hosted, some hosts have limited download speed

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/52888_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [October 5, 2023, 9:07am UTC](https://discuss.huggingface.co/t/streaming-batched-data/21603/5 "2023-10-05T09:07:55Z")

</div>

Update from 2023:

Now you can iterate on batches using

```python
for batch in train_ds.iter(batch_size=32):
    ...

```
