@Team 2018-08-20T22:29:40.000000Z 字数 6195 阅读 6009

# 深入理解TensorFlow中的tf.metrics算子

叶虎

## 1. 概述

• tf.metrics.accuracy()
• tf.metrics.precision()
• tf.metrics.recall()
• tf.metrics.mean_iou()

## 3. 生成数据

import numpy as nplabels = np.array([[1,1,1,0],                   [1,1,1,0],                   [1,1,1,0],                   [1,1,1,0]], dtype=np.uint8)predictions = np.array([[1,0,0,0],                        [1,1,0,0],                        [1,1,1,0],                        [0,1,1,1]], dtype=np.uint8)n_batches = len(labels)

## 4. 建立评价指标

n_items = labels.sizeaccuracy = (labels ==  predictions).sum() / n_itemsprint("Accuracy :", accuracy)[OUTPUT]Accuracy : 0.6875

• 正确预测的例子总和
• 目前所有例子的总数

# Initialize running variablesN_CORRECT = 0N_ITEMS_SEEN = 0

# Update running variablesN_CORRECT += (batch_labels == batch_predictions).sum()N_ITEMS_SEEN += batch_labels.size

# Calculate accuracy on updated valuesacc = float(N_CORRECT) / N_ITEMS_SEEN

# Create running variablesN_CORRECT = 0N_ITEMS_SEEN = 0def reset_running_variables():    """ Resets the previous values of running variables to zero     """    global N_CORRECT, N_ITEMS_SEEN    N_CORRECT = 0    N_ITEMS_SEEN = 0def update_running_variables(labs, preds):    global N_CORRECT, N_ITEMS_SEEN    N_CORRECT += (labs == preds).sum()    N_ITEMS_SEEN += labs.sizedef calculate_accuracy():    global N_CORRECT, N_ITEMS_SEEN    return float(N_CORRECT) / N_ITEMS_SEEN

### 4.1 整体accuracy

reset_running_variables()for i in range(n_batches):    update_running_variables(labs=labels[i], preds=predictions[i])accuracy = calculate_accuracy()print("[NP] SCORE: ", accuracy)[OUTPUT][NP] SCORE:  0.6875

### 4.2 每个batch的accuracy

for i in range(n_batches):    reset_running_variables()    update_running_variables(labs=labels[i], preds=predictions[i])    acc = calculate_accuracy()    print("- [NP] batch {} score: {}".format(i, acc))[OUTPUT]- [NP] batch 0 score: 0.5- [NP] batch 1 score: 0.75- [NP] batch 2 score: 1.0- [NP] batch 3 score: 0.5

## 5. TensorFlow中的metrics

• 会同样地创建两个变量（变量会加入tf.GraphKeys.LOCAL_VARIABLES集合中），并将其放入幕后的计算图中：
• total（相当于N_CORRECT
• count（相当于N_ITEMS_SEEN
• 返回两个tensorflow操作。
• accuracy（相当于calculate_accuracy()
• update_op（相当于update_running_variables()

tf.metrics.accuracy(label, prediction, name="my_metric")

# Isolate the variables stored behind the scenes by the metric operationrunning_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="my_metric")<tf.Variable 'my_metric/total:0' shape=() dtype=float32_ref>,<tf.Variable 'my_metric/count:0' shape=() dtype=float32_ref>

running_vars_initializer = tf.variables_initializer(var_list=running_vars)

session.run(running_vars_initializer)

session.run(tf.local_variables_initializer())

### 5.1 计算整体accuracy

import tensorflow as tfgraph = tf.Graph()with graph.as_default():    # Placeholders to take in batches onf data    tf_label = tf.placeholder(dtype=tf.int32, shape=[None])    tf_prediction = tf.placeholder(dtype=tf.int32, shape=[None])    # Define the metric and update operations    tf_metric, tf_metric_update = tf.metrics.accuracy(tf_label,                                                      tf_prediction,                                                      name="my_metric")    # Isolate the variables stored behind the scenes by the metric operation    running_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="my_metric")    # Define initializer to initialize/reset running variables    running_vars_initializer = tf.variables_initializer(var_list=running_vars)with tf.Session(graph=graph) as session:    session.run(tf.global_variables_initializer())    # initialize/reset the running variables    session.run(running_vars_initializer)    for i in range(n_batches):        # Update the running variables on new batch of samples        feed_dict={tf_label: labels[i], tf_prediction: predictions[i]}        session.run(tf_metric_update, feed_dict=feed_dict)    # Calculate the score    score = session.run(tf_metric)    print("[TF] SCORE: ", score)[OUTPUT][TF] SCORE:  0.6875

### 5.2 计算每个batch的accuracy

with tf.Session(graph=graph) as session:    session.run(tf.global_variables_initializer())    for i in range(n_batches):        # Reset the running variables        session.run(running_vars_initializer)        # Update the running variables on new batch of samples        feed_dict={tf_label: labels[i], tf_prediction: predictions[i]}        session.run(tf_metric_update, feed_dict=feed_dict)        # Calculate the score on this batch        score = session.run(tf_metric)        print("[TF] batch {} score: {}".format(i, score))[OUTPUT][TF] batch 0 score: 0.5[TF] batch 1 score: 0.75[TF] batch 2 score: 1.0[TF] batch 3 score: 0.5

### 5.3 要避免的问题

_ , score = session.run([tf_metric_update, tf_metric], feed_dict=feed_dict)score, _ = session.run([tf_metric, tf_metric_update], feed_dict=feed_dict)

## 6. 其它metric

tf.metrics中的其他评估指标将以相同的方式工作。它们之间的唯一区别可能是调用tf.metrics函数时需要额外参数。例如，tf.metrics.mean_iou需要额外的参数num_classes来表示预测的类别数。另一个区别是背后所创建的变量，如tf.metrics.mean_iou创建的是一个混淆矩阵，但仍然可以按照我在本文第5部分中描述的方式收集和初始化它们。

• 私有
• 公开
• 删除