cannot import name ‘dtensor‘ from ‘tensorflow.compat.v2.experimental‘

问题解决:cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental'

  • 环境:
    • tensorflow和cuda、python版本对应
    • 解决方法:
    • 最终解决方法:

环境:

tensorflow-gpu=2.6.0,keras=2.6.0
环境对应但仍有问题

tensorflow和cuda、python版本对应

cannot import name ‘dtensor‘ from ‘tensorflow.compat.v2.experimental‘_第1张图片

我的cuda版本:CUDA Version: 12.0
[参考链接] https://tensorflow.google.cn/install/source?hl=en

解决方法:

重新下载了高版本的tensorflow-gpu。

pip install tensorflow-gpu==2.10.0

发现keras已经存在了2.10.0,但出现问题

This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX_VNNI FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-02-23 14:41:39.592866: I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2023-02-23 14:41:39.609203: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2023-02-23 14:41:39.929699: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-11.7/lib64:/usr/local/cuda-11.7/lib64
2023-02-23 14:41:39.929738: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-11.7/lib64:/usr/local/cuda-11.7/lib64
2023-02-23 14:41:39.929742: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
这个TensorFlow二进制文件使用oneAPI深度神经网络库(oneDNN)进行了优化,以便在性能关键操作中使用以下CPU指令:AVX2 AVX_VNNI FMA
要在其他操作中启用它们,请使用适当的编译器标志重新构建TensorFlow。
2023-02-23 14:41:39.592866: I tensorflow/core/util/util。cc:169] oneDNN自定义操作正在进行。由于来自不同计算顺序的浮点舍入误差,您可能会看到略有不同的数值结果。要关闭它们,请设置环境变量“TF_ENABLE_ONEDNN_OPTS=0”。
2023-02-23 14:41:39.609203: E tensorflow/stream_executor/cuda/cuda_blas。cc:2981]无法注册cuBLAS工厂:当一个插件cuBLAS已经注册时,试图注册工厂
2023-02-23 14:41:39.929699: W tensorflow/stream_executor/platform/default/dso_loader。cc:64]无法加载动态库'libnvinfer.so.7';dlerror: libnvinfer.so。7:无法打开共享对象文件:没有这样的文件或目录;LD_LIBRARY_PATH: /usr/local/cuda - 11.7 / lib64: / usr / lib64 /地方/ cuda - 11.7
2023-02-23 14:41:39.929738: W tensorflow/stream_executor/platform/default/dso_loader。cc:64]无法加载动态库'libnvinfer_plugin.so.7';dlerror: libnvinfer_plugin.so。7:无法打开共享对象文件:没有这样的文件或目录;LD_LIBRARY_PATH: /usr/local/cuda - 11.7 / lib64: / usr / lib64 /地方/ cuda - 11.7
2023-02-23 14:41:39.929742: W tensorflow/compiler/tf2tensorrt/utils/py_utils。TF-TRT警告:不能释放一些TensorRT库。如果你想在TensorRT中使用Nvidia GPU,请确保上面提到的缺少的库已经正确安装。

根据提示,解决以上问题:

第一条:
I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
方法:
import os
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
第二条:
 E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
 方法:
 什么乱七八糟的错误啊啊啊啊啊

最终解决方法:

卸载tensorflow-gpu,重新安装,用 conda 安装

conda install tensorflow-gpu

安装完后是2.4.1版本

然后安装keras

conda install keras
import tensorflow as tf
print(tf.__version__)
import keras
print(keras.__version__)
2.4.1
2.4.3

终于可以用tensorflow和keras了。
总结:在版本对应的情况下仍出现该问题,应该是pip和conda安装的库互相不兼容,各版本乱七八糟的了,卸载近用conda安装解决了此问题。
但是tensorflow-gpu变成2.4.1,版本下降了

你可能感兴趣的:(tensorflow,深度学习,python)