🤗 Datasets relies on two main classes during the dataset building process: DatasetBuilder and BuilderConfig.
( cache_dir: typing.Optional[str] = None config_name: typing.Optional[str] = None hash: typing.Optional[str] = None base_path: typing.Optional[str] = None info: typing.Optional[datasets.info.DatasetInfo] = None features: typing.Optional[datasets.features.features.Features] = None use_auth_token: typing.Union[str, bool, NoneType] = None repo_id: typing.Optional[str] = None data_files: typing.Union[str, list, dict, datasets.data_files.DataFilesDict, NoneType] = None data_dir: typing.Optional[str] = None name = 'deprecated' **config_kwargs )
Parameters
"~/.cache/huggingface/datasets"
.
Added in 2.3.0
Parameter name was renamed to config_name.
self._relative_data_dir
) is: name/version/hash/
.
"~/.huggingface"
.
os.path.join(data_dir, "**")
as data_files.
For builders that require manual download, it must be the path to the local directory containing the
manually downloaded data.
Deprecated in 2.3.0
Use config_name instead.
Abstract base class for all datasets.
DatasetBuilder has 3 key methods:
Configuration: Some DatasetBuilders expose multiple variants of the dataset by defining a [BuilderConfig] subclass and accepting a config object (or name) on construction. Configurable datasets expose a pre-defined set of configurations in [DatasetBuilder.builder_configs].
( split: typing.Optional[datasets.splits.Split] = None run_post_process = True ignore_verifications = False in_memory = False )
Parameters
datasets.Split
) — Which subset of the data to return.
Return a Dataset for the specified split.
( output_dir: typing.Optional[str] = None download_config: typing.Optional[datasets.download.download_config.DownloadConfig] = None download_mode: typing.Optional[datasets.download.download_manager.DownloadMode] = None ignore_verifications: bool = False try_from_hf_gcs: bool = True dl_manager: typing.Optional[datasets.download.download_manager.DownloadManager] = None base_path: typing.Optional[str] = None use_auth_token: typing.Union[str, bool, NoneType] = None file_format: str = 'arrow' max_shard_size: typing.Union[int, str, NoneType] = None storage_options: typing.Optional[dict] = None **download_and_prepare_kwargs )
Parameters
str
, optional) — output directory for the dataset.
Default to this builder’s cache_dir
, which is inside ~/.cache/huggingface/datasets by default.
Added in 2.5.0
REUSE_DATASET_IF_EXISTS
bool
) — Ignore the verifications of the downloaded/processed dataset information (checksums/size/splits/…)
bool
) — If True, it will try to download the already prepared dataset from the Hf google cloud storage
str
, optional) — base path for relative paths that are used to download files. This can be a remote url.
If not specified, the value of the base_path attribute (self.base_path) will be used instead.
Union[str, bool]
, optional) — Optional string or boolean to use as Bearer token for remote files on the Datasets Hub.
If True, will get token from ~/.huggingface.
str
, optional) — format of the data files in which the dataset will be written.
Supported formats: “arrow”, “parquet”. Default to “arrow” format.
If the format is “parquet”, then image and audio data are embedded into the Parquet files instead of pointing to local files.
Added in 2.5.0
Union[str, int]
, optional) — Maximum number of bytes written per shard.
Only available for the “parquet” format with a default of “500MB”. The size is based on uncompressed data size,
so in practice your shard files may be smaller than max_shard_size thanks to Parquet compression.
Added in 2.5.0
dict
, optional) — Key/value pairs to be passed on to the caching file-system backend, if any.
Added in 2.5.0
Downloads and prepares dataset for reading.
Example:
Downdload and prepare the dataset as Arrow files that can be loaded as a Dataset using builder.as_dataset()
>>> from datasets import load_dataset_builder
>>> builder = load_dataset_builder("rotten_tomatoes")
>>> ds = builder.download_and_prepare()
Downdload and prepare the dataset as sharded Parquet files locally
>>> from datasets import load_dataset_builder
>>> builder = load_dataset_builder("rotten_tomatoes")
>>> ds = builder.download_and_prepare("./output_dir", file_format="parquet")
Downdload and prepare the dataset as sharded Parquet files in a cloud storage
>>> from datasets import load_dataset_builder
>>> storage_options = {"key": aws_access_key_id, "secret": aws_secret_access_key}
>>> builder = load_dataset_builder("rotten_tomatoes")
>>> ds = builder.download_and_prepare("s3://my-bucket/my_rotten_tomatoes", storage_options=storage_options, file_format="parquet")
Empty dict if doesn’t exist
Example:
>>> from datasets import load_dataset_builder
>>> ds_builder = load_dataset_builder('rotten_tomatoes')
>>> ds_builder.get_all_exported_dataset_infos()
{'default': DatasetInfo(description="Movie Review Dataset.
a dataset of containing 5,331 positive and 5,331 negative processed
s from Rotten Tomatoes movie reviews. This data was first used in Bo
Lillian Lee, ``Seeing stars: Exploiting class relationships for
t categorization with respect to rating scales.'', Proceedings of the
5.
ion='@InProceedings{Pang+Lee:05a,
= {Bo Pang and Lillian Lee},
= {Seeing stars: Exploiting class relationships for sentiment
categorization with respect to rating scales},
tle = {Proceedings of the ACL},
2005
age='http://www.cs.cornell.edu/people/pabo/movie-review-data/', license='', features={'text': Value(dtype='string', id=None), 'label': ClassLabel(num_classes=2, names=['neg', 'pos'], id=None)}, post_processed=None, supervised_keys=SupervisedKeysData(input='', output=''), task_templates=[TextClassification(task='text-classification', text_column='text', label_column='label')], builder_name='rotten_tomatoes_movie_review', config_name='default', version=1.0.0, splits={'train': SplitInfo(name='train', num_bytes=1074810, num_examples=8530, dataset_name='rotten_tomatoes_movie_review'), 'validation': SplitInfo(name='validation', num_bytes=134679, num_examples=1066, dataset_name='rotten_tomatoes_movie_review'), 'test': SplitInfo(name='test', num_bytes=135972, num_examples=1066, dataset_name='rotten_tomatoes_movie_review')}, download_checksums={'https://storage.googleapis.com/seldon-datasets/sentence_polarity_v1/rt-polaritydata.tar.gz': {'num_bytes': 487770, 'checksum': 'a05befe52aafda71d458d188a1c54506a998b1308613ba76bbda2e5029409ce9'}}, download_size=487770, post_processing_size=None, dataset_size=1345461, size_in_bytes=1833231)}
Empty DatasetInfo if doesn’t exist
Example:
>>> from datasets import load_dataset_builder
>>> ds_builder = load_dataset_builder('rotten_tomatoes')
>>> ds_builder.get_exported_dataset_info()
DatasetInfo(description="Movie Review Dataset.
a dataset of containing 5,331 positive and 5,331 negative processed
s from Rotten Tomatoes movie reviews. This data was first used in Bo
Lillian Lee, ``Seeing stars: Exploiting class relationships for
t categorization with respect to rating scales.'', Proceedings of the
5.
ion='@InProceedings{Pang+Lee:05a,
= {Bo Pang and Lillian Lee},
= {Seeing stars: Exploiting class relationships for sentiment
categorization with respect to rating scales},
tle = {Proceedings of the ACL},
2005
age='http://www.cs.cornell.edu/people/pabo/movie-review-data/', license='', features={'text': Value(dtype='string', id=None), 'label': ClassLabel(num_classes=2, names=['neg', 'pos'], id=None)}, post_processed=None, supervised_keys=SupervisedKeysData(input='', output=''), task_templates=[TextClassification(task='text-classification', text_column='text', label_column='label')], builder_name='rotten_tomatoes_movie_review', config_name='default', version=1.0.0, splits={'train': SplitInfo(name='train', num_bytes=1074810, num_examples=8530, dataset_name='rotten_tomatoes_movie_review'), 'validation': SplitInfo(name='validation', num_bytes=134679, num_examples=1066, dataset_name='rotten_tomatoes_movie_review'), 'test': SplitInfo(name='test', num_bytes=135972, num_examples=1066, dataset_name='rotten_tomatoes_movie_review')}, download_checksums={'https://storage.googleapis.com/seldon-datasets/sentence_polarity_v1/rt-polaritydata.tar.gz': {'num_bytes': 487770, 'checksum': 'a05befe52aafda71d458d188a1c54506a998b1308613ba76bbda2e5029409ce9'}}, download_size=487770, post_processing_size=None, dataset_size=1345461, size_in_bytes=1833231)
Return the path of the module of this class or subclass.
Base class for datasets with data generation based on dict generators.
GeneratorBasedBuilder
is a convenience class that abstracts away much
of the data writing and reading of DatasetBuilder
. It expects subclasses to
implement generators of feature dictionaries across the dataset splits
(_split_generators
). See the method docstrings for details.
( *args beam_runner = None beam_options = None **kwargs )
Beam based Builder.
( cache_dir: typing.Optional[str] = None config_name: typing.Optional[str] = None hash: typing.Optional[str] = None base_path: typing.Optional[str] = None info: typing.Optional[datasets.info.DatasetInfo] = None features: typing.Optional[datasets.features.features.Features] = None use_auth_token: typing.Union[str, bool, NoneType] = None repo_id: typing.Optional[str] = None data_files: typing.Union[str, list, dict, datasets.data_files.DataFilesDict, NoneType] = None data_dir: typing.Optional[str] = None name = 'deprecated' **config_kwargs )
Base class for datasets with data generation based on Arrow loading functions (CSV/JSON/Parquet).
( name: str = 'default' version: typing.Union[str, datasets.utils.version.Version, NoneType] = 0.0.0 data_dir: typing.Optional[str] = None data_files: typing.Optional[datasets.data_files.DataFilesDict] = None description: typing.Optional[str] = None )
Parameters
str
, default "default"
) —
str
, optional) —
str
, optional) —
str
or Sequence
or Mapping
, optional) — Path(s) to source data file(s).
str
, optional) —
Base class for DatasetBuilder data configuration.
DatasetBuilder subclasses with data configuration options should subclass BuilderConfig and add their own properties.
( config_kwargs: dict custom_features: typing.Optional[datasets.features.features.Features] = None )
The config id is used to build the cache directory. By default it is equal to the config name. However the name of a config is not sufficient to have a unique identifier for the dataset being generated since it doesn’t take into account:
( dataset_name: typing.Optional[str] = None data_dir: typing.Optional[str] = None download_config: typing.Optional[datasets.download.download_config.DownloadConfig] = None base_path: typing.Optional[str] = None record_checksums = True )
(
url_or_urls
)
→
str
or list
or dict
Download given URL(s).
By default, if there is more than one URL to download, multiprocessing is used with maximum num_proc = 16
.
Pass customized download_config.num_proc
to change this behavior.
( url_or_urls ) → extracted_path(s)
Returns
extracted_path(s)
str
, extracted paths of given URL(s).
Download and extract given url_or_urls.
( url_or_urls custom_download ) → downloaded_path(s)
Returns
downloaded_path(s)
str
, The downloaded paths matching the given input
url_or_urls.
Download given urls(s) by calling custom_download
.
( path_or_paths num_proc = None ) → extracted_path(s)
Returns
extracted_path(s)
str
, The extracted paths matching the given input
path_or_paths.
Extract given path(s).
(
path_or_buf: typing.Union[str, _io.BufferedReader]
)
→
tuple
[str`, `io.BufferedReader
]
Iterate over files within an archive.
( paths: typing.Union[str, typing.List[str]] ) → str
Iterate over file paths.
( downloaded_path_or_paths pipeline )
Ship the files using Beam FileSystems to the pipeline temp dir.
( dataset_name: typing.Optional[str] = None data_dir: typing.Optional[str] = None download_config: typing.Optional[datasets.download.download_config.DownloadConfig] = None base_path: typing.Optional[str] = None )
Download manager that uses the ”::” separator to navigate through (possibly remote) compressed archives.
Contrary to the regular DownloadManager, the download
and extract
methods don’t actually download nor extract
data, but they rather return the path or url that could be opened using the xopen
function which extends the
builtin open
function to stream data from remote files.
(
url_or_urls
)
→
str
Download given url(s).
( url_or_urls ) → extracted_path(s)
Returns
extracted_path(s)
str
, extracted paths of given URL(s).
Download and extract given url_or_urls.
(
path_or_paths
)
→
str
Extract given path(s).
(
urlpath_or_buf: typing.Union[str, _io.BufferedReader]
)
→
tuple
[str`, `io.BufferedReader
]
Iterate over files within an archive.
( urlpaths: typing.Union[str, typing.List[str]] ) → str
Iterate over files.
( cache_dir: typing.Union[str, pathlib.Path, NoneType] = None force_download: bool = False resume_download: bool = False local_files_only: bool = False proxies: typing.Optional[typing.Dict] = None user_agent: typing.Optional[str] = None extract_compressed_file: bool = False force_extract: bool = False delete_extracted: bool = False use_etag: bool = True num_proc: typing.Optional[int] = None max_retries: int = 1 use_auth_token: typing.Union[bool, str, NoneType] = None ignore_url_params: bool = False download_desc: typing.Optional[str] = None )
Parameters
str
or Path
, optional) — Specify a cache directory to save the file to (overwrite the
default cache dir).
bool
, default False
) — If True, re-dowload the file even if it’s already cached in
the cache dir.
bool
, default False
) — If True, resume the download if incompletly recieved file is
found.
dict
, optional) —
str
, optional) — Optional string or dict that will be appended to the user-agent on remote
requests.
bool
, default False
) — If True and the path point to a zip or tar file,
extract the compressed file in a folder along the archive.
bool
, default False
) — If True when extract_compressed_file is True and the archive
was already extracted, re-extract the archive and override the folder where it was extracted.
bool
, default False
) — Whether to delete (or keep) the extracted files.
bool
, default True
) — Whether to use the ETag HTTP response header to validate the cached files.
int
, optional) — The number of processes to launch to download the files in parallel.
int
, default 1
) — The number of times to retry an HTTP request if it fails.
str
or bool
, optional) — Optional string or boolean to use as Bearer token
for remote files on the Datasets Hub. If True, will get token from ~/.huggingface.
bool
, default False
) — Whether to strip all query parameters and #fragments from
the download URL before using it for caching the file.
str
, optional) — A description to be displayed alongside with the progress bar while downloading the files.
Configuration for our cached path manager.
( value names = None module = None qualname = None type = None start = 1 )
Enum
for how to treat pre-existing downloads and data.
The default mode is REUSE_DATASET_IF_EXISTS
, which will reuse both
raw downloads and the prepared dataset if they exist.
The generations modes:
Downloads | Dataset | |
---|---|---|
REUSE_DATASET_IF_EXISTS (default) |
Reuse | Reuse |
REUSE_CACHE_IF_EXISTS |
Reuse | Fresh |
FORCE_REDOWNLOAD |
Fresh | Fresh |
( name: str gen_kwargs: typing.Dict = <factory> )
Defines the split information for the generator.
This should be used as returned value of
GeneratorBasedBuilder._split_generators()
.
See GeneratorBasedBuilder._split_generators()
for more info and example
of usage.
Enum
for dataset splits.
Datasets are typically split into different subsets to be used at various stages of training and evaluation.
TRAIN
: the training data.VALIDATION
: the validation data. If present, this is typically used as
evaluation data while iterating on a model (e.g. changing hyperparameters,
model architecture, etc.).TEST
: the testing data. This is the data to report metrics on. Typically
you do not want to use this during model iteration as you may overfit to it.ALL
: the union of all defined dataset splits.Note: All splits, including compositions inherit from datasets.SplitBase
See the :doc:guide on splits </loading>
for more information.
Example:
>>> datasets.SplitGenerator(
... name=datasets.Split.TRAIN,
... gen_kwargs={"split_key": "train", "files": dl_manager.download_and extract(url)},
... ),
... datasets.SplitGenerator(
... name=datasets.Split.VALIDATION,
... gen_kwargs={"split_key": "validation", "files": dl_manager.download_and extract(url)},
... ),
... datasets.SplitGenerator(
... name=datasets.Split.TEST,
... gen_kwargs={"split_key": "test", "files": dl_manager.download_and extract(url)},
... )
Descriptor corresponding to a named split (train, test, …).
Example:
Each descriptor can be composed with other using addition or slice. Ex
split = datasets.Split.TRAIN.subsplit(datasets.percent[0:25]) + datasets.Split.TEST
The resulting split will correspond to 25% of the train split merged with
100% of the test split.
Warning:
A split cannot be added twice, so the following will fail:
split = (
datasets.Split.TRAIN.subsplit(datasets.percent[:25]) +
datasets.Split.TRAIN.subsplit(datasets.percent[75:])
) # Error
split = datasets.Split.TEST + datasets.Split.ALL # Error
Warning:
Split corresponding to the union of all defined dataset splits.
( split_name rounding = None from_ = None to = None unit = None )
Reading instruction for a dataset.
Examples:
# The following lines are equivalent:
ds = datasets.load_dataset('mnist', split='test[:33%]')
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction.from_spec('test[:33%]'))
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction('test', to=33, unit='%'))
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction(
'test', from_=0, to=33, unit='%'))
# The following lines are equivalent:
ds = datasets.load_dataset('mnist', split='test[:33%]+train[1:-1]')
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction.from_spec(
'test[:33%]+train[1:-1]'))
ds = datasets.load_dataset('mnist', split=(
datasets.ReadInstruction('test', to=33, unit='%') +
datasets.ReadInstruction('train', from_=1, to=-1, unit='abs')))
# The following lines are equivalent:
ds = datasets.load_dataset('mnist', split='test[:33%](pct1_dropremainder)')
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction.from_spec(
'test[:33%](pct1_dropremainder)'))
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction(
'test', from_=0, to=33, unit='%', rounding="pct1_dropremainder"))
# 10-fold validation:
tests = datasets.load_dataset(
'mnist',
[datasets.ReadInstruction('train', from_=k, to=k+10, unit='%')
for k in range(0, 100, 10)])
trains = datasets.load_dataset(
'mnist',
[datasets.ReadInstruction('train', to=k, unit='%') + datasets.ReadInstruction('train', from_=k+10, unit='%')
for k in range(0, 100, 10)])
( spec )
Parameters
test
: test split.
test + validation
: test split + validation split.
test[10:]
: test split, minus its first 10 records.
test[:10%]
: first 10% records of test split.
test[:20%](pct1_dropremainder)
: first 10% records, rounded with
the pct1_dropremainder
rounding.
test[:-5%]+train[40%:60%]
: first 95% of test + middle 20% of
train.
Creates a ReadInstruction instance out of a string spec.
Translate instruction into a list of absolute instructions.
Those absolute instructions are then to be added together.
( version_str: str description: typing.Optional[str] = None major: typing.Union[str, int, NoneType] = None minor: typing.Union[str, int, NoneType] = None patch: typing.Union[str, int, NoneType] = None )
Dataset version MAJOR.MINOR.PATCH.
Returns True if other_version matches.