tf.metrics.accuracy
tf.metrics.accuracy(
labels,
predictions,
weights=None,
metrics_collections=None,
updates_collections=None,
name=None
)
'''
Args:
labels: The ground truth values, a Tensor whose shape matches predictions.
predictions: The predicted values, a Tensor of any shape.
weights: Optional Tensor whose rank is either 0, or the same rank as labels, and must be broadcastable to labels (i.e., all dimensions must be either 1, or the same as the corresponding labels dimension).
metrics_collections: An optional list of collections that accuracy should be added to.
updates_collections: An optional list of collections that update_op should be added to.
name: An optional variable_scope name.
Returns:
accuracy: A Tensor representing the accuracy, the value of total divided by count.
update_op: An operation that increments the total and count variables appropriately and whose value matches accuracy.
'''
說明
根據标簽 labels 和 predicts(模型的預測)來計算準确率acc
該函數會建立兩個局部變量 total 和 count
The accuracy function creates two local variables, total and count that are used to compute the frequency with which predictions matches labels. This frequency is ultimately returned as accuracy: an idempotent operation that simply divides total by count.
一定要注意下面這段話:
For estimation of the metric over a stream of data, the function creates an update_op operation that updates these variables and returns the accuracy. Internally, an is_correct operation computes a Tensor with elements 1.0 where the corresponding elements of predictions and labels match and 0.0 otherwise. Then update_op increments total with the reduced sum of the product of weights and is_correct, and update_op increments count with the reduced sum of weights.
簡單點就是說:如果輸入是一個 a stream of data,雖然我們是一個一個batch_size的資料輸入,但是我們最後傳回的是在整個資料集上的準确率,因為update_op每次都會自動更新 total 和count
注意函數傳回的
accuracy: A Tensor representing the accuracy, the value of total divided by count.
update_op: An operation that increments the total and count variables appropriately and whose value matches accuracy.