tensorflow2踩坑记录

使用tensorflow2过程中常常会出现一些莫名其妙的错误,百度一般也很难找到答案,现记录我使用过程中遇到的一些报错,慢更~

文章目录

    • 1. module 'tensorflow' has no attribute 'compat'
    • 2. tensorflow.python.framework.errors_impl.InternalError: Blas GEMV launch failed: m=2, n=10 [Op:MatMul] name: MatMul/

1. module ‘tensorflow’ has no attribute ‘compat’ 出现 module ‘tensorflow’ has no attribute ‘compat’
tensorflow2踩坑记录
文章图片

错误原因:tensorflow-gpu版本跟tensorflow-estimator版本不匹配,比如我的tensorflow-gpu版本是2.0.0,但tensorflow-estimator是2.2.0(有可能显示的是2.0.0,但是在程序运行时还是会报错)
解决方案:
先在anaconda prompt中输入conda list,查看tensorflow-estimator的版本,我的tensorflow-gpu是2.0.0,但是tensorflow-estimator是2.2.0的版本,所以会报错误。
需要重新安装 tensorflow-estimator,使用 pip 命令,不要使用 conda 命令,如果使用 conda 命令的话所有的 tensorflow 相关的包都会被卸载掉
先删除电脑上已经安装好的tensorflow-estimator版本,输入:
pip uninstall tensorflow-estimator

然后执行:
pip install tensorflow-estimator==2.0.0 pip install tensorflow-gpu=2.0.0

问题解决!
2. tensorflow.python.framework.errors_impl.InternalError: Blas GEMV launch failed: m=2, n=10 [Op:MatMul] name: MatMul/ 【tensorflow2踩坑记录】tensorflow2踩坑记录
文章图片

错误原因:
  • 经过查找相关资料和自己试验,发现这可能和GPU的内存分配有关系;因为默认情况下在代码中使用GPU时,有把内存占满的趋势;即使有时候计算的数据量并不足以占用整个GPU。
  • 所以,我们的思路之一就是对GPU的使用模式进行设置,如下面的代码所示,我们将GPU设置为memory_growth模式,它的意思是此时我们需要多少GPU内存就使用多少,不会过多占用。
问题解决:在你的代码中添加如下代码
## 列出你所有的物理GPU gpus = tf.config.experimental.list_physical_devices('GPU') tf.config.experimental.set_memory_growth(gpus[0], True)

如果你有多个GPU,那么你也可以将所有的GPU都设置成这个模式,如下面代码所示。
for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True)

问题解决!

    推荐阅读