Maps `map_func` across this dataset, and interleaves the results. For example, you can use `Dataset.interleave()` to process many input files concurrently: ```python # Preprocess 4 files concurrently, and interleave blocks of 16 records from # each file. filenames = ["/var/
(self,
map_func,
cycle_length=AUTOTUNE,
block_length=1,
num_parallel_calls=None)
| 1293 | return FlatMapDataset(self, map_func) |
| 1294 | |
| 1295 | def interleave(self, |
| 1296 | map_func, |
| 1297 | cycle_length=AUTOTUNE, |
| 1298 | block_length=1, |
| 1299 | num_parallel_calls=None): |
| 1300 | """Maps `map_func` across this dataset, and interleaves the results. |
| 1301 | |
| 1302 | For example, you can use `Dataset.interleave()` to process many input files |
| 1303 | concurrently: |
| 1304 | |
| 1305 | ```python |
| 1306 | # Preprocess 4 files concurrently, and interleave blocks of 16 records from |
| 1307 | # each file. |
| 1308 | filenames = ["/var/data/file1.txt", "/var/data/file2.txt", ...] |
| 1309 | dataset = (Dataset.from_tensor_slices(filenames) |
| 1310 | .interleave(lambda x: |
| 1311 | TextLineDataset(x).map(parse_fn, num_parallel_calls=1), |
| 1312 | cycle_length=4, block_length=16)) |
| 1313 | ``` |
| 1314 | |
| 1315 | The `cycle_length` and `block_length` arguments control the order in which |
| 1316 | elements are produced. `cycle_length` controls the number of input elements |
| 1317 | that are processed concurrently. If you set `cycle_length` to 1, this |
| 1318 | transformation will handle one input element at a time, and will produce |
| 1319 | identical results to `tf.data.Dataset.flat_map`. In general, |
| 1320 | this transformation will apply `map_func` to `cycle_length` input elements, |
| 1321 | open iterators on the returned `Dataset` objects, and cycle through them |
| 1322 | producing `block_length` consecutive elements from each iterator, and |
| 1323 | consuming the next input element each time it reaches the end of an |
| 1324 | iterator. |
| 1325 | |
| 1326 | For example: |
| 1327 | |
| 1328 | ```python |
| 1329 | a = Dataset.range(1, 6) # ==> [ 1, 2, 3, 4, 5 ] |
| 1330 | |
| 1331 | # NOTE: New lines indicate "block" boundaries. |
| 1332 | a.interleave(lambda x: Dataset.from_tensors(x).repeat(6), |
| 1333 | cycle_length=2, block_length=4) # ==> [1, 1, 1, 1, |
| 1334 | # 2, 2, 2, 2, |
| 1335 | # 1, 1, |
| 1336 | # 2, 2, |
| 1337 | # 3, 3, 3, 3, |
| 1338 | # 4, 4, 4, 4, |
| 1339 | # 3, 3, |
| 1340 | # 4, 4, |
| 1341 | # 5, 5, 5, 5, |
| 1342 | # 5, 5] |
| 1343 | ``` |
| 1344 | |
| 1345 | NOTE: The order of elements yielded by this transformation is |
| 1346 | deterministic, as long as `map_func` is a pure function. If |
| 1347 | `map_func` contains any stateful operations, the order in which |
| 1348 | that state is accessed is undefined. |
| 1349 | |
| 1350 | Args: |
| 1351 | map_func: A function mapping a dataset element to a dataset. |
| 1352 | cycle_length: (Optional.) The number of input elements that will be |