More rigorous shape inference in to_tf_dataset
{'login': 'Rocketknight1', 'id': 12866554, 'node_id': 'MDQ6VXNlcjEyODY2NTU0', 'avatar_url': 'https://avatars.githubusercontent.com/u/12866554?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/Rocketknight1', 'html_url': 'https://github.com/Rocketknight1', 'followers_url': 'https://api.github.com/users/Rocketknight1/followers', 'following_url': 'https://api.github.com/users/Rocketknight1/following{/other_user}', 'gists_url': 'https://api.github.com/users/Rocketknight1/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/Rocketknight1/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/Rocketknight1/subscriptions', 'organizations_url': 'https://api.github.com/users/Rocketknight1/orgs', 'repos_url': 'https://api.github.com/users/Rocketknight1/repos', 'events_url': 'https://api.github.com/users/Rocketknight1/events{/privacy}', 'received_events_url': 'https://api.github.com/users/Rocketknight1/received_events', 'type': 'User', 'site_admin': False}
[]
[]
_The documentation is not available anymore as the PR was closed or merged._
`tf.data` needs to know the shape of tensors emitted from a `tf.data.Dataset`. Although `None` dimensions are possible, overusing them can cause problems - Keras uses the dataset tensor spec at compile-time, and so saying that a dimension is `None` when it's actually constant can hurt performance, or even cause training to fail for dimensions that are needed to determine the shape of weight tensors!

The compromise I used here was to sample several batches from the underlying dataset and apply the `collate_fn` to them, and then to see which dimensions were "empirically variable". There's an obvious problem here, though - if you sample 10 batches and they all have the same shape on a certain dimension, there's still a small chance that the 11th batch will be different, and Keras will throw an error  if a dataset tries to emit a tensor whose shape doesn't match the spec.

I encountered this bug in practice once or twice for datasets that were mostly-but-not-totally constant on a given dimension, and I still don't have a perfect solution, but this PR should greatly reduce the risk. It samples many more batches, and also samples very small batches (size 2) - this increases the variability, making it more likely that a few outlier samples will be detected.

Ideally, of course, we'd determine the full output shape analytically, but that's surprisingly tricky when the `collate_fn` can be any arbitrary Python code!
{'url': 'https://api.github.com/repos/huggingface/datasets/issues/4763/reactions', 'total_count': 0, '+1': 0, '-1': 0, 'laugh': 0, 'hooray': 0, 'confused': 0, 'heart': 0, 'rocket': 0, 'eyes': 0}
{'url': 'https://api.github.com/repos/huggingface/datasets/pulls/4763', 'html_url': 'https://github.com/huggingface/datasets/pull/4763', 'diff_url': 'https://github.com/huggingface/datasets/pull/4763.diff', 'patch_url': 'https://github.com/huggingface/datasets/pull/4763.patch', 'merged_at': '2022-09-08T19:15:41Z'}