在tensorflow2.x版本的tensorflow.contrib.metrics里没有aggregate_metric_map()

本文介绍了在TensorFlow 2.0版本中遇到'AttributeError: module 'tensorflow' has no attribute 'contrib''问题时,如何在新版中访问aggregate_metric_map()函数。作者提供了自定义函数实现,并指导读者如何在没有'contrib'模块的情况下使用。
部署运行你感兴趣的模型镜像

如果使用一行的代码内的tensorflow 2.0中运行

tf.contrib.metrics.aggregate_metric_map()

运行时会出现错误

AttributeError: module 'tensorflow' has no attribute 'contrib'

那么我们如何在tensorflow2.X里访问aggregate_metric_map()呢?可以从旧tensorflow存储库复制该函数放到metrics里,因为它没有任何特殊的依赖关系

def aggregate_metric_map(names_to_tuples):
  """Aggregates the metric names to tuple dictionary.
  This function is useful for pairing metric names with their associated value
  and update ops when the list of metrics is long. For example:
  python
    metrics_to_values, metrics_to_updates = slim.metrics.aggregate_metric_map({
        'Mean Absolute Error': new_slim.metrics.streaming_mean_absolute_error(
            predictions, labels, weights),
        'Mean Relative Error': new_slim.metrics.streaming_mean_relative_error(
            predictions, labels, labels, weights),
        'RMSE Linear': new_slim.metrics.streaming_root_mean_squared_error(
            predictions, labels, weights),
        'RMSE Log': new_slim.metrics.streaming_root_mean_squared_error(
            predictions, labels, weights),
    })

  Args:
    names_to_tuples: a map of metric names to tuples, each of which contain the
      pair of (value_tensor, update_op) from a streaming metric.
  Returns:
    A dictionary from metric names to value ops and a dictionary from metric
    names to update ops.
  """
  metric_names = names_to_tuples.keys()
  value_ops, update_ops = zip(*names_to_tuples.values())
  return dict(zip(metric_names, value_ops)), dict(zip(metric_names, update_ops))

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纬领网络

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值