2022年5月5日 星期四

TensorFlow 升版至 v2.6 出現 'tensorflow' has no attribute 'compat' 等錯誤訊息

晚上把閒置已久的 INHON Carbonbook 筆電拿出來更新 Win10, 然後更新 TensoeFlow, 從原本的 1.5 版 (好久以前的版本啊) 提升到最新的 2.6 版 :

D:\>pip install tensorflow -U    
Requirement already satisfied: tensorflow in c:\python36\lib\site-packages (1.5.0)
Collecting tensorflow
  Downloading tensorflow-2.6.2-cp36-cp36m-win_amd64.whl (423.3 MB)
     |██████████                      | 129.0 MB 20 kB/s eta 4:01:46

不知道是甚麼原因下載到一半就出現一堆 ReadTimeoutError 而失敗, 試過幾次都沒成功 :




乾脆直接到 TensorFlow 官網下載 whl 檔來離線安裝 : 


要根據 Python 版本來下載對應之 whl 檔, 我的 Pyhton 是 3.6 版, 所以選擇 2.6.0 版的 TensorFlow whl 檔下載 : 

C:\Users\Tony Huang>python   
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.




然後用 pip install 離線安裝 :

D:\>pip install tensorflow_cpu-2.6.0-cp36-cp36m-win_amd64.whl
Processing d:\tensorflow_cpu-2.6.0-cp36-cp36m-win_amd64.whl
Collecting termcolor~=1.1.0
  Downloading termcolor-1.1.0.tar.gz (3.9 kB)
  Preparing metadata (setup.py) ... done
Collecting keras-preprocessing~=1.1.2
  Downloading Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)
Collecting typing-extensions~=3.7.4
  Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting clang~=5.0
  Downloading clang-5.0.tar.gz (30 kB)
  Preparing metadata (setup.py) ... done
Collecting wrapt~=1.12.1
  Downloading wrapt-1.12.1.tar.gz (27 kB)
  Preparing metadata (setup.py) ... done
Collecting astunparse~=1.6.3
  Downloading astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Collecting protobuf>=3.9.2
  Downloading protobuf-3.19.4-cp36-cp36m-win_amd64.whl (897 kB)
Collecting tensorflow-estimator~=2.6
  Downloading tensorflow_estimator-2.8.0-py2.py3-none-any.whl (462 kB)
Collecting google-pasta~=0.2
  Downloading google_pasta-0.2.0-py3-none-any.whl (57 kB)
Collecting absl-py~=0.10
  Downloading absl_py-0.15.0-py3-none-any.whl (132 kB)
Collecting numpy~=1.19.2
  Downloading numpy-1.19.5-cp36-cp36m-win_amd64.whl (13.2 MB)
Collecting h5py~=3.1.0
  Downloading h5py-3.1.0-cp36-cp36m-win_amd64.whl (2.7 MB)
Collecting six~=1.15.0
  Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting flatbuffers~=1.12.0
  Downloading flatbuffers-1.12-py2.py3-none-any.whl (15 kB)
Collecting keras~=2.6
  Downloading keras-2.8.0-py2.py3-none-any.whl (1.4 MB)
Collecting wheel~=0.35
  Downloading wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Collecting gast==0.4.0
  Downloading gast-0.4.0-py3-none-any.whl (9.8 kB)
Collecting opt-einsum~=3.3.0
  Downloading opt_einsum-3.3.0-py3-none-any.whl (65 kB)
Collecting tensorboard~=2.6
  Downloading tensorboard-2.9.0-py3-none-any.whl (5.8 MB)
Collecting grpcio<2.0,>=1.37.0
  Downloading grpcio-1.46.0-cp36-cp36m-win_amd64.whl (3.5 MB)
Collecting cached-property
  Downloading cached_property-1.5.2-py2.py3-none-any.whl (7.6 kB)
Requirement already satisfied: markdown>=2.6.8 in c:\python36\lib\site-packages (from tensorboard~=2.6->tensorflow-cpu==2.6.0) (2.6.11)
Collecting google-auth-oauthlib<0.5,>=0.4.1
  Downloading google_auth_oauthlib-0.4.6-py2.py3-none-any.whl (18 kB)
Collecting tensorboard-plugin-wit>=1.6.0
  Downloading tensorboard_plugin_wit-1.8.1-py3-none-any.whl (781 kB)
