tensorflow2.6.0跑tensorflow1.x代码(RTX3060,Windows10)

文章讲述了在使用RTX3060显卡时遇到的CUDA和Tensorflow版本不兼容问题,包括Tensorflow1.x到2.x升级导致的错误,如模块找不到、贡献模块移除等,并提供了相应的代码修改方案。另外,还提到了CUDA版本与GPU不匹配引发的内部错误,以及解决方法是降低CUDA版本。最后,作者分享了自己配置的系统环境(Windows11,CUDA11.2,cuDNN8.1.1.33,tensorflow-gpu2.6.0,Python3.6.13)并鼓励读者交流类似问题。

问题背景:

提示:这里简述问题背景:

  • 电脑显卡是RTX3060,和CUDA10.x不适配
  • CUDA11.x和tensorflow1.x不适配
    因此配置CUDA11.x+tensorflow2.x

tensorflow1.x转2.x出现的问题

提示:这里描述项目中遇到的问题:

1. AttributeError:module tensorflow no attribute app

在这里插入图片描述

修改前的代码:
import tensorflow as tf
修改后的代码:
import tensorflow.compat.v1 as tf

2. module ‘tensorflow.compat.v1’ has no attribute ‘contrib’

修改前的代码:
=tf.contrib.rnn.LSTMCell(self.lstm_dim)
修改后的代码:
=tf.nn.rnn_cell.LSTMCell(self.lstm_dim)
修改前的代码:
=tf.contrib.crf_log_likelihood(inputs=self.ner_project_logits,tag_indices=self.label, sequence_lengths=self.sent_len)
修改后的代码:
import tensorflow_addons as tfa
=tfa.text.crf_log_likelihood(inputs=self.ner_project_logits,tag_indices=self.label,sequence_lengths=self.sent_len)
修改前的代码
initializer = tf.contrib.layers.xavier_initializer()
修改后的代码
initializer = tf.keras.initializers.glorot_normal()

3.RTX3060显卡不支持CUDA10.x

InternalError: Blas GEMM launch failed : a.shape=(100, 784), b.shape=(784, 10), m=100, n=10…

在这里插入图片描述

修改CUDA版本即可

4.安装tensorflow-gpu时,显示当前版本已被移除

The “tensorflow-gpu” package has been removed!

在这里插入图片描述

找相对应的whl文件,下载下来然后在pip install xxx

5.可以检测到GPU,但是无法用GPU跑代码

print(tf.test.is_gpu_available())
print(tf.test.is_built_with_cuda())

上述代码的输出都为true,但是还是没有办法使用GPU
错误代码如下:

W tensorflow/core/common_runtime/colocation_graph.cc:1016] Failed to place the graph without changing the devices of some resources. Some of the operations (that had to be colocated with resource generating operations) are not supported on the resources' devices. Current candidate devices are [
/job:localhost/replica:0/task:0/device:CPU:0].

划重点:当前tensorflow-gpu版本为2.6.0,CUDA版本为11.3,所以一直出现这样的问题!!!!更改CUDA版本为11.2,问题解决了!!!


本人目前配置

  1. Windows 11
  2. RTX 3060
  3. CUDA 11.2
  4. cuDNN 8.1.1.33
  5. tensorflow-gpu 2.6.0
  6. python 3.6.13
    仅供参考,遇到问题的时候忘记记录了,可能还有遗漏的问题,欢迎大家在评论区给我评论哦,我之后也会把相关文件的链接放到评论区
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值