Dataset or IterableDataset
Last updated
Last updated
There are two types of dataset objects, a and an . Whichever type of dataset you choose to use or create depends on the size of the dataset. In general, an is ideal for big datasets (think hundreds of GBs!) due to its lazy behavior and speed advantages, while a is great for everything else. This page will compare the differences between a and an to help you pick the right dataset object for you.
When you have a regular , you can access it using my_dataset[0]
. This provides random access to the rows. Such datasets are also called “map-style” datasets. For example you can download ImageNet-1k like this and access any row:
Copied
But one caveat is that you must have the entire dataset stored on your disk or in memory, which blocks you from accessing datasets bigger than the disk. Because it can become inconvenient for big datasets, there exists another type of dataset, the . When you have an IterableDataset
, you can access it using a for
loop to load the data progressively as you iterate over the dataset. This way, only a small fraction of examples is loaded in memory, and you don’t write anything on disk.
For example, you can stream the ImageNet-1k dataset without downloading it on disk:
Copied
This is not the only difference though, because the “lazy” behavior of an IterableDataset
is also present when it comes to dataset creation and processing.
Copied
To create an IterableDataset
on the other hand, you must provide a “lazy” way to load the data. In Python, we generally use generator functions. These functions yield
one example at a time, which means you can’t access a row by slicing it like a regular Dataset
:
Copied
Copied
However, this requires a conversion step from CSV to Arrow format, which takes time and disk space if your dataset is big.
To save disk space and skip the conversion step, you can define an IterableDataset
by streaming from the local files directly. This way, the data is read progressively from the local files as you iterate over the dataset:
Copied
Copied
Because of that, you can chain multiple processing steps and they will all run at once when you start iterating over the dataset:
Copied
Copied
Copied
Copied
Copied
Copied
If you’re using your dataset on multiple epochs, the effective seed to shuffle the shards order in the shuffle buffer is seed + epoch
. It makes it easy to reshuffle a dataset between epochs:
Copied
Copied
Copied
Streaming can read online data without writing any file to disk. For example, you can stream datasets made out of multiple shards, each of which is hundreds of gigabytes like , or . Learn more about how to stream a dataset in the .
You can create a using lists or dictionaries, and the data is entirely converted to Arrow so you can easily access any row:
It is possible to convert local or remote data files to an Arrow using :
Many file formats are supported, like CSV, JSONL, and Parquet, as well as image and audio files. You can find more information in the corresponding guides for loading , , , and datasets.
When you process a object using , the entire dataset is processed immediately and returned. This is similar to how pandas
works for example.
On the other hand, due to the “lazy” nature of an IterableDataset
, calling does not apply your map
function over the full dataset. Instead, your map
function is applied on-the-fly.
When you shuffle a using , you apply an exact shuffling of the dataset. It works by taking a list of indices [0, 1, 2, ... len(my_dataset) - 1]
and shuffling this list. Then, accessing my_dataset[0]
returns the row and index defined by the first element of the indices mapping that has been shuffled:
Since we don’t have random access to the rows in the case of an IterableDataset
, we can’t use a shuffled list of indices and access a row at an arbitrary position. This prevents the use of exact shuffling. Instead, a fast approximate shuffling is used in . It uses a shuffle buffer to sample random examples iteratively from the dataset. Since the dataset is still read iteratively, it provides excellent speed performance:
But using a shuffle buffer is not enough to provide a satisfactory shuffling for machine learning model training. So also shuffles the dataset shards if your dataset is made of multiple files or sources:
Regular objects are based on Arrow which provides fast random access to the rows. Thanks to memory mapping and the fact that Arrow is an in-memory format, reading data from disk doesn’t do expensive system calls and deserialization. It provides even faster data loading when iterating using a for
loop by iterating on contiguous Arrow record batches.
However as soon as your has an indices mapping (via for example), the speed can become 10x slower. This is because there is an extra step to get the row index to read using the indices mapping, and most importantly, you aren’t reading contiguous chunks of data anymore. To restore the speed, you’d need to rewrite the entire dataset on your disk again using , which removes the indices mapping. This may take a lot of time depending of the size of your dataset though:
In this case, we recommend switching to an and leveraging its fast approximate shuffling method . It only shuffles the shards order and adds a shuffle buffer to your dataset, which keeps the speed of your dataset optimal. You can also reshuffle the dataset easily:
If you want to benefit from the “lazy” behavior of an or their speed advantages, you can switch your map-style to an :
If you want to shuffle your dataset or , we recommend generating a sharded :