Cucco’s Compute Hack

コンピュータ関係の記事を書いていきます。

anacondaでTensorFlow環境の準備

何が起きているかわからないが以下で成功した。

(base) C:\>conda create -n tensorflow_gpuenv tensorflow-gpu

いろいろインストールしていい?と質問があるのでyを回答。

以上で使えました。

環境を使うときのおまじないは以下。

# To activate this environment, use
#
#     $ conda activate tensorflow_gpuenv
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Install TensorFlow with pip  |  TensorFlow にあった動作確認プログラムを動かしてみる。

(tensorflow_gpuenv) C:\>python -c "import tensorflow as tf; print(tf.__version__)"
1.11.0

TensorFlow にあったサンプルのプログラムをC:\tensor\hello_tensor.pyに保存して実行するとこんな感じ。

(tensorflow_gpuenv) C:\>python C:\tensor\hello_tensor.py
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 1s 0us/step
Epoch 1/5
2018-10-27 12:06:34.981339: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2018-10-27 12:06:36.063940: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 0 with properties:
name: GeForce GTX 1050 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.30GiB
2018-10-27 12:06:36.070639: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1490] Adding visible gpu devices: 0
2018-10-27 12:06:36.586738: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-10-27 12:06:36.591156: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977]      0
2018-10-27 12:06:36.593388: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0:   N
2018-10-27 12:06:36.595640: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1103] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3011 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
60000/60000 [==============================] - 9s 143us/step - loss: 0.2009 - acc: 0.9416
Epoch 2/5
60000/60000 [==============================] - 6s 93us/step - loss: 0.0803 - acc: 0.9755
Epoch 3/5
60000/60000 [==============================] - 6s 93us/step - loss: 0.0518 - acc: 0.9836
Epoch 4/5
60000/60000 [==============================] - 6s 94us/step - loss: 0.0365 - acc: 0.9888
Epoch 5/5
60000/60000 [==============================] - 6s 94us/step - loss: 0.0268 - acc: 0.9910
10000/10000 [==============================] - 0s 45us/step

一方で、失敗例。tensorFlowのPipインストール手順で環境構築したら、動作確認プログラムがモジュールが足りない、となった。
何が悪いんだろう?

(venv) (base) C:\>python -c "import tensorflow as tf; print(tf.__version__)"
Traceback (most recent call last):
  File "C:\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\venv\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:\venv\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "C:\venv\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 "<string>", line 1, in <module>
  File "C:\venv\lib\site-packages\tensorflow\__init__.py", line 22, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "C:\venv\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\venv\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:\venv\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "C:\venv\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.