Stream
Last updated
Last updated
Dataset streaming lets you work with a dataset without downloading it. The data is streamed as you iterate over the dataset. This is especially helpful when:
You donβt want to wait for an extremely large dataset to download.
The dataset size exceeds the amount of available disk space on your computer.
You want to quickly explore just a few samples of a dataset.
Copied
Dataset streaming also lets you work with a dataset made of local files without doing any conversion. In this case, the data is streamed from the local files as you iterate over the dataset. This is especially helpful when:
You donβt want to wait for an extremely large local dataset to be converted to Arrow.
The converted files size would exceed the amount of available disk space on your computer.
You want to quickly explore just a few samples of a dataset.
Copied
Copied
Copied
Copied
Sometimes you may want to reshuffle the dataset after each epoch. This will require you to set a different seed for each epoch. Use IterableDataset.set_epoch()
in between epochs to tell the dataset what epoch youβre on.
Your seed effectively becomes: initial seed + current epoch
.
Copied
You can split your dataset one of two ways:
Copied
Copied
take
and skip
prevent future calls to shuffle
because they lock in the order of the shards. You should shuffle
your dataset before splitting it.
Copied
Define sampling probabilities from each of the original datasets for more control over how each of them are sampled and combined. Set the probabilities
argument with your desired sampling probabilities:
Copied
Around 80% of the final dataset is made of the en_dataset
, and 20% of the fr_dataset
.
You can also specify the stopping_strategy
. The default strategy, first_exhausted
, is a subsampling strategy, i.e the dataset construction is stopped as soon one of the dataset runs out of samples. You can specify stopping_strategy=all_exhausted
to execute an oversampling strategy. In this case, the dataset construction is stopped as soon as every samples in every dataset has been added at least once. In practice, it means that if a dataset is exhausted, it will return to the beginning of this dataset until the stop criterion has been reached. Note that if no sampling probabilities are specified, the new dataset will have max_length_datasets*nb_dataset samples
.
The following methods allow you to modify the columns of a dataset. These methods are useful for renaming or removing columns and changing columns to a new set of features.
Copied
Copied
Copied
Casting only works if the original feature type and new feature type are compatible. For example, you can cast a column with the feature type Value('int32')
to Value('bool')
if the original column only contains ones and zeros.
Copied
It allows you to apply a processing function to each example in a dataset, independently or in batches. This function can even create new rows and columns.
Copied
Copied
Copied
Tokenization
Copied
Copied
Copied
PytorchHide Pytorch contentCopied
Lastly, create a simple training loop and start training:
Copied
For example, the English split of the dataset is 1.2 terabytes, but you can use it instantly with streaming. Stream a dataset by setting streaming=True
in as shown below:
For example, you can stream a local dataset of hundreds of compressed JSONL files like to use it instantly:
Loading a dataset in streaming mode creates a new dataset type instance (instead of the classic object), known as an . This special type of dataset has its own set of processing methods shown below.
An is useful for iterative jobs like training a model. You shouldnβt use a for jobs that require random access to examples because you have to iterate all over it using a for loop. Getting the last example in an iterable dataset would require you to iterate over all the previous examples. You can find more details in the .
If you have an existing object, you can convert it to an with the function. This is actually faster than setting the streaming=True
argument in because the data is streamed from local files.
The function supports sharding when the is instantiated. This is useful when working with big datasets, and youβd like to shuffle the dataset or to enable fast parallel loading with a PyTorch DataLoader.
Like a regular object, you can also shuffle a with .
The buffer_size
argument controls the size of the buffer to randomly sample examples from. Letβs say your dataset has one million examples, and you set the buffer_size
to ten thousand. will randomly select examples from the first ten thousand examples in the buffer. Selected examples in the buffer are replaced with new examples. By default, the buffer size is 1,000.
will also shuffle the order of the shards if the dataset is sharded into multiple files.
returns the first n
examples in a dataset:
omits the first n
examples in a dataset and returns the remaining examples:
can combine an with other datasets. The combined dataset returns alternating examples from each of the original datasets.
Use when you need to rename a column in your dataset. Features associated with the original column are actually moved under the new column name, instead of just replacing the original column in-place.
Provide with the name of the original column, and the new column name:
When you need to remove one or more columns, give the name of the column to remove. Remove more than one column by providing a list of column names:
changes the feature type of one or more columns. This method takes your new Features
as its argument. The following sample code shows how to change the feature types of ClassLabel
and Value
:
Use to change the feature type of just one column. Pass the column name and its new feature type as arguments:
Similar to the function for a regular , π Datasets features for processing an . applies processing on-the-fly when examples are streamed.
The following example demonstrates how to tokenize a . The function needs to accept and output a dict
:
Next, apply this function to the dataset with :
Letβs take a look at another example, except this time, you will remove a column with . When you remove a column, it is only removed after the example has been provided to the mapped function. This allows the mapped function to use the content of the columns before they are removed.
Specify the column to remove with the remove_columns
argument in :
also supports working with batches of examples. Operate on batches by setting batched=True
. The default batch size is 1000, but you can adjust it with the batch_size
argument. This opens the door to many interesting applications such as tokenization, splitting long sentences into shorter chunks, and data augmentation.
See other examples of batch processing in the documentation. They work the same for iterable datasets.
You can filter rows in the dataset based on a predicate function using . It returns rows that match a specified condition:
can also filter by indices if you set with_indices=True
:
can be integrated into a training loop. First, shuffle the dataset: