Computes the percentage of times that predictions matches labels. Args: predictions: the predicted values, a `Tensor` whose dtype and shape matches 'labels'. labels: the ground truth values, a `Tensor` of any shape and bool, integer, or string dtype. weigh
(predictions, labels, weights=None, name=None)
| 30 | |
| 31 | |
| 32 | def accuracy(predictions, labels, weights=None, name=None): |
| 33 | """Computes the percentage of times that predictions matches labels. |
| 34 | |
| 35 | Args: |
| 36 | predictions: the predicted values, a `Tensor` whose dtype and shape |
| 37 | matches 'labels'. |
| 38 | labels: the ground truth values, a `Tensor` of any shape and |
| 39 | bool, integer, or string dtype. |
| 40 | weights: None or `Tensor` of float values to reweight the accuracy. |
| 41 | name: A name for the operation (optional). |
| 42 | |
| 43 | Returns: |
| 44 | Accuracy `Tensor`. |
| 45 | |
| 46 | Raises: |
| 47 | ValueError: if dtypes don't match or |
| 48 | if dtype is not bool, integer, or string. |
| 49 | """ |
| 50 | if not (labels.dtype.is_integer or |
| 51 | labels.dtype in (dtypes.bool, dtypes.string)): |
| 52 | raise ValueError( |
| 53 | 'Labels should have bool, integer, or string dtype, not %r' % |
| 54 | labels.dtype) |
| 55 | if not labels.dtype.is_compatible_with(predictions.dtype): |
| 56 | raise ValueError('Dtypes of predictions and labels should match. ' |
| 57 | 'Given: predictions (%r) and labels (%r)' % |
| 58 | (predictions.dtype, labels.dtype)) |
| 59 | with ops.name_scope(name, 'accuracy', values=[predictions, labels]): |
| 60 | is_correct = math_ops.cast( |
| 61 | math_ops.equal(predictions, labels), dtypes.float32) |
| 62 | if weights is not None: |
| 63 | is_correct = math_ops.multiply(is_correct, weights) |
| 64 | num_values = math_ops.multiply(weights, array_ops.ones_like(is_correct)) |
| 65 | return math_ops.div(math_ops.reduce_sum(is_correct), |
| 66 | math_ops.reduce_sum(num_values)) |
| 67 | return math_ops.reduce_mean(is_correct) |
| 68 | |
| 69 | |
| 70 | def f1_score(labels, predictions, weights=None, num_thresholds=200, |
no test coverage detected