Creates a `Dataset` that includes only 1/`num_shards` of this dataset. This dataset operator is very useful when running distributed training, as it allows each worker to read a unique subset. When reading a single input file, you can skip elements as follows: ```python d = tf
(self, num_shards, index)
| 1015 | return SkipDataset(self, count) |
| 1016 | |
| 1017 | def shard(self, num_shards, index): |
| 1018 | """Creates a `Dataset` that includes only 1/`num_shards` of this dataset. |
| 1019 | |
| 1020 | This dataset operator is very useful when running distributed training, as |
| 1021 | it allows each worker to read a unique subset. |
| 1022 | |
| 1023 | When reading a single input file, you can skip elements as follows: |
| 1024 | |
| 1025 | ```python |
| 1026 | d = tf.data.TFRecordDataset(input_file) |
| 1027 | d = d.shard(num_workers, worker_index) |
| 1028 | d = d.repeat(num_epochs) |
| 1029 | d = d.shuffle(shuffle_buffer_size) |
| 1030 | d = d.map(parser_fn, num_parallel_calls=num_map_threads) |
| 1031 | ``` |
| 1032 | |
| 1033 | Important caveats: |
| 1034 | |
| 1035 | - Be sure to shard before you use any randomizing operator (such as |
| 1036 | shuffle). |
| 1037 | - Generally it is best if the shard operator is used early in the dataset |
| 1038 | pipeline. For example, when reading from a set of TFRecord files, shard |
| 1039 | before converting the dataset to input samples. This avoids reading every |
| 1040 | file on every worker. The following is an example of an efficient |
| 1041 | sharding strategy within a complete pipeline: |
| 1042 | |
| 1043 | ```python |
| 1044 | d = Dataset.list_files(pattern) |
| 1045 | d = d.shard(num_workers, worker_index) |
| 1046 | d = d.repeat(num_epochs) |
| 1047 | d = d.shuffle(shuffle_buffer_size) |
| 1048 | d = d.interleave(tf.data.TFRecordDataset, |
| 1049 | cycle_length=num_readers, block_length=1) |
| 1050 | d = d.map(parser_fn, num_parallel_calls=num_map_threads) |
| 1051 | ``` |
| 1052 | |
| 1053 | Args: |
| 1054 | num_shards: A `tf.int64` scalar `tf.Tensor`, representing the number of |
| 1055 | shards operating in parallel. |
| 1056 | index: A `tf.int64` scalar `tf.Tensor`, representing the worker index. |
| 1057 | |
| 1058 | Returns: |
| 1059 | Dataset: A `Dataset`. |
| 1060 | |
| 1061 | Raises: |
| 1062 | InvalidArgumentError: if `num_shards` or `index` are illegal values. |
| 1063 | Note: error checking is done on a best-effort basis, and errors aren't |
| 1064 | guaranteed to be caught upon dataset creation. (e.g. providing in a |
| 1065 | placeholder tensor bypasses the early checking, and will instead result |
| 1066 | in an error during a session.run call.) |
| 1067 | """ |
| 1068 | return ShardDataset(self, num_shards, index) |
| 1069 | |
| 1070 | def batch(self, batch_size, drop_remainder=False): |
| 1071 | """Combines consecutive elements of this dataset into batches. |