# tff.analytics.data_processing.get_capped_elements_with_counts

<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) |

Gets the capped elements with counts from the input dataset.

    tff.analytics.data_processing.get_capped_elements_with_counts(
        dataset: tf.data.Dataset,
        max_user_contribution: int,
        batch_size: int = 1,
        string_max_bytes: Optional[int] = None
    )

The input `dataset` must yield batched rank-1 tensors. This function reads
each coordinate of the tensor as an individual element and caps the total
number of elements to return. Note either none of the elements in one batch is
added to the returned result, or all the elements are added. This means the
length of the returned list of elements could be less than
`max_user_contribution` when `dataset` is capped.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `dataset` | A [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset). |
| `max_user_contribution` | The maximum number of elements to return. |
| `batch_size` | The number of elements in each batch of `dataset`. |
| `string_max_bytes` | The maximum length (in bytes) of strings in the dataset. Strings longer than `string_max_bytes` will be truncated. Defaults to `None`, which means there is no limit of the string length. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| `elements` | A rank-1 Tensor containing the unique elements of the input dataset after being capped. If the total number of elements is less than or equal to `max_user_contribution`, returns all the elements in `dataset`. |
| `counts` | A rank-1 Tensor containing the counts for each of the elements in `elements`. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | -- If the shape of elements in `dataset` is not rank 1. -- If `max_user_contribution` is less than 1. -- If `batch_size` is less than 1. -- If `string_max_bytes` is not `None` and is less than 1. |
| `TypeError` | If `dataset.element_spec.dtype` must be [`tf.string`](https://www.tensorflow.org/api_docs/python/tf#string) is not [`tf.string`](https://www.tensorflow.org/api_docs/python/tf#string). |

<br />