def per_sentence_accuracy(targets, predictions, weights=None):
"""Computes the per-sentence accuracy.
Given a set of ground truth values and a set of predicted labels as tensors of
the same shape, it returns a tensor of rank equal to the ground truth values
tensor minus 1, with values 1.0 if all the predicted values along the -1 axis
are correct, 0.0 otherwise. So, if the grount truth is [[1, 2, 3], [0, 9, 23]]
and the predicted labels are [[1, 2, 3], [9, 0, 23]] the result will be: [1,0].
Arguments:
target: the gold truth values `Tensor`, with `tf.int32` as `dtype`. It has rank
`[d_0, d_1, ..., d_{r-1}]` and the last value is supposed to range between
`0` and `num_classes - 1`, where `num_classes` is the number of possible classes.
predictions: the predicted values `Tensor` with `tf.float32` as `dtype`. It can
have shape `[d_0, d_1, ..., d_{r-1}, num_classes]` and dtype `float32` and
represents the probability distribution across the output classes generated by
the model -- so that the predicted label is the one coming from argmax over the
last dimension. Alternatively it can be of the same shape, `dtype` and format of
`target`, and it will considered as the predicted labels.
weights: coefficients for the metric. This must be scalar or of same rank as `target`.
Returns:
values: a `Tensor` of `dtype=tf.float32` and of [d_0, d_1, ..., d_{r-2}]
representing the accuracy per sentence, i.e. for all the elements of the
-1 axis, weighted according to the input argument `weights`
weights: a `Tensor` of `dtype=tf.float32` and of the same shape of `values`
representing the weighted scheme for the streaming average on `values`, which
is the same tensor of the input `weights` argument.
"""
values, weights = accuracy(targets, predictions, weights)
values = tf.cast(values, tf.bool)
if weights is not None:
weights = tf.cast(weights, tf.bool)
values = ops.logical_impl(weights, values)
values = tf.reduce_all(values, axis=-1)
return tf.cast(values, tf.float32), tf.cast(tf.ones_like(values), tf.float32)
评论列表
文章目录