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

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

    • 报错代码
    • 原因分析
    • 解决办法
      • 安装
      • 用法

报错代码

conv = tf.contrib.layers.batch_norm()

原因分析

因为使用了TF1.x的库,而环境是TF2.x的版本,此时tf.contrib模块已被移除,根据官方迁移指南对其修改。
在这里插入图片描述
AttributeError: module ‘tensorflow‘ has no attribute ‘contrib‘_第1张图片
查看TF Slim源码发现名字接近的函数

def batch_norm()

解决办法

安装

pip install --upgrade tf_slim

用法

import tf_slim as slim
#conv = tf.contrib.layers.batch_norm()  #报错代码
conv = slim.batch_norm()

你可能感兴趣的:(debug,tensorflow,人工智能,python)