# tff.simulation.baselines.BaselineTaskDatasets

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/federated/blob/v0.87.0 Version 2.0, January 2004 Licensed under the Apache License, Version 2.0 (the) |

A convenience class for a task's data and preprocessing logic.

    tff.simulation.baselines.BaselineTaskDatasets(
        train_data: https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData,
        test_data: https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData,
        validation_data: Optional[https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData] = None,
        train_preprocess_fn: Optional[https://www.tensorflow.org/federated/api_docs/python/tff/Computation] = None,
        eval_preprocess_fn: Optional[https://www.tensorflow.org/federated/api_docs/python/tff/Computation] = None
    )

<br />

<br />

<br />

| ## Args ||
|---|---|
| `train_data` | A [`tff.simulation.datasets.ClientData`](https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData) for training. |
| `test_data` | A [`tff.simulation.datasets.ClientData`](https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData) or a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) for computing test metrics. |
| `validation_data` | An optional [`tff.simulation.datasets.ClientData`](https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData) or a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) for computing validation metrics. |
| `train_preprocess_fn` | An optional callable accepting and returning a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset), used to perform dataset preprocessing for training. If set to `None`, we use the identity map for all train preprocessing. |
| `eval_preprocess_fn` | An optional callable accepting and returning a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset), used to perform evaluation (eg. validation, testing) preprocessing. If `None`, evaluation preprocessing will be done via the identity map. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If `train_data` and `test_data` have different element types after preprocessing with `train_preprocess_fn` and `eval_preprocess_fn`, or if `validation_data` is not `None` and has a different element type than the test data. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `train_data` | A [`tff.simulation.datasets.ClientData`](https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData) for training. |
| `test_data` | The test data for the baseline task. Can be a [`tff.simulation.datasets.ClientData`](https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData) or a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset). |
| `validation_data` | The validation data for the baseline task. Can be one of [`tff.simulation.datasets.ClientData`](https://www.tensorflow.org/federated/api_docs/python/tff/simulation/datasets/ClientData), [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset), or `None` if the task does not have a validation dataset. |
| `train_preprocess_fn` | A callable mapping accepting and return [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) instances, used for preprocessing train datasets. Set to `None` if no train preprocessing occurs for the task. |
| `eval_preprocess_fn` | A callable mapping accepting and return [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) instances, used for preprocessing evaluation datasets. Set to `None` if no eval preprocessing occurs for the task. |
| `element_type_structure` | A nested structure of [`tf.TensorSpec`](https://www.tensorflow.org/api_docs/python/tf/TensorSpec) objects defining the type of the elements contained in datasets associated to this task. |

<br />

## Methods

### `get_centralized_test_data`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    get_centralized_test_data() -> tf.data.Dataset

Returns a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) of test data for the task.

If the baseline task has centralized data, then this method will return
the centralized data after applying preprocessing. If the test data is
federated, then this method will first amalgamate the client datasets into
a single dataset, then apply preprocessing.

### `sample_train_clients`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    sample_train_clients(
        num_clients: int, replace: bool = False, random_seed: Optional[int] = None
    ) -> list[tf.data.Dataset]

Samples training clients uniformly at random.

<br />

<br />

<br />

| Args ||
|---|---|
| `num_clients` | A positive integer representing number of clients to be sampled. |
| `replace` | Whether to sample with replacement. If set to `False`, then `num_clients` cannot exceed the number of training clients in the associated train data. |
| `random_seed` | An optional integer used to set a random seed for sampling. If no random seed is passed or the random seed is set to `None`, this will attempt to set the random seed according to the current system time (see `numpy.random.RandomState` for details). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A list of [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) instances representing the client datasets. ||

<br />

### `summary`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    summary(
        print_fn: Callable[[str], Any] = print
    )

Prints a summary of the train, test, and validation data.

The summary will be printed as a table containing information on the type
of train, test, and validation data (ie. federated or centralized) and the
number of clients each data structure has (if it is federated). For example,
if the train data has 10 clients, and both the test and validation data are
centralized, then this will print the following table:

    Split      |Dataset Type |Number of Clients |
    =============================================
    Train      |Federated    |10                |
    Test       |Centralized  |N/A               |
    Validation |Centralized  |N/A               |
    _____________________________________________

In addition, this will print two lines after the table indicating whether
train and eval preprocessing functions were passed in. In the example above,
if we passed in a train preprocessing function but no eval preprocessing
function, it would also print the lines:

    Train Preprocess Function: True
    Eval Preprocess Function: False

To capture the summary, you can use a custom print function. For example,
setting `print_fn = summary_list.append` will cause each of the lines above
to be appended to `summary_list`.

<br />

<br />

<br />

| Args ||
|---|---|
| `print_fn` | An optional callable accepting string inputs. Used to print each row of the summary. Defaults to `print` if not specified. |

<br />