AttributeError:模块“tensorflow”在 conda 提示中没有属性“keras”
Posted
技术标签:
【中文标题】AttributeError:模块“tensorflow”在 conda 提示中没有属性“keras”【英文标题】:AttributeError: module 'tensorflow' has no attribute 'keras' in conda prompt 【发布时间】:2020-09-07 18:35:10 【问题描述】:*我尝试安装tensorflow和keras
我安装了 tensorflow,并且没有错误地导入它
Keras 已安装,但我无法导入它 *
(base) C:\Windows\system32>pip uninstall keras
Found existing installation: Keras 2.3.1
Uninstalling Keras-2.3.1:
Would remove:
c:\users\asus\anaconda3\anaconda\lib\site-packages\docs\*
c:\users\asus\anaconda3\anaconda\lib\site-packages\keras-2.3.1.dist-info\*
c:\users\asus\anaconda3\anaconda\lib\site-packages\keras\*
Proceed (y/n)? y
Successfully uninstalled Keras-2.3.1
(base) C:\Windows\system32>pip install keras
Collecting keras
Using cached Keras-2.3.1-py2.py3-none-any.whl (377 kB)
Requirement already satisfied: six>=1.9.0 in c:\users\asus\anaconda3\anaconda\lib\site-packages (from keras) (1.14.0)
Requirement already satisfied: numpy>=1.9.1 in c:\users\asus\anaconda3\anaconda\lib\site-packages (from keras) (1.18.4)
Requirement already satisfied: keras-applications>=1.0.6 in c:\users\asus\anaconda3\anaconda\lib\site-packages (from keras) (1.0.8)
Requirement already satisfied: keras-preprocessing>=1.0.5 in c:\users\asus\anaconda3\anaconda\lib\site-packages (from keras) (1.1.2)
Requirement already satisfied: scipy>=0.14 in c:\users\asus\anaconda3\anaconda\lib\site-packages (from keras) (1.4.1)
Requirement already satisfied: pyyaml in c:\users\asus\anaconda3\anaconda\lib\site-packages (from keras) (5.3.1)
Requirement already satisfied: h5py in c:\users\asus\anaconda3\anaconda\lib\site-packages (from keras) (2.10.0)
Installing collected packages: keras
Successfully installed keras-2.3.1
那我试试
(base) C:\Windows\system32>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from keras.models import sequential
Using TensorFlow backend.
2020-05-21 10:03:38.204077: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-05-21 10:03:38.210602: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\utils\__init__.py", line 26, in <module>
from .vis_utils import model_to_dot
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\utils\vis_utils.py", line 7, in <module>
from ..models import Model
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\models.py", line 10, in <module>
from .engine.input_layer import Input
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\engine\__init__.py", line 8, in <module>
from .training import Model
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\engine\training.py", line 14, in <module>
from . import training_utils
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\engine\training_utils.py", line 17, in <module>
from .. import metrics as metrics_module
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\metrics.py", line 1850, in <module>
BaseMeanIoU = tf.keras.metrics.MeanIoU
AttributeError: module 'tensorflow' has no attribute 'keras'
>>> from keras.models import Sequential
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\utils\__init__.py", line 26, in <module>
from .vis_utils import model_to_dot
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\utils\vis_utils.py", line 7, in <module>
from ..models import Model
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\models.py", line 12, in <module>
from .engine.training import Model
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\engine\__init__.py", line 8, in <module>
from .training import Model
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\engine\training.py", line 14, in <module>
from . import training_utils
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\engine\training_utils.py", line 17, in <module>
from .. import metrics as metrics_module
File "C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\keras\metrics.py", line 1850, in <module>
BaseMeanIoU = tf.keras.metrics.MeanIoU
AttributeError: module 'tensorflow' has no attribute 'keras'
我尝试: pip install --upgrade --no-deps --force-reinstall tensorflow
pip install --upgrade pip setuptools wheel
pip 卸载 protobuf
pip 安装 protobuf
pip install termcolor
**我有: keras 2.3.1 pypi_0 pypi
keras-applications 1.0.8 pypi_0 pypi
keras-preprocessing 1.1.2 pypi_0 pypi
密钥环 21.1.1 py36_2
kivy 1.10.1.dev0 pypi_0 pypi
kivy-deps-glew 0.2.0 pypi_0 pypi
kivy-deps-gstreamer 0.2.0 pypi_0 pypi
kivy-deps-sdl2 0.2.0 pypi_0 pypi
kivy-garden 0.1.4 pypi_0 pypi
kiwisolver 1.2.0 py36h74a9793_0
惰性对象代理 1.4.3 py36he774522_0 **
我有 numpy、pytorch、pip、tensorflow 和大多数 DL、ML、CV、Ai、DS 库
(base) C:\Users\ASUS>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
2020-05-22 06:23:19.327748: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-05-22 06:23:19.343057: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
>>> print(tensorflow)
<module 'tensorflow' from 'C:\\Users\\ASUS\\Anaconda3\\Anaconda\\lib\\site-packages\\tensorflow\\__init__.py'>
>>>
当我运行张量流时
(base) C:\Users\ASUS>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow.compat.v1 as tf
2020-05-22 06:18:12.900849: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-05-22 06:18:12.914907: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
>>> tf.disable_v2_behavior()
WARNING:tensorflow:From C:\Users\ASUS\Anaconda3\Anaconda\lib\site-packages\tensorflow\python\compat\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.compat.v1.Session()
2020-05-22 06:20:28.305196: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-05-22 06:20:29.278356: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 computeCapability: 6.1
coreClock: 1.493GHz coreCount: 5 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2020-05-22 06:20:29.295408: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-05-22 06:20:29.305186: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-05-22 06:20:29.314235: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2020-05-22 06:20:29.323997: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2020-05-22 06:20:29.336231: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
2020-05-22 06:20:29.349522: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-05-22 06:20:29.362633: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2020-05-22 06:20:29.372823: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-05-22 06:20:29.392493: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-05-22 06:20:29.413844: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x18bf4dc2f60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-05-22 06:20:29.424116: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-05-22 06:20:29.432117: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-05-22 06:20:29.446877: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
我解决后 在下面查看我的答案
(base) C:\Users\ASUS>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2020-05-29 17:59:47.814562: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
>>> import keras
Using TensorFlow backend.
>>> from keras.models import Sequential
>>> tf.test.is_built_with_cuda()
True
>>> tf.config.list_physical_devices('GPU')
2020-05-29 18:02:56.764618: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-05-29 18:02:57.602680: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 computeCapability: 6.1
coreClock: 1.493GHz coreCount: 5 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2020-05-29 18:02:57.607142: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-05-29 18:02:57.703213: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-05-29 18:02:57.766722: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-05-29 18:02:57.797062: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-05-29 18:02:57.872961: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-05-29 18:02:57.920469: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-05-29 18:02:58.035860: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-05-29 18:02:58.148699: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
>>> tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
2020-05-29 18:03:43.187834: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 computeCapability: 6.1
coreClock: 1.493GHz coreCount: 5 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2020-05-29 18:03:43.200093: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-05-29 18:03:43.205083: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-05-29 18:03:43.214701: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-05-29 18:03:43.217616: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-05-29 18:03:43.229945: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-05-29 18:03:43.238134: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-05-29 18:03:43.247848: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-05-29 18:03:43.256302: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-05-29 18:03:43.264822: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-05-29 18:03:43.269994: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0
2020-05-29 18:03:43.271951: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N
2020-05-29 18:03:43.275672: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/device:GPU:0 with 2993 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1)
True
>>>
【问题讨论】:
尝试使用 Conda 包管理器安装 TensorFlow。conda install tensorflow
你没有提到很重要的事情,你安装的是哪个版本的TensorFlow?
版本:2.2.0 摘要:TensorFlow 是一个适合所有人的开源机器学习框架。作者:Google Inc. 作者电子邮件:packages@tensorflow.org 许可证:Apache 2.0 位置:c:\users\asus\anaconda3\anaconda\lib\site-packages 需要:六个、google-pasta、tensorflow-estimator、grpcio、 tensorboard, scipy, wrapt, wheel, gast, keras-preprocessing, numpy, astunparse, opt-einsum, absl-py, h5py, protobuf, termcolor 要求:
请打开python shell,导入tensorflow,然后打印(tensorflow),并上报输出
python Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import tensorflow 2020-05-21 16:43:31.481970: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found 2020-05-21 16:43:31.491107: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. >>> print('tensorflow') tensorflow
【参考方案1】:
不要忘记为使用 tensorflow 创建一个环境。它需要它自己的环境。当我使用 conda 时,我在尝试使用 tf 时遇到了一些麻烦,这主要是因为我没有以正确的方式设置它。
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands
【讨论】:
我可以在没有 env 的情况下在我的 python 上安装 keras 吗?因为 envs 不能与 env 文件夹外的 python 上的代码一起使用,所以当我将 lib 用于所有项目和代码时,我希望它在我的 python 中,我可以将它导入到 env 文件夹外的任何代码中【参考方案2】:感谢所有答案但我解决了它我按照本教程使用 CUDA 10.1 https://towardsdatascience.com/installing-tensorflow-with-cuda-cudnn-and-gpu-support-on-windows-10-60693e46e781 。在本教程之后我卸载这个库并再次安装它pip install keras
、pip install --upgrade setuptools
、pip install cmake
、pip install keras-models
、@ 987654326@、pip install keras-preprocessing
并下载 Visual Studio 2015。然后从我的问题中运行代码,如 from keras.models import Sequential
并检查路径路径。
【讨论】:
以上是关于AttributeError:模块“tensorflow”在 conda 提示中没有属性“keras”的主要内容,如果未能解决你的问题,请参考以下文章
AttributeError:模块'keras'没有属性'initializers'
AttributeError:模块'asyncio'没有属性'run'
AttributeError:模块'mysql'没有属性'connector'