# Use load dataset to load a sample of the dataset

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-load-dataset-to-load-a-sample-of-the-dataset%2F6245%3E%5C
**Category:** 🤗Datasets\
**Created:** [May 19, 2021, 7:02am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-load-dataset-to-load-a-sample-of-the-dataset%2F6245 "2021-05-19T07:02:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theainerd](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/theainerd/32/38982_2.png) [@theainerd](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ftheainerd)%5C
**Post date:** [May 19, 2021, 7:02am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-load-dataset-to-load-a-sample-of-the-dataset%2F6245%2F1 "2021-05-19T07:02:19Z")

</div>

Hello 👋,

I would really love to load a sample of the dataset rather than the whole data at first. Can I do this with hugging face library. I don’t want to download the full dataset as it is 23GB large rather just download a sample and work on it asap before working on the whole dataset.

Any ideas ??

---

<div class="post-metadata">

**Author:** ![osanseviero](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/osanseviero/32/38738_2.png) [@osanseviero](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fosanseviero)%5C
**Post date:** [May 19, 2021, 7:21am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-load-dataset-to-load-a-sample-of-the-dataset%2F6245%2F2 "2021-05-19T07:21:40Z")

</div>

As far as I know, this is something that is actively being worked on.

> <https://github.com/huggingface/datasets/pull/2375>
>
> \# Dataset Streaming
> 
> \## API
> 
> Current API is
> 
> \`\`\`python
> from datasets impo…rt load\_dataset
> 
> \# Load an IterableDataset without downloading data
> snli = load\_dataset("snli", streaming=True)
> 
> \# Access examples by streaming data
> print(next(iter(snli\["train"\]))) 
> \# {'premise': 'A person on a horse jumps over a broken down airplane.',
> \# 'hypothesis': 'A person is training his horse for a competition.',
> \# 'label': 1}
> \`\`\`
> 
> I already implemented a few methods:
> \- IterableDataset.map: apply transforms on-the-fly to the examples
> \- IterableDataset.shuffle: shuffle the data \_a la\_ TFDS, i.e. with a shuffling buffer
> \- IterableDataset.with\_format: set the format to \`"torch"\` to get a \`torch.utils.data.IterableDataset\`
> \- merge\_datasets: merge two iterable datasets by alternating one or the other (you can specify the probabilities)
> 
> I would love to have your opinion on the API design :)
> 
> \## Implementation details
> 
> \### Streaming
> 
> Data streaming is done using \`fsspec\` which has nice caching features.
> 
> To make dataset streaming work I extend the \`open\` function of dataset scripts to support opening remote files without downloading them entirely. It also works with remote compressed archives (currently only zip is supported):
> 
> \`\`\`python
> \# Get a file-like object by streaming data from a remote file
> open("https://github.com/davidsbatista/NER-datasets/raw/master/CONLL2003/train.txt")
> 
> \# Get a file-like object by streaming data from a remote compressed archive by using the hop separator "::"
> open("zip://snli\_1.0\_train.txt::https://nlp.stanford.edu/projects/snli/snli\_1.0.zip")
> \`\`\`
> 
> I also extend the \`os.path.join\` function to support navigation in remote compressed archives, since it has to deal with the \`"::"\` separator. This separator is used by \`fsspec\`.
> 
> Finally I also added a retry mechanism in case the connection fails during data streaming.
> 
> \### Transforms
> 
> An IterableDataset wraps an ExamplesIterable instance. There are different subclasses depending on the transforms we want to apply:
> \- ExamplesIterable: the basic one
> \- MappedExamplesIterable: an iterable with a \`map\` function applied on the fly
> \- BufferShuffledExamplesIterable: an iterable with a shuffling buffer
> \- CyclingMultiSourcesExamplesIterable: alternates between several ExamplesIterable
> \- RandomlyCyclingMultiSourcesExamplesIterable: randomly alternates between several ExamplesIterable
> 
> \### DatasetBuilder
> 
> I use the same builders as usual. I just added a new method \`\_get\_examples\_iterable\_for\_split\` to get an ExamplesIterable for a given split. Currently only the GeneratorBasedBuilder and the ArrowBasedBuilder implement it.
> 
> The BeamBasedBuilder doesn't implement it yet.
> It means that datasets like wikipedia and natural\_questions can't be loaded as IterableDataset for now.
> 
> \## Other details
> 
> \<S\>I may have to do some changes in many dataset script to use \`download\` instead of \`download\_and\_extract\` when extraction is not needed. This will avoid errors for streaming.\</s\>
> 
> EDIT: Actually I just check for the extension of the file to do extraction only if needed.
> 
> EDIT2: It's not possible to stream from .tar.gz files without downloading the file completely. For now I raise an error if one want to get a streaming dataset based on .tar.gz files.
> 
> \## TODO
> 
> usual stuff:
> 
> \- \[x\] make streaming dependency "aiohttp" optional: \`pip install datasets\[streaming\]\`
> \- \[x\] tests
> \- \[x\] docs

@lhoestq might be able to provide more info 🙂

---

<div class="post-metadata">

**Author:** ![theainerd](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/theainerd/32/38982_2.png) [@theainerd](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ftheainerd)%5C
**Post date:** [May 19, 2021, 7:26am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-load-dataset-to-load-a-sample-of-the-dataset%2F6245%2F3 "2021-05-19T07:26:08Z")

</div>

Thanks for the info. Such a time saver.

---

<div class="post-metadata">

**Author:** ![vblagoje](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/vblagoje/32/20818_2.png) [@vblagoje](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvblagoje)%5C
**Post date:** [May 24, 2021, 5:11pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-load-dataset-to-load-a-sample-of-the-dataset%2F6245%2F4 "2021-05-24T17:11:56Z")

</div>

This, changes everything. Can’t wait @lhoestq
