Windows 10上のTensorflow(Keras)でGPUを有効にする

Windows 10でディープラーニングの勉強をする際にKerasとTensorflowを少し使っているのですが、自分ちのPCが一応ゲーミングPCなので、GPUを有効にしてみた。

ほとんど、

www.kkaneko.jp

を参考にさせてもらったのですが、一部違いとしては、Visual Studio Community 2019のC++によるデスクトップ開発をインストールしないと、CUDAをインストールするときに、CUDA Visual Studio Integration非サポートの警告が表示されました。あと、Tensorflowのインストールは、

www.tensorflow.org

のとおり、

pip install tensorflow

だけで、特にtensorflow-gpuを着けなくても、GPUは有効化されました。

from tensorflow.python.client import device_lib
device_lib.list_local_devices()
[name: "/device:CPU:0"
 device_type: "CPU"
 memory_limit: 268435456
 locality {
 }
 incarnation: 5082371811293174043,
 name: "/device:XLA_CPU:0"
 device_type: "XLA_CPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 1801013567918152845
 physical_device_desc: "device: XLA_CPU device",
 name: "/device:GPU:0"
 device_type: "GPU"
 memory_limit: 5060693856
 locality {
   bus_id: 1
   links {
   }
 }
 incarnation: 3644703809891559097
 physical_device_desc: "device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1",
 name: "/device:XLA_GPU:0"
 device_type: "XLA_GPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 2100757963281322856
 physical_device_desc: "device: XLA_GPU device"]

kerasでMNISTの分類の学習時間の計測をしてみたところ、モデルによるとは思いますが*1

  • GPUあり:実行時間:144.0960705280304秒
  • GPUなし:実行時間:294.20286107063293秒

と速くなりました。

*1:とある学習サイトのコピペモデルなので、版権の関係上ここには載せません

XGBoost

Windows 10にanacondaでXGBoostをインストールしようとしたら、エラーが出た。

(base) C:\Users\hoge>conda install -c anaconda py-xgboost
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: -
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
Examining py-xgboost:  40%|████████████████████████▊                                     | 2/5 [00:00<00:00,  8.30it/s]/failed

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - py-xgboost -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.5,<3.6.0a0']

Your python: python=3.8

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

python 3.8は未対応とのこと。 仕方がないので、pipでインストールした。

(base) C:\Users\hoge>pip install xgboost
Collecting xgboost
  Downloading xgboost-1.3.0.post0-py3-none-win_amd64.whl (95.2 MB)
     |████████████████████████████████| 95.2 MB 63 kB/s
Requirement already satisfied: scipy in d:\users\tkonishi\miniconda3\lib\site-packages (from xgboost) (1.5.2)
Requirement already satisfied: numpy in d:\users\tkonishi\miniconda3\lib\site-packages (from xgboost) (1.18.5)
Requirement already satisfied: numpy in d:\users\tkonishi\miniconda3\lib\site-packages (from xgboost) (1.18.5)
Installing collected packages: xgboost
Successfully installed xgboost-1.3.0.post0

ImportError: DLL load failed while importing _pywrap_tensorflow_internal: 指定されたモジュールが見つかりません。

Windows 10にtensorflow 2.3.1をインストールした。

 (base) C:\> pip install tensorflow

tensorflowをインポートしたところで、Import Error発生。

import tensorflow as tf
(略)
C:\Users\hoge\miniconda3\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 while importing _pywrap_tensorflow_internal: 指定されたモジュールが見つかりません。

During handling of the above exception, another exception occurred:
(略)

対処の正解は、こちらのとおり「visual studio 2015, 2017, 2019ランタイムのインストール」でした。 maxigundan.com

import tensorflow as tf

2.3.1

graphviz: FileNotFoundError: [WinError 2] 指定されたファイルが見つかりません。

Windows 10にgraphvizをインストールしてPythonから使おうとすると

(略)
FileNotFoundError: [WinError 2] 指定されたファイルが見つかりません。
(略)
ExecutableNotFound: failed to execute ['dot', '-Tsvg'], make sure the Graphviz executables are on your systems' PATH
<graphviz.files.Source at 0x1956e9d07c0>

が出た。

 (base) C:\>dot -V
dot - graphviz version 2.38.0 (20140413.2041)

コマンドラインからdotへのPATHはとおっているので、

qiita.com

のとおりなのだけど、環境変数に追加するパスがbinまでではなく、graphvizまでとおす必要があった

(base) C:>echo %PATH%
(途中略);C:\Users\hoge\miniconda3\Library\bin\graphviz;(この後も略)

追加する環境変数は、ユーザー環境変数でもシステム環境変数のどちらでもよいみたい。

悪魔の物理学 -情報熱力学入門-

www.youtube.com

YouTube見ていてたまたま見つけた、静岡大学サイエンスカフェの動画。2016年。

マクスウェルの悪魔を題材ににして、情報熱力学の概要を1時間で紹介。マクスウェルの悪魔を説明する前に、前提となる熱力学と統計力学の初歩を丁寧に説明して、初めて聴いても無理のない構成にしており好感が持てる。マクスウェルの悪魔の解決の説明に、シラードのエンジンもランダウアーの原理もでてこないけど、細かい説明は不要な場だからいいのかもしれない。相互情報量については、もう少し説明が欲しかったと思うのはないものねだりでしょうか。