Collecting werkzeug>=1.0.1
  Downloading Werkzeug-2.0.3-py3-none-any.whl (289 kB)
Collecting google-auth<3,>=1.6.3
  Downloading google_auth-2.6.6-py2.py3-none-any.whl (156 kB)
Collecting tensorboard-data-server<0.7.0,>=0.6.0
  Downloading tensorboard_data_server-0.6.1-py3-none-any.whl (2.4 kB)
Collecting setuptools>=41.0.0
  Downloading setuptools-59.6.0-py3-none-any.whl (952 kB)
Collecting requests<3,>=2.21.0
  Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
Collecting pyasn1-modules>=0.2.1
  Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
Collecting rsa<5,>=3.1.4
  Downloading rsa-4.8-py3-none-any.whl (39 kB)
Collecting cachetools<6.0,>=2.0.0
  Downloading cachetools-4.2.4-py3-none-any.whl (10 kB)
Collecting requests-oauthlib>=0.7.0
  Downloading requests_oauthlib-1.3.1-py2.py3-none-any.whl (23 kB)
Collecting certifi>=2017.4.17
  Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
Collecting charset-normalizer~=2.0.0
  Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Requirement already satisfied: idna<4,>=2.5 in c:\python36\lib\site-packages (from requests<3,>=2.21.0->tensorboard~=2.6->tensorflow-cpu==2.6.0) (2.7)
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.9-py2.py3-none-any.whl (138 kB)
Collecting dataclasses
  Downloading dataclasses-0.8-py3-none-any.whl (19 kB)
Collecting pyasn1<0.5.0,>=0.4.6
  Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
Collecting oauthlib>=3.0.0
  Downloading oauthlib-3.2.0-py3-none-any.whl (151 kB)
Building wheels for collected packages: clang, termcolor, wrapt
  Building wheel for clang (setup.py) ... done
  Created wheel for clang: filename=clang-5.0-py3-none-any.whl size=32178 sha256=f833bf8d66d8cb51236eba844bd6abfd359b1757129ad8c50e86b8c6cf8fdfe6
  Stored in directory: c:\users\tony huang\appdata\local\pip\cache\wheels\22\4c\94\0583f60c9c5b6024ed64f290cb2d43b06bb4f75577dc3c93a7
  Building wheel for termcolor (setup.py) ... done
  Created wheel for termcolor: filename=termcolor-1.1.0-py3-none-any.whl size=5718 sha256=23915d6b655639c01972fca92426dbeffe9d0ae13321c303cfe46ca92cf9fa60
  Stored in directory: c:\users\tony huang\appdata\local\pip\cache\wheels\93\2a\eb\e58dbcbc963549ee4f065ff80a59f274cc7210b6eab962acdc
  Building wheel for wrapt (setup.py) ... done
  Created wheel for wrapt: filename=wrapt-1.12.1-py3-none-any.whl size=21439 sha256=78edb83e3b58e25c45a4892a15c748fd1cee8bc6c403dbf5042169454e11c585
  Stored in directory: c:\users\tony huang\appdata\local\pip\cache\wheels\32\42\7f\23cae9ff6ef66798d00dc5d659088e57dbba01566f6c60db63
Successfully built clang termcolor wrapt
Installing collected packages: urllib3, pyasn1, charset-normalizer, certifi, six, rsa, requests, pyasn1-modules, oauthlib, cachetools, requests-oauthlib, google-auth, dataclasses, wheel, werkzeug, tensorboard-plugin-wit, tensorboard-data-server, setuptools, protobuf, numpy, grpcio, google-auth-oauthlib, cached-property, absl-py, wrapt, typing-extensions, termcolor, tensorflow-estimator, tensorboard, opt-einsum, keras-preprocessing, keras, h5py, google-pasta, gast, flatbuffers, clang, astunparse, tensorflow-cpu
  Attempting uninstall: six
    Found existing installation: six 1.11.0
    Uninstalling six-1.11.0:
      Successfully uninstalled six-1.11.0
  Attempting uninstall: requests
    Found existing installation: requests 2.13.0
    Uninstalling requests-2.13.0:
      Successfully uninstalled requests-2.13.0
  Attempting uninstall: wheel
    Found existing installation: wheel 0.30.0
    Uninstalling wheel-0.30.0:
      Successfully uninstalled wheel-0.30.0
  Attempting uninstall: werkzeug
    Found existing installation: Werkzeug 0.14.1
    Uninstalling Werkzeug-0.14.1:
      Successfully uninstalled Werkzeug-0.14.1
  Attempting uninstall: setuptools
    Found existing installation: setuptools 38.5.1
    Uninstalling setuptools-38.5.1:
      Successfully uninstalled setuptools-38.5.1
  Attempting uninstall: protobuf
    Found existing installation: protobuf 3.5.1
    Uninstalling protobuf-3.5.1:
      Successfully uninstalled protobuf-3.5.1
  Attempting uninstall: numpy
    Found existing installation: numpy 1.14.1+mkl
    Uninstalling numpy-1.14.1+mkl:
      Successfully uninstalled numpy-1.14.1+mkl
  Attempting uninstall: absl-py
    Found existing installation: absl-py 0.1.10
    Uninstalling absl-py-0.1.10:
      Successfully uninstalled absl-py-0.1.10
  Attempting uninstall: wrapt
    Found existing installation: wrapt 1.10.11
    Uninstalling wrapt-1.10.11:
      Successfully uninstalled wrapt-1.10.11
  Attempting uninstall: keras
    Found existing installation: Keras 2.1.4
    Uninstalling Keras-2.1.4:
      Successfully uninstalled Keras-2.1.4
