使用 tf 别名导入 tensorflow 时无法导入 keras 模块
Unable to import keras modules when importing tensorflow with tf alias
我在这里遇到了一件奇怪的事情。我已经在 Ubuntu 20.04 本地安装了 Tensorflow。
我在 vscode 中创建了一个虚拟环境(我不使用 conda)。
tensorflow 安装是使用 pip 安装的,如官方 Tensorflow 站点所示。
(.venv) (base) mundsen1:~/dev/Python/ImageProcessing$ python
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-09-19 11:32:08.997073:
tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could
not load dynamic library 'libcudart.so.11.0'; dlerror:
libcudart.so.11.0: cannot open shared object file: No such file or
directory
2021-09-19 11:32:08.997096: 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.
!!!!!!!!上面的警告消息不是问题,因为我没有 GPU。
!!!!!!!!奇怪的是:
>>> from tf.keras.models import Model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tf'
!!!!虽然我可以执行...
>>> tf.__version__
'2.6.0'
>>> tf.keras.__version__
'2.6.0'
>>> tf.keras.models
<module 'keras.api._v2.keras.models' from /home/alexandre/dev/Python/ImageProcessing/.venv/lib/python3.8/site-packages/keras/api/_v2/keras/models/__init__.py'>
>>> dir(tf.keras.models)
['Model', 'Sequential', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_sys', 'clone_model', 'load_model', 'model_from_config', 'model_from_json', 'model_from_yaml', 'save_model']
>>> for func in dir(tf.keras.models):
... print(func)
...
Model
Sequential
__builtins__
__cached__
__doc__
__file__
__loader__
__name__
__package__
__path__
__spec__
_sys
clone_model
load_model
model_from_config
model_from_json
model_from_yaml
save_model
from tensorflow.keras.models import Model, load_model
!!!!没问题!
!!!!但是当我使用 tf 别名进行模块导入时,它失败了...
>>> from tf.keras.models import Model, load_model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tf'
>>>
所以我可以使用 tf 变量,只要我不使用它来导入模块。
有人知道这里发生了什么吗?
它看起来是一个纯粹的“Python”问题,我在 Colab Notebook 中执行相同的指令时遇到同样的问题。
尝试使用 from tensorflow.keras import <module>
将 tensorflow 导入为 tf 为您的文件创建一个 tensorflow as tf 的别名 () 仅。当您从名为 tf 的库导入 keras 时,该库不存在。
这不是“Python 问题”,希望对您有所帮助。
我在这里遇到了一件奇怪的事情。我已经在 Ubuntu 20.04 本地安装了 Tensorflow。 我在 vscode 中创建了一个虚拟环境(我不使用 conda)。 tensorflow 安装是使用 pip 安装的,如官方 Tensorflow 站点所示。
(.venv) (base) mundsen1:~/dev/Python/ImageProcessing$ python
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-09-19 11:32:08.997073:
tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could
not load dynamic library 'libcudart.so.11.0'; dlerror:
libcudart.so.11.0: cannot open shared object file: No such file or
directory
2021-09-19 11:32:08.997096: 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.
!!!!!!!!上面的警告消息不是问题,因为我没有 GPU。
!!!!!!!!奇怪的是:
>>> from tf.keras.models import Model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tf'
!!!!虽然我可以执行...
>>> tf.__version__
'2.6.0'
>>> tf.keras.__version__
'2.6.0'
>>> tf.keras.models
<module 'keras.api._v2.keras.models' from /home/alexandre/dev/Python/ImageProcessing/.venv/lib/python3.8/site-packages/keras/api/_v2/keras/models/__init__.py'>
>>> dir(tf.keras.models)
['Model', 'Sequential', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_sys', 'clone_model', 'load_model', 'model_from_config', 'model_from_json', 'model_from_yaml', 'save_model']
>>> for func in dir(tf.keras.models):
... print(func)
...
Model
Sequential
__builtins__
__cached__
__doc__
__file__
__loader__
__name__
__package__
__path__
__spec__
_sys
clone_model
load_model
model_from_config
model_from_json
model_from_yaml
save_model
from tensorflow.keras.models import Model, load_model
!!!!没问题!
!!!!但是当我使用 tf 别名进行模块导入时,它失败了...
>>> from tf.keras.models import Model, load_model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tf'
>>>
所以我可以使用 tf 变量,只要我不使用它来导入模块。
有人知道这里发生了什么吗?
它看起来是一个纯粹的“Python”问题,我在 Colab Notebook 中执行相同的指令时遇到同样的问题。
尝试使用 from tensorflow.keras import <module>
将 tensorflow 导入为 tf 为您的文件创建一个 tensorflow as tf 的别名 (
这不是“Python 问题”,希望对您有所帮助。