1. 程式人生 > >【02】如何檢驗 tensorflow 是否安裝正確

【02】如何檢驗 tensorflow 是否安裝正確

思路:

找一個 demo.py ,執行,檢驗

# 引入 tensorflow 模組 
import tensorflow as tf 
# 建立一個整型常量,即 0 階 Tensor
t0 = tf.constant(3, dtype=tf.int32) 
# 建立一個浮點數的一維陣列,即 1 階 Tensor
t1 = tf.constant([3., 4.1, 5.2], dtype=tf.float32) 
# 建立一個字串的2x2陣列,即 2 階 Tensor
t2 = tf.constant([['Apple', 'Orange'], ['Potato', 'Tomato']], dtype=tf.string) 
# 建立一個 2x3x1 陣列,即 3 階張量,資料型別預設為整型 
t3 = tf.constant([[[5], [6], [7]], [[4], [3], [2]]]) 
# 列印上面建立的幾個 Tensor 
print(t0)
print(t1)
print(t2)
print(t3)

執行 demo.py

問題#1 找不到指定模組?

>>> 
==== RESTART: D:\下載\forGPU--CUDA--TensorFlow--CUDNN\tensorflow_demo_01.py ====
Traceback (most recent call last):
  File "D:\ProgramFiles\python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\ProgramFiles\python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\ProgramFiles\python36\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 "D:\ProgramFiles\python36\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "D:\ProgramFiles\python36\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: 找不到指定的模組。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\下載\forGPU--CUDA--TensorFlow--CUDNN\tensorflow_demo_01.py", line 2, in <module>
    import tensorflow as tf
  File "D:\ProgramFiles\python36\lib\site-packages\tensorflow\__init__.py", line 22, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "D:\ProgramFiles\python36\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "D:\ProgramFiles\python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "D:\ProgramFiles\python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\ProgramFiles\python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\ProgramFiles\python36\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 "D:\ProgramFiles\python36\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "D:\ProgramFiles\python36\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: 找不到指定的模組。


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

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

安裝 tensorflow

D:\下載>pip install tensorflow
Collecting tensorflow
  Downloading https://files.pythonhosted.org/packages/c1/e1/a5693a158f3867417d7b50bd0514c83304706242fae74463705b8c373777/tensorflow-1.11.0-cp36-cp36m-win_amd64.whl (46.9MB)
    72% |███████████████████████         | 34.1MB 59kB/s eta 0:03:35Exception:
Traceback (most recent call last):
  File "d:\programfiles\python36\lib\site-packages\pip\_vendor\urllib3\response.py", line 331, in _error_catcher
    yield
  File "d:\programfiles\python36\lib\site-packages\pip\_vendor\urllib3\response.py", line 413, in read
    data = self._fp.read(amt)
  File "d:\programfiles\python36\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 62, in read
    data = self.__fp.read(amt)
  File "d:\programfiles\python36\lib\http\client.py", line 449, in read
    n = self.readinto(b)
  File "d:\programfiles\python36\lib\http\client.py", line 493, in readinto
    n = self.fp.readinto(b)
  File "d:\programfiles\python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "d:\programfiles\python36\lib\ssl.py", line 1012, in recv_into
    return self.read(nbytes, buffer)
  File "d:\programfiles\python36\lib\ssl.py", line 874, in read
    return self._sslobj.read(len, buffer)
  File "d:\programfiles\python36\lib\ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\cli\base_command.py", line 143, in main
    status = self.run(options, args)
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\commands\install.py", line 318, in run
    resolver.resolve(requirement_set)
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\resolve.py", line 102, in resolve
    self._resolve_one(requirement_set, req)
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\resolve.py", line 256, in _resolve_one
    abstract_dist = self._get_abstract_dist_for(req_to_install)
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\resolve.py", line 209, in _get_abstract_dist_for
    self.require_hashes
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\operations\prepare.py", line 283, in prepare_linked_requirement
    progress_bar=self.progress_bar
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\download.py", line 836, in unpack_url
    progress_bar=progress_bar
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\download.py", line 673, in unpack_http_url
    progress_bar)
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\download.py", line 897, in _download_http_url
    _download_url(resp, link, content_file, hashes, progress_bar)
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\download.py", line 617, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\utils\hashes.py", line 48, in check_against_chunks
    for chunk in chunks:
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\download.py", line 585, in written_chunks
    for chunk in chunks:
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\utils\ui.py", line 159, in iter
    for x in it:
  File "d:\programfiles\python36\lib\site-packages\pip\_internal\download.py", line 574, in resp_read
    decode_content=False):
  File "d:\programfiles\python36\lib\site-packages\pip\_vendor\urllib3\response.py", line 465, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "d:\programfiles\python36\lib\site-packages\pip\_vendor\urllib3\response.py", line 430, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "d:\programfiles\python36\lib\contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "d:\programfiles\python36\lib\site-packages\pip\_vendor\urllib3\response.py", line 336, in _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

D:\下載>

本地安裝

 pip install tensorflow-1.11.0-cp36-cp36m-win_amd64.whl 

完成後再次執行 demo.py

>>> 
==== RESTART: D:\下載\forGPU--CUDA--TensorFlow--CUDNN\tensorflow_demo_01.py ====
Tensor("Const:0", shape=(), dtype=int32)
Tensor("Const_1:0", shape=(3,), dtype=float32)
Tensor("Const_2:0", shape=(2, 2), dtype=string)
Tensor("Const_3:0", shape=(2, 3, 1), dtype=int32)
>>> 

嗯,這個表明 tensorflow 安裝成功了。

下個問題,如何檢驗 tensorflow-gpu 安裝成功了?