用 conda 安装的 TensorFlow
在 conda 下创建的 3.8 python 环境,安装 TensorFlow 2.6
但是简单的程序运行报错,不知道该咋办了
代码
import tensorflow as tf import sys print(sys.version) print(tf.__version__)
运行报错:
PS C:\Users\Administrator> & C:/ProgramData/miniconda3/envs/TensorFlow_py/python.exe d:/MyCodes/Phython/App2/Test01.py C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\framework\dtypes.py:585: FutureWarning: In the future `np.object` will be defined as the corresponding NumPy scalar. np.object, Traceback (most recent call last): File "d:/MyCodes/Phython/App2/Test01.py", line 1, in <module> import tensorflow as tf File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\__init__.py", line 41, in <module> from tensorflow.python.tools import module_util as _module_util File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\__init__.py", line 46, in <module> from tensorflow.python import data File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\data\__init__.py", line 25, in <module> from tensorflow.python.data import experimental File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 97, in <module> from tensorflow.python.data.experimental import service File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 353, in <module> from tensorflow.python.data.experimental.ops.data_service_ops import distribute File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 26, in <module> from tensorflow.python.data.experimental.ops import compression_ops File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 20, in <module> from tensorflow.python.data.util import structure File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\data\util\structure.py", line 26, in <module> from tensorflow.python.data.util import nest File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\data\util\nest.py", line 40, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 28, in <module> from tensorflow.python.framework import constant_op File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\framework\constant_op.py", line 29, in <module> from tensorflow.python.eager import execute File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\eager\execute.py", line 27, in <module> from tensorflow.python.framework import dtypes File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\tensorflow\python\framework\dtypes.py", line 585, in <module> np.object, File "C:\ProgramData\miniconda3\envs\TensorFlow_py\lib\site-packages\numpy\__init__.py", line 305, in __getattr__ raise AttributeError(__former_attrs__[attr]) AttributeError: module 'numpy' has no attribute 'object'. `np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
要怎么处理?
您遇到的错误涉及到 TensorFlow 使用的 NumPy 模块中的一个别名问题。根据错误信息,np.object 是一个已经被弃用的别名,而您当前使用的 NumPy 版本可能较新,已经删除了这个别名。
为了解决这个问题,您可以考虑以下几个步骤:
更新 NumPy: 更新 NumPy 到最新版本可能会解决这个问题。您可以使用以下 conda 命令更新 NumPy:
r
Copy code
conda install -c conda-forge numpy
这会安装最新版本的 NumPy,其中不再包含已弃用的别名。
检查 TensorFlow 版本: 您可以确保使用的 TensorFlow 版本与您的 Python 环境兼容。在某些情况下,特定版本的 TensorFlow 可能对应着特定版本的 NumPy。您可以考虑降级 TensorFlow 或升级 NumPy 以确保它们之间的兼容性。
r
Copy code
conda install -c conda-forge tensorflow=2.6
降级 NumPy: 如果更新 NumPy 不起作用,您可以尝试降级 NumPy 版本,直到找到与 TensorFlow 兼容的版本。可以使用以下命令:
r
Copy code
conda install -c conda-forge numpy=1.19
请注意,上述命令中的版本号仅作为示例,您可能需要根据实际情况调整版本号。同时,请确保备份您的环境或使用虚拟环境,以免影响其他项目。
如果问题仍然存在,您可能需要查看 TensorFlow 和 NumPy 的官方文档、GitHub 存储库或相关社区,以获取更多关于版本兼容性的信息和解决方案。
torch和tensorflow最好不要用conda装,用pip装