01

# 概述

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

02

03

# 生成数据

import numpy as np
labels = 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)

04

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

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

# Initialize running variables
N_CORRECT = 0
N_ITEMS_SEEN = 0

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

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

# Create running variables
N_CORRECT = 0
N_ITEMS_SEEN = 0
def reset_running_variables():
""" Resets the previous values of running variables to zero     """
global N_CORRECT, N_ITEMS_SEEN
N_CORRECT = 0
N_ITEMS_SEEN = 0
def update_running_variables(labs, preds):
global N_CORRECT, N_ITEMS_SEEN
N_CORRECT += (labs == preds).sum()
N_ITEMS_SEEN += labs.size
def 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

05

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 operation
running_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 tf
graph = 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)

06

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

07

END

0 条评论

• ### Tensorflow快速入门

作者：叶　虎 编辑：李文臣 PART 01 Tensorflow简介 引言 实践深度学习肯定要至少学习并掌握一个深度学习框架。这里我们介绍一个最流行的深度学习框...

• ### 实例介绍TensorFlow的输入流水线

在训练模型时，我们首先要处理的就是训练数据的加载与预处理的问题，这里称这个过程为输入流水线（input pipelines，或输入管道，[参考：https://...

• ### CNN模型之SqueezeNet

作者： 叶 虎 编辑：赵一帆 01 引言 SqueezeNet是Han等提出的一种轻量且高效的CNN模型，它参数比AlexNet少50x，但模型性能（acc...

• ### TensorFlow指南（一）——上手TensorFlow

http://blog.csdn.net/u011239443/article/details/79066094 TensorFlow是谷歌开源的深度学习库...

• ### tensorflow编程: Running Graphs

A class for running TensorFlow operations.   这是一个类，执行 tensorflow 中的 op 。它里面定...

• ### TensorFlow从0到1 - 16 - L2正则化对抗“过拟合”

前面的14 交叉熵损失函数——防止学习缓慢和15 重新思考神经网络初始化从学习缓慢问题入手，尝试改进神经网络的学习。本篇讨论过拟合问题，并引入与之相对的L2正...

• ### TensorFlow从0到1丨第十六篇 L2正则化对抗“过拟合”

前面的第十四篇 交叉熵损失函数——防止学习缓慢和第十五篇 重新思考神经网络初始化从学习缓慢问题入手，尝试改进神经网络的学习。本篇讨论过拟合问题，并引入与之相对的...

• ### Tensorflow 2.0 的这些新设计，你适应好了吗？

如果说两代 Tensorflow 有什么根本不同，那应该就是 Tensorflow 2.0 更注重使用的低门槛，旨在让每个人都能应用机器学习技术。考虑到它可能会...

• ### TensorFlow2.X学习笔记(4)--TensorFlow低阶API之AutoGraph相关研究

而Autograph机制可以将动态图转换成静态计算图，兼收执行效率和编码效率之利。