Concatenate values along an axis across batches. The function `streaming_concat` creates two local variables, `array` and `size`, that are used to store concatenated values. Internally, `array` is used as storage for a dynamic array (if `maxsize` is `None`), which ensures that updates can b
(values,
axis=0,
max_size=None,
metrics_collections=None,
updates_collections=None,
name=None)
| 3556 | |
| 3557 | |
| 3558 | def streaming_concat(values, |
| 3559 | axis=0, |
| 3560 | max_size=None, |
| 3561 | metrics_collections=None, |
| 3562 | updates_collections=None, |
| 3563 | name=None): |
| 3564 | """Concatenate values along an axis across batches. |
| 3565 | |
| 3566 | The function `streaming_concat` creates two local variables, `array` and |
| 3567 | `size`, that are used to store concatenated values. Internally, `array` is |
| 3568 | used as storage for a dynamic array (if `maxsize` is `None`), which ensures |
| 3569 | that updates can be run in amortized constant time. |
| 3570 | |
| 3571 | For estimation of the metric over a stream of data, the function creates an |
| 3572 | `update_op` operation that appends the values of a tensor and returns the |
| 3573 | length of the concatenated axis. |
| 3574 | |
| 3575 | This op allows for evaluating metrics that cannot be updated incrementally |
| 3576 | using the same framework as other streaming metrics. |
| 3577 | |
| 3578 | Args: |
| 3579 | values: `Tensor` to concatenate. Rank and the shape along all axes other |
| 3580 | than the axis to concatenate along must be statically known. |
| 3581 | axis: optional integer axis to concatenate along. |
| 3582 | max_size: optional integer maximum size of `value` along the given axis. |
| 3583 | Once the maximum size is reached, further updates are no-ops. By default, |
| 3584 | there is no maximum size: the array is resized as necessary. |
| 3585 | metrics_collections: An optional list of collections that `value` should be |
| 3586 | added to. |
| 3587 | updates_collections: An optional list of collections `update_op` should be |
| 3588 | added to. |
| 3589 | name: An optional variable_scope name. |
| 3590 | |
| 3591 | Returns: |
| 3592 | value: A `Tensor` representing the concatenated values. |
| 3593 | update_op: An operation that concatenates the next values. |
| 3594 | |
| 3595 | Raises: |
| 3596 | ValueError: if `values` does not have a statically known rank, `axis` is |
| 3597 | not in the valid range or the size of `values` is not statically known |
| 3598 | along any axis other than `axis`. |
| 3599 | """ |
| 3600 | with variable_scope.variable_scope(name, 'streaming_concat', (values,)): |
| 3601 | # pylint: disable=invalid-slice-index |
| 3602 | values_shape = values.get_shape() |
| 3603 | if values_shape.dims is None: |
| 3604 | raise ValueError('`values` must have known statically known rank') |
| 3605 | |
| 3606 | ndim = len(values_shape) |
| 3607 | if axis < 0: |
| 3608 | axis += ndim |
| 3609 | if not 0 <= axis < ndim: |
| 3610 | raise ValueError('axis = %r not in [0, %r)' % (axis, ndim)) |
| 3611 | |
| 3612 | fixed_shape = [dim.value for n, dim in enumerate(values_shape) if n != axis] |
| 3613 | if any(value is None for value in fixed_shape): |
| 3614 | raise ValueError('all dimensions of `values` other than the dimension to ' |
| 3615 | 'concatenate along must have statically known size') |
no test coverage detected