Successfully installed absl-py-0.15.0 astunparse-1.6.3 cached-property-1.5.2 cachetools-4.2.4 certifi-2021.10.8 charset-normalizer-2.0.12 clang-5.0 dataclasses-0.8 flatbuffers-1.12 gast-0.4.0 google-auth-2.6.6 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.46.0 h5py-3.1.0 keras-2.8.0 keras-preprocessing-1.1.2 numpy-1.19.5 oauthlib-3.2.0 opt-einsum-3.3.0 protobuf-3.19.4 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.27.1 requests-oauthlib-1.3.1 rsa-4.8 setuptools-59.6.0 six-1.15.0 tensorboard-2.9.0 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.1 tensorflow-cpu-2.6.0 tensorflow-estimator-2.8.0 termcolor-1.1.0 typing-extensions-3.7.4.3 urllib3-1.26.9 werkzeug-2.0.3 wheel-0.37.1 wrapt-1.12.1

很快就完成更新啦! 

但是匯入 TensorFlow 時卻出現找不到動態連結檔 "msvcp140_1.dll" 問題 :

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python36\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Python36\lib\site-packages\tensorflow\python\__init__.py", line 40, in <module>
    from tensorflow.python.eager import context
  File "C:\Python36\lib\site-packages\tensorflow\python\eager\context.py", line 35, in <module>
    from tensorflow.python import pywrap_tfe
  File "C:\Python36\lib\site-packages\tensorflow\python\pywrap_tfe.py", line 28, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 28, in <module>
    self_check.preload_check()
  File "C:\Python36\lib\site-packages\tensorflow\python\platform\self_check.py", line 61, in preload_check
    % " or ".join(missing))
ImportError: Could not find the DLL(s) 'msvcp140_1.dll'. TensorFlow requires that these DLLs be installed in a directory that is named in your %PATH% environment variable. You may install these DLLs by downloading "Microsoft C++ Redistributable for Visual Studio 2015, 2017 and 2019" for your platform from this URL: https://support.microsoft.com/help/2977003/the-latest-supported-visual-c-downloads

我連線此 URL 下載 VC_redist.x64.exe (約 24 MB) 來安裝後, 雖然 DLL 問題消失, 但卻出現 "AttributeError: module 'tensorflow' has no attribute 'compat'" 錯誤訊息 : 

