【问题标题】:AttributeError: module 'tensorflow' has no attribute 'contrib' in tensorflow 2.0AttributeError:模块“tensorflow”在 tensorflow 2.0 中没有属性“contrib”
【发布时间】:2020-08-17 15:39:02
【问题描述】:

我正在尝试在 tensorflow 2.0 中运行 mean iou 在代码中我使用了一行

tf.contrib.metrics.aggregate_metric_map()

但在运行代码时出现错误

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

我们如何在 TF 2.0 中访问aggregate_metric_map()

【问题讨论】:

    标签: python tensorflow keras deep-learning tensorflow2.0


    【解决方案1】:

    您可以从旧的tensorflow 存储库中复制该函数,因为它没有任何特殊的依赖关系。

    参考:https://github.com/tensorflow/tensorflow/blob/r1.8/tensorflow/contrib/metrics/python/ops/metric_ops.py

    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))
    

    【讨论】:

      【解决方案2】:

      Tensorflow 2.0 没有 contrib 模块。这是来自 TF 文档的引用

      大量较旧的 TensorFlow 1.x 代码使用 Slim 库,该库与 TensorFlow 1.x 一起打包为 tf.contrib.layers。 作为一个 contrib 模块,它在 TensorFlow 2.0 中不再可用,即使在 tf.compat.v1 中也是如此。使用 Slim 将代码转换为 TF 2.0 比转换使用 v1.layers 的存储库更复杂。实际上,先将 Slim 代码转换为 v1.layers,然后再转换为 Keras 可能是有意义的。

      更多信息请阅读TF migration guide

      【讨论】:

        猜你喜欢
        • 2021-06-23
        • 2022-11-09
        • 2019-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-16
        • 2020-03-17
        相关资源
        最近更新 更多