首页 > 解决方案 > tf.contrib.metrics.f1_score 无法导入

问题描述

我正在尝试使用 tf.contrib.metrics.f1_score 计算 F1 分数,但它给了我一个错误。我知道如何使用精度和召回率来计算它,但我想使用这个函数。

我已经在 ubuntu 16.04 LTS 上使用 tensorflow 版本 1.9.0 和 gpu 支持但没有 gpu 支持

from tensorflow.contrib.metrics import f1_score as ms

我收到此错误:

ImportError: Traceback (most recent call   last)
<ipython-input-6-627f14191ea2> in <module>()----> 1 from tensorflow.contrib.metrics import  f1_score as ms

ImportError: cannot import name 'f1_score'

from tensorflow.contrib import metrics as ms
ms.f1_score

我收到此错误:

AttributeError Traceback (most recent call last)
<ipython-input-8-c19f57465581> in <module>()
1 from tensorflow.contrib import metrics as ms
----> 2 ms.f1_score

AttributeError: module 'tensorflow.contrib.metrics' has no attribute 'f1_score'

我希望 ms.f1_score 会加载

标签: python-3.xtensorflow

解决方案


如果您确定您有tf.contrib可用的并且这对您不起作用,那么您可能需要重新安装 tensorflow 使用pip install -U tensorflow或使用 -GPU 如果您正在使用该版本。

如果失败,请到安装tensorflow的地方手动检查是否可用,如果可用,请确保在同一目录(当前工作目录)下没有名为tensorflow的文件。 py 或 tf.py

之后你应该得到 正确加载图片

更新:正如用户@grwlf 所指出的

自 TensorFlow 2.0 起,tf.contrib 模块已移至 Addons 存储库。请参阅 github.com/tensorflow/addons。在那里,F1 mesure 可作为 F1Score 从tensorflow_addons.metrics import F1Score


推荐阅读