无法从tensorflow.keras.metrics导入指标 [英] unable to import Metric from tensorflow.keras.metrics

查看:425
本文介绍了无法从tensorflow.keras.metrics导入指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个自定义指标评估程序,我正在关注它此链接.我的虚拟代码是

I want to write a custom metric evaluator for which I am following this link. my dummy code is

import tensorflow as tf    
from tensorflow import keras    

class DummyMetric(keras.metrics.Metric):

    def __init__(self, name='categorical_true_positives', **kwargs):
      super(DummyMetric, self).__init__(name=name, **kwargs)
      self.true_positives = self.add_weight(name='tp', initializer='zeros')

    def update_state(self, y_true, y_pred, sample_weight=None):
      print("Evaluating tensor of shape {} against gt of shape {}".format(y_pred.shape, y_true.shape))
      self.true_positives.assign_add(1.0)

    def result(self):
      return self.true_positives

    def reset_states(self):
      # The state of the metric will be reset at the start of each epoch.
      self.true_positives.assign(0.)

我的tensorflow版本是从源代码安装的1.13.1 .

keras.metrics.Metric抛出

AttributeError:模块'tensorflow._api.v1.keras.metrics'没有属性'Metric'.

AttributeError: module 'tensorflow._api.v1.keras.metrics' has no attribute 'Metric'.

当我执行pip install tensorflow-gpu==1.14时,此错误消失了.

When I do pip install tensorflow-gpu==1.14 then this error goes away.

请尽可能提出建议的解决方案/黑客,使其在不升级到1.14的情况下正常工作

please suggest any solution/hack if possible which will make it work without upgrading to 1.14

推荐答案

似乎这可能已被排除在__init__.py之外,他们估计在1.14中已将其修复.我能够以这种方式导入它:

It seems like this was probably left out of an __init__.py and they fixed that in 1.14 I guess. I was able to import it this way:

from tensorflow.python.keras.metrics import Metric

它在文件中定义:

tensorflow/python/keras/metrics.py

这篇关于无法从tensorflow.keras.metrics导入指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