<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/core/load.py#L723-L840) |

Gets a data source from the named dataset.

    tfds.data_source(
        name: str,
        *,
        split: (Tree[splits_lib.SplitArg] | None) = None,
        data_dir: (epath.PathLike | None) = None,
        download: bool = True,
        decoders: (TreeDict[decode.partial_decode.DecoderArg] | None) = None,
        deserialize_method: decode.DeserializeMethod = https://www.tensorflow.org/datasets/api_docs/python/tfds/decode/DeserializeMethod#DESERIALIZE_AND_DECODE,
        builder_kwargs: (dict[str, Any] | None) = None,
        download_and_prepare_kwargs: (dict[str, Any] | None) = None,
        try_gcs: bool = False
    ) -> type_utils.ListOrTreeOrElem[Sequence[Any]]

### Used in the notebooks

| Used in the tutorials |
|---|
| - [TFDS for Jax and PyTorch](https://www.tensorflow.org/datasets/data_source) |

[`tfds.data_source`](https://www.tensorflow.org/datasets/api_docs/python/tfds/data_source) is a convenience method that:

1. Fetches the [`tfds.core.DatasetBuilder`](https://www.tensorflow.org/datasets/api_docs/python/tfds/core/DatasetBuilder) by name:

       builder = tfds.builder(name, data_dir=data_dir, **builder_kwargs)

2. Generates the data (when `download=True`):

       builder.download_and_prepare(**download_and_prepare_kwargs)

3. Gets the data source:

       ds = builder.as_data_source(split=split)

You can consume data sources:

- In Python by iterating over them:

    for example in ds['train']:
      print(example)

- With a DataLoader (e.g., with [Pytorch](https://pytorch.org/docs/stable/data.html)).

> [!WARNING]
> **Warning:** calling this function might potentially trigger the download of hundreds of GiB to disk. Refer to the `download` argument.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `name` | `str`, the registered name of the `DatasetBuilder` (the snake case version of the class name). The config and version can also be specified in the name as follows: `'dataset_name[/config_name][:version]'`. For example, `'movielens/25m-ratings'` (for the latest version of `'25m-ratings'`), `'movielens:0.1.0'` (for the default config and version 0.1.0), or`'movielens/25m-ratings:0.1.0'`. Note that only the latest version can be generated, but old versions can be read if they are present on disk. For convenience, the `name` parameter can contain comma-separated keyword arguments for the builder. For example, `'foo_bar/a=True,b=3'` would use the `FooBar` dataset passing the keyword arguments `a=True` and `b=3` (for builders with configs, it would be `'foo_bar/zoo/a=True,b=3'` to use the `'zoo'` config and pass to the builder keyword arguments `a=True` and `b=3`). |
| `split` | Which split of the data to load (e.g. `'train'`, `'test'`, `['train', 'test']`, `'train[80%:]'`,...). See our [split API guide](https://www.tensorflow.org/datasets/splits). If `None`, will return all splits in a `dict[Split, Sequence]` |
| `data_dir` | directory to read/write data. Defaults to the value of the environment variable TFDS_DATA_DIR, if set, otherwise falls back to '\~/tensorflow_datasets'. |
| `download` | `bool` (optional), whether to call [`tfds.core.DatasetBuilder.download_and_prepare`](https://www.tensorflow.org/datasets/api_docs/python/tfds/core/DatasetBuilder#download_and_prepare) before calling [`tfds.core.DatasetBuilder.as_data_source`](https://www.tensorflow.org/datasets/api_docs/python/tfds/core/DatasetBuilder#as_data_source). If `False`, data is expected to be in `data_dir`. If `True` and the data is already in `data_dir`, `download_and_prepare` is a no-op. |
| `decoders` | Nested dict of `Decoder` objects which allow to customize the decoding. The structure should match the feature structure, but only customized feature keys need to be present. See [the guide](https://github.com/tensorflow/datasets/blob/master/docs/decode.md) for more info. |
| `deserialize_method` | Whether the read examples should be deserialized and/or decoded. If not specified, it'll deserialize the data and decode the features. Decoding is only supported if the examples are tf examples. Note that if the parse method is other than PARSE_AND_DECODE, then the `decoders` argument is ignored. |
| `builder_kwargs` | `dict` (optional), keyword arguments to be passed to the [`tfds.core.DatasetBuilder`](https://www.tensorflow.org/datasets/api_docs/python/tfds/core/DatasetBuilder) constructor. `data_dir` will be passed through by default. |
| `download_and_prepare_kwargs` | `dict` (optional) keyword arguments passed to [`tfds.core.DatasetBuilder.download_and_prepare`](https://www.tensorflow.org/datasets/api_docs/python/tfds/core/DatasetBuilder#download_and_prepare) if `download=True`. Allow to control where to download and extract the cached data. If not set, cache_dir and manual_dir will automatically be deduced from data_dir. |
| `try_gcs` | `bool`, if True, [`tfds.load`](https://www.tensorflow.org/datasets/api_docs/python/tfds/load) will see if the dataset exists on the public GCS bucket before building it locally. This is equivalent to passing `data_dir='gs://tfds-data/datasets'`. Warning: `try_gcs` is different than `builder_kwargs.download_config.try_download_gcs`. `try_gcs` (default: False) overrides `data_dir` to be the public GCS bucket. `try_download_gcs` (default: True) allows downloading from GCS while keeping a different `data_dir` than the public GCS bucket. So, to fully bypass GCS, please use `try_gcs=False` and `download_and_prepare_kwargs={'download_config': tfds.core.download.DownloadConfig(try_download_gcs=False)})`. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| `Sequence` if `split`, `dict<key: tfds.Split, value: Sequence>` otherwise. ||

<br />