栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

AttributeError: module ‘tensorflow.compat.v1’ has no attribute ‘contrib’

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AttributeError: module ‘tensorflow.compat.v1’ has no attribute ‘contrib’

项目场景:

AttributeError: module ‘tensorflow.compat.v1’ has no attribute ‘contrib’


问题描述

公司的是3080显卡,跑tensorflow1.x的代码,由于cuda版本限制只能配tensorflow2.6的环境,运行会报这个错


原因分析: 原因是tensorflow1.x的contrib包被整合进了三个包里(据这位大佬说:https://blog.csdn.net/xiangfengl/article/details/123904263)
tf.keras.layers.Layer
tf.keras.Model
tf.Module 解决方案:

解决方法一目了然,找到新包中的对应方法替换一下。
先把import tensorflow as tf 替换成如下:

	import tensorflow.compat.v1 as tf
	tf.disable_v2_behavior()

这里列举一些我遇到的
原来的:

	tf.contrib.layers.l2_regularizer(scale = 1e-10)
	tf.contrib.layers.xavier_initializer()
	tf.contrib.layers.batch_norm(input, decay=0.99, updates_collections=None, epsilon=1e-5, scale=True, is_training=phase_train)
	tf.contrib.layers.instance_norm(input)

依次替换为:

	tf.keras.regularizers.l2(1e-10)
	tf.keras.initializers.glorot_normal()
	tf.keras.layers.BatchNormalization()(input)
	InstanceNormalization()(input)

其中最后一个InstanceNormalization()(input)会报错,需要安装keras_contrib库:

pip install git+https://www.github.com/keras-team/keras-contrib.git

然后还需要在自己的代码中引入:

from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization

这就是我的解决方法,有问题欢迎指出。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934844.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号