【问题标题】:TensorFlow 2.2.0 not importing Sequential from tensorflow.keras.models PyVer==3.8TensorFlow 2.2.0 未从 tensorflow.keras.models PyVer==3.8 导入 Sequential
【发布时间】:2020-09-30 07:40:40
【问题描述】:

在尝试运行后出现的错误下方,如果回滚顶部的 Tensorflow 2.0 版本,该错误不会持续存在。 在 Jupyter Notebook 中自动完成时,所有部分都正常显示,只是无法加载。 是否有任何可能的解决方法来避免回滚到旧版本?

在:

from tensorflow.keras.models import Sequential

输出:

ImportError                               Traceback (most recent call last)
~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     57 
---> 58   from tensorflow.python.pywrap_tensorflow_internal import *
     59 

~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in <module>
     27             return _mod
---> 28     _pywrap_tensorflow_internal = swig_import_helper()
     29     del swig_import_helper

~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
     23             try:
---> 24                 _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
     25             finally:

~\Anaconda3\lib\imp.py in load_module(name, file, filename, details)
    241         else:
--> 242             return load_dynamic(name, filename, file)
    243     elif type_ == PKG_DIRECTORY:

~\Anaconda3\lib\imp.py in load_dynamic(name, path, file)
    341             name=name, loader=loader, origin=path)
--> 342         return _load(spec)
    343 

ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-4-f05745da3b73> in <module>
----> 1 from tensorflow.keras.models import Sequential

~\Anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
     39 import sys as _sys
     40 
---> 41 from tensorflow.python.tools import module_util as _module_util
     42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     43 

~\Anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
     48 import numpy as np
     49 
---> 50 from tensorflow.python import pywrap_tensorflow
     51 
     52 # Protocol buffers

~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     67 for some common reasons and solutions.  Include the entire stack trace
     68 above this error message when asking for help.""" % traceback.format_exc()
---> 69   raise ImportError(msg)
     70 
     71 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long

ImportError: Traceback (most recent call last):
  File "C:\Users\Michele\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Michele\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Michele\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\Michele\Anaconda3\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\Michele\Anaconda3\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

【问题讨论】:

    标签: python-3.x tensorflow keras tf.keras tensorflow2.x


    【解决方案1】:

    简单使用:

    from tensorflow.keras import Sequential
    

    它会正常工作。
    干杯

    【讨论】:

      【解决方案2】:

      你可以在TensorFlow Keras中做这些顺序模型的例子。你也可以根据你的情况添加层。

      
      from tensorflow import keras
      from tensorflow.keras import layers
      from tensorflow.keras import Sequential
      
      model = Sequential()
      # Adds a densely-connected layer with 64 units to the model:
      model.add(layers.Dense(64, activation='relu'))
      # Add another:
      model.add(layers.Dense(64, activation='relu'))
      # Add an output layer with 10 output units:
      model.add(layers.Dense(10))
      
      

      import tensorflow as tf
      from tensorflow import keras
      from tensorflow.keras import layers
      
      model = tf.keras.Sequential()
      # Adds a densely-connected layer with 64 units to the model:
      model.add(layers.Dense(64, activation='relu'))
      # Add another:
      model.add(layers.Dense(64, activation='relu'))
      # Add an output layer with 10 output units:
      model.add(layers.Dense(10))
      
      

      【讨论】:

        【解决方案3】:

        截至最新的 TensorFlow 版本 >= 2.2.0,开发人员已修改(使其更容易)加载模型/层等的方式。

        因此,Sequential 类不再在包tensorflow.keras.models 中,而是在tensorflow.keras

        【讨论】:

        • 毕竟就是这么简单,感谢您的澄清。我在任何地方的文档中都找不到,你能分享它的发布位置吗?
        • 不,我在空闲时间自己尝试过,看看如何修改;同时,TensorFlow 上的新文档很可能会更新为新的实现示例
        猜你喜欢
        • 2022-12-24
        • 2021-10-02
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        • 2020-04-10
        • 1970-01-01
        • 1970-01-01
        • 2023-02-09
        相关资源
        最近更新 更多