如果使用一行的代码内的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 2.0版本中遇到'AttributeError: module 'tensorflow' has no attribute 'contrib''问题时,如何在新版中访问aggregate_metric_map()函数。作者提供了自定义函数实现,并指导读者如何在没有'contrib'模块的情况下使用。
2273

被折叠的 条评论
为什么被折叠?



