计算 predictions 匹配 labels 的频率。
用法
tf.compat.v1.metrics.accuracy(
labels, predictions, weights=None, metrics_collections=None,
updates_collections=None, name=None
)参数
-
labels基本事实值,一个Tensor,其形状与predictions匹配。 -
predictions预测值,任何形状的Tensor。 -
weights可选Tensor,其秩为0,或与labels相同的秩,并且必须可广播到labels(即,所有维度必须是1,或与相应的labels维度相同) . -
metrics_collectionsaccuracy应添加到的可选集合列表。 -
updates_collectionsupdate_op应添加到的可选集合列表。 -
name可选的 variable_scope 名称。
返回
-
accuracy一个Tensor代表准确度,total的值除以count。 -
update_op适当增加total和count变量并且其值与accuracy匹配的操作。
抛出
-
ValueError如果predictions和labels的形状不匹配,或者如果weights不是None并且其形状与predictions不匹配,或者如果metrics_collections或updates_collections不是列表或元组。 -
RuntimeError如果启用了即刻执行。
迁移到 TF2
警告:这个 API 是为 TensorFlow v1 设计的。继续阅读有关如何从该 API 迁移到本机 TensorFlow v2 等效项的详细信息。见TensorFlow v1 到 TensorFlow v2 迁移指南有关如何迁移其余代码的说明。
tf.compat.v1.metrics.accuracy 与即刻执行或 tf.function 不兼容。请改用tf.keras.metrics.Accuracy 进行 TF2 迁移。实例化tf.keras.metrics.Accuracy对象后,可以先调用update_state()方法记录预测/标签,然后调用result()方法即刻获取准确率。您还可以在调用compile 方法时将其附加到 Keras 模型。有关详细信息,请参阅本指南。
到原生 TF2 的结构映射
前:
accuracy, update_op = tf.compat.v1.metrics.accuracy(
labels=labels,
predictions=predictions,
weights=weights,
metrics_collections=metrics_collections,
update_collections=update_collections,
name=name)
后:
m = tf.keras.metrics.Accuracy(
name=name,
dtype=None)
m.update_state(
y_true=labels,
y_pred=predictions,
sample_weight=weights)
accuracy = m.result()
如何映射参数
| TF1 参数名称 | TF2 参数名称 | 注意 |
|---|---|---|
label |
y_true |
在update_state() 方法中 |
predictions |
y_true |
在update_state() 方法中 |
weights |
sample_weight |
在update_state() 方法中 |
metrics_collections
|
不支持 | 应显式跟踪指标或使用 Keras API,例如add_metric,而不是通过集合 |
updates_collections |
不支持 | - |
name |
name |
在构造函数中 |
使用示例之前和之后
前:
g = tf.Graph()
with g.as_default():
logits = [1, 2, 3]
labels = [0, 2, 3]
acc, acc_op = tf.compat.v1.metrics.accuracy(logits, labels)
global_init = tf.compat.v1.global_variables_initializer()
local_init = tf.compat.v1.local_variables_initializer()
sess = tf.compat.v1.Session(graph=g)
sess.run([global_init, local_init])
print(sess.run([acc, acc_op]))
[0.0, 0.66667]
后:
m = tf.keras.metrics.Accuracy()
m.update_state([1, 2, 3], [0, 2, 3])
m.result().numpy()
0.66667
# Used within Keras model
model.compile(optimizer='sgd',
loss='mse',
metrics=[tf.keras.metrics.Accuracy()])
accuracy 函数创建两个局部变量 total 和 count 用于计算 predictions 匹配 labels 的频率。该频率最终返回为 accuracy :一个幂等运算,只需将 total 除以 count 。
为了估计数据流上的度量,该函数创建一个 update_op 操作来更新这些变量并返回 accuracy 。在内部,is_correct 操作计算 Tensor,其中元素 1.0 与 predictions 和 labels 的对应元素匹配,否则为 0.0。然后 update_op 将 total 与 weights 和 is_correct 的乘积的减和相加,并用 weights 的减和后增加 count 。
如果 weights 是 None ,则权重默认为 1。使用权重 0 来屏蔽值。
相关用法
- Python tf.compat.v1.metrics.mean用法及代码示例
- Python tf.compat.v1.mixed_precision.enable_mixed_precision_graph_rewrite用法及代码示例
- Python tf.compat.v1.map_fn用法及代码示例
- Python tf.compat.v1.make_template用法及代码示例
- Python tf.compat.v1.multinomial用法及代码示例
- Python tf.compat.v1.math.log_softmax用法及代码示例
- Python tf.compat.v1.mixed_precision.MixedPrecisionLossScaleOptimizer用法及代码示例
- Python tf.compat.v1.math.softmax用法及代码示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代码示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代码示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代码示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代码示例
- Python tf.compat.v1.Variable.eval用法及代码示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代码示例
- Python tf.compat.v1.layers.conv3d用法及代码示例
- Python tf.compat.v1.strings.length用法及代码示例
- Python tf.compat.v1.data.Dataset.snapshot用法及代码示例
- Python tf.compat.v1.data.experimental.SqlDataset.reduce用法及代码示例
- Python tf.compat.v1.feature_column.categorical_column_with_vocabulary_file用法及代码示例
- Python tf.compat.v1.data.TextLineDataset.from_tensors用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.metrics.accuracy。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
