LoginSignup
50
34

More than 5 years have passed since last update.

Anaconda PromptでのTensorFlowの導入に苦戦した話

Last updated at Posted at 2019-02-14

何がしたいか

ディープラーニングの勉強がしたくて、以下の本を手にしました。

いちばんやさしい ディープラーニング 入門教室

これにしたがって環境構築を行っていました。
問題が発生するまでのインストール手順などは省略します。

  • 使用OS:Windows10
  • 手順①:Python3のインストール
  • 手順②:Anacondaのインストール
  • 手順③:TensorFlowのインストール ←ここで引っかかった

何が引っかかったか

書籍の中で紹介されているTensorFlowのインストール方法は、

AnacondaPrompt
>pip install tensorflow

で、これをやってみるとなんか問題なくインストールできたように見えるんですよね。
特にエラーも吐き出しませんでした。

その後、書籍に従いTensorFlowが呼び出せるかの確認をするため、以下のように打ち込んでいきます。

AnacondaPrompt
>python
>>>import tensorflow as tf
>>>hello = tf.constant('Hello, TensorFlow!')
>>>sess = tf.Session()
>>>print(sess.run(hello))
b'Hello, TensorFlow!' #←これが表示されれば成功らしい

よっしゃやったるで、と意気揚々と打ち込んでいくと、以下のようなエラーが。

AnacondaPrompt
(base) C:\Users\shinsenman\keras>python
Python 3.7.2 (default, Feb 11 2019, 14:11:50) [MSC v.1915 64 bit (AMD64)] :: Anaconda custom (64-bit) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
ImportError: numpy.core.multiarray failed to import

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 980, in _find_and_load
SystemError: <class '_frozen_importlib._ModuleLockManager'> returned a result with an error set
ImportError: numpy.core._multiarray_umath failed to import
ImportError: numpy.core.umath failed to import
2019-02-14 16:30:03.514448: F tensorflow/python/lib/core/bfloat16.cc:675] Check failed: PyBfloat16_Type.tp_base != nullptr

(base) C:\Users\shinsenman\keras>

※kerasは今後この書籍に従って使用するフレームワークの名前です。

困りました。とりあえずnumpy.core._multiarray_umathって名前のモジュールが見つからんぞと言ってるなと判断。
検索してみると、numpyのバージョンが古いとエラーが出るみたいだったので(その時見たサイトはこちら)、とりあえずそれに従いnumpyをアップグレードしてみることに。

しかしうまくいかない。しかも今度はもっと長文のエラーを吐いてきた。

AnacondaPrompt
>>> import tensorflow as tf
Traceback (most recent call last):
  File "C:\Users\shinsenman\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 16, in <module>
    from . import multiarray
  File "C:\Users\shinsenman\Anaconda3\lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
    from . import overrides
  File "C:\Users\shinsenman\Anaconda3\lib\site-packages\numpy\core\overrides.py", line 6, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed: 指定されたモジュールが見つかりません。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\shinsenman\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "C:\Users\shinsenman\Anaconda3\lib\site-packages\tensorflow\python\__init__.py", line 47, in <module>
    import numpy as np
  File "C:\Users\shinsenman\Anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import core
  File "C:\Users\shinsenman\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 47, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
  your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
  1. Check that you are using the Python you expect (you're using C:\Users\shinsenman\Anaconda3\python.exe),
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy versions you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

     Note: this error has many possible causes, so please don't comment on
     an existing issue about this - open a new one instead.

うーんなんとなく言ってることは分かるようで分からない。
エラーを検索にかけると、「PATHが通っていない」とか、「Anacondaのインストール時の初期設定でPATHが……云々(イマイチわからんかった)」とかあって、でも調べてみたらPATHは通ってるっぽいし、DLLが見つからんと言われても、どのDLLなのかが読み取れず、お手上げ状態に。

どうやって解決したか

結論から言うとconda install tensorflowで再インストールしたら解決しました。
確かに、上記エラーを検索する中でpipとcondaが混同するとうまくいかなくなるとかいう記事は見ていたのですが、この問題がそれに該当するとは思ってなくてスルーしてました。
とりあえず、pipでインストールした分をアンインストールした方がいいかなと判断し、そのあとにcondaでインストールしました。

(base) C:\Users\shinsenman\keras>pip uninstall tensorflow
Uninstalling tensorflow-1.13.0rc1:
  Would remove:
    c:\users\shinsenman\anaconda3\lib\site-packages\tensorflow-1.13.0rc1.dist-info\*
    c:\users\shinsenman\anaconda3\lib\site-packages\tensorflow\*
    c:\users\shinsenman\anaconda3\scripts\freeze_graph.exe
    c:\users\shinsenman\anaconda3\scripts\saved_model_cli.exe
    c:\users\shinsenman\anaconda3\scripts\tensorboard.exe
    c:\users\shinsenman\anaconda3\scripts\tf_upgrade_v2.exe
    c:\users\shinsenman\anaconda3\scripts\tflite_convert.exe
    c:\users\shinsenman\anaconda3\scripts\toco.exe
    c:\users\shinsenman\anaconda3\scripts\toco_from_protos.exe
Proceed (y/n)? y
  Successfully uninstalled tensorflow-1.13.0rc1

(base) C:\Users\shinsenman\keras>conda install tensorflow

結構時間かかったんですが、インストールが終わり、無事にTensorFlowの呼び出しに成功。やったね!

結論

pipでインストールしてうまくいかなかった場合は、condaで再インストールしてみよう。

参考

このTensorFlowの呼び出しに関して、なんでこの記述をするんだろうっていうのが分からなかったので調べてみると、わかりやすくまとまっている記事を発見したのでリンクを貼っておきます。記事はこちらから

50
34
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50
34