>>> import tensorflow as tf   
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
  File "C:\Python36\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Python36\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python.feature_column import feature_column_lib as feature_column
  File "C:\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column_lib.py", line 22, in <module>
    from tensorflow.python.feature_column.feature_column import *
  File "C:\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 147, in <module>
    from tensorflow.python.layers import base
  File "C:\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 20, in <module>
    from tensorflow.python.keras.legacy_tf_layers import base
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\__init__.py", line 25, in <module>
    from tensorflow.python.keras import models
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\models\__init__.py", line 21, in <module>
    from tensorflow.python.keras._impl.keras.models import load_model
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\__init__.py", line 21, in <module>
    from tensorflow.python.keras._impl.keras import activations
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\activations.py", line 24, in <module>
    from tensorflow.python.keras._impl.keras.utils.generic_utils import deserialize_keras_object
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\utils\__init__.py", line 36, in <module>
    from tensorflow.python.keras._impl.keras.utils.training_utils import multi_gpu_model
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\utils\training_utils.py", line 22, in <module>
    from tensorflow.python.keras._impl.keras.engine.training import Model
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\__init__.py", line 21, in <module>
    from tensorflow.python.keras._impl.keras.engine.topology import get_source_inputs
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\topology.py", line 39, in <module>
    from tensorflow.python.layers import network as tf_network
  File "C:\Python36\lib\site-packages\tensorflow\python\layers\network.py", line 23, in <module>
    from tensorflow.python.estimator import util as estimator_util
  File "C:\Python36\lib\site-packages\tensorflow\python\estimator\util.py", line 26, in <module>
    from tensorflow_estimator.python.estimator import util
  File "C:\Python36\lib\site-packages\tensorflow_estimator\__init__.py", line 8, in <module>
    from tensorflow_estimator._api.v1 import estimator
  File "C:\Python36\lib\site-packages\tensorflow_estimator\_api\v1\estimator\__init__.py", line 8, in <module>
    from tensorflow_estimator._api.v1.estimator import experimental
  File "C:\Python36\lib\site-packages\tensorflow_estimator\_api\v1\estimator\experimental\__init__.py", line 8, in <module>
    from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
  File "C:\Python36\lib\site-packages\tensorflow_estimator\python\estimator\canned\dnn.py", line 27, in <module>
    from tensorflow_estimator.python.estimator import estimator
  File "C:\Python36\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 55, in <module>
    from tensorflow_estimator.python.estimator import util as estimator_util
  File "C:\Python36\lib\site-packages\tensorflow_estimator\python\estimator\util.py", line 74, in <module>
    class _DatasetInitializerHook(tf.compat.v1.train.SessionRunHook):
AttributeError: module 'tensorflow' has no attribute 'compat'     

真是一波平了一波又起, 我在下面這篇文章找到解法, 說是要安裝 tensorflow-estimator 2.1 版 


D:\>pip install tensorflow-estimator==2.1.*
Collecting tensorflow-estimator==2.1.*
  Downloading tensorflow_estimator-2.1.0-py2.py3-none-any.whl (448 kB)
Installing collected packages: tensorflow-estimator
  Attempting uninstall: tensorflow-estimator
    Found existing installation: tensorflow-estimator 2.8.0
    Uninstalling tensorflow-estimator-2.8.0:
      Successfully uninstalled tensorflow-estimator-2.8.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow-cpu 2.6.0 requires tensorflow-estimator~=2.6, but you have tensorflow-estimator 2.1.0 which is incompatible.
Successfully installed tensorflow-estimator-2.1.0

雖然安裝成功了, 但卻提示說 tensorflow 2.6 版要配 tensorflow-estimator 2.6 以上, 所以我又重新安裝 : 

D:\>pip install tensorflow-estimator~=2.6 
Collecting tensorflow-estimator~=2.6
  Using cached tensorflow_estimator-2.8.0-py2.py3-none-any.whl (462 kB)
Installing collected packages: tensorflow-estimator
  Attempting uninstall: tensorflow-estimator
    Found existing installation: tensorflow-estimator 2.1.0
    Uninstalling tensorflow-estimator-2.1.0:
      Successfully uninstalled tensorflow-estimator-2.1.0
Successfully installed tensorflow-estimator-2.8.0

但又蹦出找不到 Layers 問題 : 

>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python36\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Python36\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python.feature_column import feature_column_lib as feature_column
  File "C:\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column_lib.py", line 22, in <module>
    from tensorflow.python.feature_column.feature_column import *
  File "C:\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 147, in <module>
    from tensorflow.python.layers import base
  File "C:\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 20, in <module>
    from tensorflow.python.keras.legacy_tf_layers import base
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\__init__.py", line 25, in <module>
    from tensorflow.python.keras import models
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\models\__init__.py", line 21, in <module>
    from tensorflow.python.keras._impl.keras.models import load_model
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\__init__.py", line 21, in <module>
    from tensorflow.python.keras._impl.keras import activations
  File "C:\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\activations.py", line 25, in <module>
    from tensorflow.python.layers.base import Layer
ImportError: cannot import name 'Layer'

好煩啊! 決定不再玩了, 明天移除 TensorFlow 2.6, 降版為 2.0 或 2.1 就好. 

沒有留言 :