Computes the number of examples, or sum of `weights`. This metric keeps track of the denominator in `tf.compat.v1.metrics.mean`. When evaluating some metric (e.g. mean) on one or more subsets of the data, this auxiliary metric is useful for keeping track of how many examples there are in ea
(values,
weights=None,
metrics_collections=None,
updates_collections=None,
name=None)
| 3721 | |
| 3722 | |
| 3723 | def count(values, |
| 3724 | weights=None, |
| 3725 | metrics_collections=None, |
| 3726 | updates_collections=None, |
| 3727 | name=None): |
| 3728 | """Computes the number of examples, or sum of `weights`. |
| 3729 | |
| 3730 | This metric keeps track of the denominator in `tf.compat.v1.metrics.mean`. |
| 3731 | When evaluating some metric (e.g. mean) on one or more subsets of the data, |
| 3732 | this auxiliary metric is useful for keeping track of how many examples there |
| 3733 | are in each subset. |
| 3734 | |
| 3735 | If `weights` is `None`, weights default to 1. Use weights of 0 to mask values. |
| 3736 | |
| 3737 | Args: |
| 3738 | values: A `Tensor` of arbitrary dimensions. Only it's shape is used. |
| 3739 | weights: Optional `Tensor` whose rank is either 0, or the same rank as |
| 3740 | `labels`, and must be broadcastable to `labels` (i.e., all dimensions must |
| 3741 | be either `1`, or the same as the corresponding `labels` dimension). |
| 3742 | metrics_collections: An optional list of collections that the metric value |
| 3743 | variable should be added to. |
| 3744 | updates_collections: An optional list of collections that the metric update |
| 3745 | ops should be added to. |
| 3746 | name: An optional variable_scope name. |
| 3747 | |
| 3748 | Returns: |
| 3749 | count: A `Tensor` representing the current value of the metric. |
| 3750 | update_op: An operation that accumulates the metric from a batch of data. |
| 3751 | |
| 3752 | Raises: |
| 3753 | ValueError: If `weights` is not `None` and its shape doesn't match `values`, |
| 3754 | or if either `metrics_collections` or `updates_collections` are not a list |
| 3755 | or tuple. |
| 3756 | RuntimeError: If eager execution is enabled. |
| 3757 | """ |
| 3758 | if context.executing_eagerly(): |
| 3759 | raise RuntimeError('tf.contrib.metrics.count is not supported when eager ' |
| 3760 | 'execution is enabled.') |
| 3761 | |
| 3762 | with variable_scope.variable_scope(name, 'count', (values, weights)): |
| 3763 | |
| 3764 | count_ = metrics_impl.metric_variable([], dtypes.float32, name='count') |
| 3765 | |
| 3766 | if weights is None: |
| 3767 | num_values = math_ops.cast(array_ops.size(values), dtypes.float32) |
| 3768 | else: |
| 3769 | values = math_ops.cast(values, dtypes.float32) |
| 3770 | values, _, weights = metrics_impl._remove_squeezable_dimensions( # pylint: disable=protected-access |
| 3771 | predictions=values, |
| 3772 | labels=None, |
| 3773 | weights=weights) |
| 3774 | weights = weights_broadcast_ops.broadcast_weights( |
| 3775 | math_ops.cast(weights, dtypes.float32), values) |
| 3776 | num_values = math_ops.reduce_sum(weights) |
| 3777 | |
| 3778 | with ops.control_dependencies([values]): |
| 3779 | update_count_op = state_ops.assign_add(count_, num_values) |
| 3780 |