无法从tensorflow导入keras [英] can't import keras from tensorflow
问题描述
我正在尝试使用keras,但无法使用,因为当我从tensorflow import keras运行时,出现此错误:
I am trying to use keras but am unable to because when I run from tensorflow import keras
I get this error:
➜ kerasTutorial python3
Python 3.7.7 (default, Mar 10 2020, 15:43:33)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from tensorflow import keras
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 64, in <module>
from tensorflow.python.framework.framework_lib import * # pylint: disable=redefined-builtin
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/framework_lib.py", line 25, in <module>
from tensorflow.python.framework.ops import Graph
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 64, in <module>
from tensorflow.python.platform import app
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/platform/app.py", line 23, in <module>
from absl.app import run as _run
File "/usr/local/lib/python3.7/site-packages/absl/app.py", line 35, in <module>
import pdb
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pdb.py", line 76, in <module>
import code
File "/Users/mathewlewis/Documents/Y4S3/Crypto/kerasTutorial/code.py", line 4, in <module>
from tensorflow.keras.models import Sequential
File "/usr/local/lib/python3.7/site-packages/tensorflow/keras/__init__.py", line 14, in <module>
from . import activations
File "/usr/local/lib/python3.7/site-packages/tensorflow/keras/activations/__init__.py", line 10, in <module>
from tensorflow.python.keras.activations import deserialize
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/__init__.py", line 27, in <module>
from tensorflow.python.keras import models
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/models.py", line 23, in <module>
from tensorflow.python.keras import backend as K
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/backend.py", line 36, in <module>
from tensorflow.python.client import session as session_module
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 38, in <module>
from tensorflow.python.framework import sparse_tensor
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/sparse_tensor.py", line 29, in <module>
from tensorflow.python.framework import constant_op
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 324, in <module>
ops.register_tensor_conversion_function(
AttributeError: module 'tensorflow.python.framework.ops' has no attribute 'register_tensor_conversion_function'
>>>
我在Mac上使用python3.我已经使用 pip3 install keras
和 pip3 install tensorflow
I am using python3 on mac. I have installed both keras and tensorflow using pip3 install keras
and pip3 install tensorflow
我的印象是,鉴于在keras上此处,应该可以从tensorflow导入keras.io他们提供了以下示例代码:
I am under the impression that importing keras from tensorflow should be possible given that here on keras.io they provide the following sample code:
import numpy as np
import tensorflow as tf
from tensorflow import keras
因为这个原因,我怀疑我没有正确安装tensorflow或keras.因此,我将运行 pip3 install tensorflow
和 pip3 install keras
时得到的结果包括在下面:
Because of this, I suspect I have improperly installed either tensorflow or keras. For this reason, I have included below the results I get when I run pip3 install tensorflow
and pip3 install keras
:
➜ kerasTutorial pip3 install tensorflow
Requirement already satisfied: tensorflow in /usr/local/lib/python3.7/site-packages (2.2.0)
Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.14.0)
Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.29.0)
Requirement already satisfied: astunparse==1.6.3 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.6.3)
Requirement already satisfied: wheel>=0.26; python_version >= "3" in /usr/local/lib/python3.7/site-packages (from tensorflow) (0.34.2)
Requirement already satisfied: google-pasta>=0.1.8 in /usr/local/lib/python3.7/site-packages (from tensorflow) (0.2.0)
Requirement already satisfied: h5py<2.11.0,>=2.10.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (2.10.0)
Requirement already satisfied: gast==0.3.3 in /usr/local/lib/python3.7/site-packages (from tensorflow) (0.3.3)
Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.7/site-packages (from tensorflow) (3.2.1)
Requirement already satisfied: protobuf>=3.8.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (3.12.0)
Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.12.1)
Requirement already satisfied: scipy==1.4.1; python_version >= "3" in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.4.1)
Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (0.9.0)
Requirement already satisfied: tensorflow-estimator<2.3.0,>=2.2.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (2.2.0)
Requirement already satisfied: tensorboard<2.3.0,>=2.2.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (2.2.1)
Requirement already satisfied: keras-preprocessing>=1.1.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.1.2)
Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.18.4)
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.1.0)
Requirement already satisfied: setuptools in /Users/mathewlewis/Library/Python/3.7/lib/python/site-packages (from protobuf>=3.8.0->tensorflow) (46.4.0)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (2.23.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (3.2.2)
Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (1.15.0)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (0.4.1)
Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (1.0.1)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (1.6.0.post3)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow) (2020.4.5.1)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow) (2.9)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow) (1.25.9)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.7/site-packages (from markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow) (1.6.0)
Requirement already satisfied: rsa<4.1,>=3.1.4 in /usr/local/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow) (4.0)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow) (4.1.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow) (0.2.8)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow) (1.3.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow) (3.1.0)
Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.7/site-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow) (3.1.0)
➜ kerasTutorial pip3 install keras
Requirement already satisfied: keras in /usr/local/lib/python3.7/site-packages (2.3.1)
Requirement already satisfied: keras-applications>=1.0.6 in /usr/local/lib/python3.7/site-packages (from keras) (1.0.8)
Requirement already satisfied: h5py in /usr/local/lib/python3.7/site-packages (from keras) (2.10.0)
Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.7/site-packages (from keras) (1.1.2)
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.7/site-packages (from keras) (1.18.4)
Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.7/site-packages (from keras) (1.14.0)
Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python3.7/site-packages (from keras) (1.4.1)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/site-packages (from keras) (5.3.1)
以防万一,我使用的是2015年中的Macbook Pro,运行macOS catalina版本10.15.4
In case this is relavent, I am on a mid 2015 macbook pro, running macos catalina version 10.15.4
这是我最好的尝试,包括所有相关信息.如果您想了解其他内容,请告诉我,我会尽力为您找到答案.非常感谢您抽出宝贵的时间阅读这个冗长的问题.
This was my best attempt at including all relavent information. If you'd like to know something else please let me know and I'll try and find that out for you. Thanks very much for taking the time to read this long question.
我也尝试过直接使用keras而不是从tensorflow导入.为此,我从keras.models中导入 import Sequential
并收到此错误:
I have also tried using keras directly rather than importing from tensorflow. To do so I ran from keras.models import Sequential
and got this error:
➜ kerasTutorial python3
Python 3.7.7 (default, Mar 10 2020, 15:43:33)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from keras.models import Sequential
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/keras/__init__.py", line 3, in <module>
from . import utils
File "/usr/local/lib/python3.7/site-packages/keras/utils/__init__.py", line 6, in <module>
from . import conv_utils
File "/usr/local/lib/python3.7/site-packages/keras/utils/conv_utils.py", line 9, in <module>
from .. import backend as K
File "/usr/local/lib/python3.7/site-packages/keras/backend/__init__.py", line 1, in <module>
from .load_backend import epsilon
File "/usr/local/lib/python3.7/site-packages/keras/backend/load_backend.py", line 90, in <module>
from .tensorflow_backend import *
File "/usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 5, in <module>
import tensorflow as tf
File "/usr/local/lib/python3.7/site-packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 64, in <module>
from tensorflow.python.framework.framework_lib import * # pylint: disable=redefined-builtin
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/framework_lib.py", line 25, in <module>
from tensorflow.python.framework.ops import Graph
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 64, in <module>
from tensorflow.python.platform import app
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/platform/app.py", line 23, in <module>
from absl.app import run as _run
File "/usr/local/lib/python3.7/site-packages/absl/app.py", line 35, in <module>
import pdb
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pdb.py", line 76, in <module>
import code
File "/Users/mathewlewis/Documents/Y4S3/Crypto/kerasTutorial/code.py", line 4, in <module>
from tensorflow.keras.models import Sequential
File "/usr/local/lib/python3.7/site-packages/tensorflow/keras/__init__.py", line 14, in <module>
from . import activations
File "/usr/local/lib/python3.7/site-packages/tensorflow/keras/activations/__init__.py", line 10, in <module>
from tensorflow.python.keras.activations import deserialize
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/__init__.py", line 27, in <module>
from tensorflow.python.keras import models
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/models.py", line 23, in <module>
from tensorflow.python.keras import backend as K
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/backend.py", line 36, in <module>
from tensorflow.python.client import session as session_module
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 38, in <module>
from tensorflow.python.framework import sparse_tensor
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/sparse_tensor.py", line 29, in <module>
from tensorflow.python.framework import constant_op
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 324, in <module>
ops.register_tensor_conversion_function(
AttributeError: module 'tensorflow.python.framework.ops' has no attribute 'register_tensor_conversion_function'
>>>
此外,我没有使用conda.我想要一个使用python3和可以使用pip3安装的软件包的解决方案.
Also, I am not using conda. I'd like to have a solution which uses python3 and packages which can be installed using pip3.
推荐答案
从您的问题中开始安装tensorflow 2.2
From your question you are installing tensorflow 2.2
Requirement already satisfied: tensorflow in /usr/local/lib/python3.7/site-packages (2.2.0)
事实上,如果您使用tf 2.2作为后端并想使用keras API,则不应安装 keras
,而应使用 from tensorflow import keras
In fact, if you are using tf 2.2 as backend and want to use keras API, you should not install keras
but should use from tensorflow import keras
因为keras软件包不支持tensorflow 2.2.如果您查看keras官方发行版 https://github.com/keras-team/keras/releases,它仅支持tensorflow 2.0.
Because keras package does not support tensorflow 2.2. If you check the keras official release https://github.com/keras-team/keras/releases, it supports up to tensorflow 2.0 only.
因此,您应该同时卸载 tensorflow
和 keras
,并且仅重新安装 tensorflow
v2.2.然后通过
Therefore, you should uninstall both tensorflow
and keras
, and only reinstall tensorflow
v2.2. Then test by
from tensorflow.keras.models import Sequential
您应该能够运行您的教程
You should be able to run your tutorial
这篇关于无法从tensorflow导入keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!