Reduces the input dataset to a single element. The transformation calls `reduce_func` successively on every element of the input dataset until the dataset is exhausted, aggregating information in its internal state. The `initial_state` argument is used for the initial state and the
(self, initial_state, reduce_func)
| 1478 | return WindowDataset(self, size, shift, stride, drop_remainder) |
| 1479 | |
| 1480 | def reduce(self, initial_state, reduce_func): |
| 1481 | """Reduces the input dataset to a single element. |
| 1482 | |
| 1483 | The transformation calls `reduce_func` successively on every element of |
| 1484 | the input dataset until the dataset is exhausted, aggregating information in |
| 1485 | its internal state. The `initial_state` argument is used for the initial |
| 1486 | state and the final state is returned as the result. |
| 1487 | |
| 1488 | For example: |
| 1489 | - `tf.data.Dataset.range(5).reduce(np.int64(0), lambda x, _: x + 1)` |
| 1490 | produces `5` |
| 1491 | - `tf.data.Dataset.range(5).reduce(np.int64(0), lambda x, y: x + y)` |
| 1492 | produces `10` |
| 1493 | |
| 1494 | Args: |
| 1495 | initial_state: An element representing the initial state of the |
| 1496 | transformation. |
| 1497 | reduce_func: A function that maps `(old_state, input_element)` to |
| 1498 | `new_state`. It must take two arguments and return a new element |
| 1499 | The structure of `new_state` must match the structure of |
| 1500 | `initial_state`. |
| 1501 | |
| 1502 | Returns: |
| 1503 | A dataset element corresponding to the final state of the transformation. |
| 1504 | |
| 1505 | """ |
| 1506 | |
| 1507 | with ops.name_scope("initial_state"): |
| 1508 | initial_state = structure.normalize_element(initial_state) |
| 1509 | state_structure = structure.type_spec_from_value(initial_state) |
| 1510 | |
| 1511 | # Iteratively rerun the reduce function until reaching a fixed point on |
| 1512 | # `state_structure`. |
| 1513 | need_to_rerun = True |
| 1514 | while need_to_rerun: |
| 1515 | |
| 1516 | wrapped_func = StructuredFunctionWrapper( |
| 1517 | reduce_func, |
| 1518 | "reduce()", |
| 1519 | input_structure=(state_structure, self.element_spec), |
| 1520 | add_to_graph=False) |
| 1521 | |
| 1522 | # Extract and validate class information from the returned values. |
| 1523 | output_classes = wrapped_func.output_classes |
| 1524 | state_classes = nest.map_structure( |
| 1525 | lambda component_spec: component_spec._to_legacy_output_classes(), # pylint: disable=protected-access |
| 1526 | state_structure) |
| 1527 | for new_state_class, state_class in zip( |
| 1528 | nest.flatten(output_classes), nest.flatten(state_classes)): |
| 1529 | if not issubclass(new_state_class, state_class): |
| 1530 | raise TypeError( |
| 1531 | "The element classes for the new state must match the initial " |
| 1532 | "state. Expected %s; got %s." % |
| 1533 | (state_classes, wrapped_func.output_classes)) |
| 1534 | |
| 1535 | # Extract and validate type information from the returned values. |
| 1536 | output_types = wrapped_func.output_types |
| 1537 | state_types = nest.map_structure( |