LoginSignup
3
5
新規開発や新技術の検証、導入にまつわる記事を投稿しよう!

dockerで機械学習(11) with anaconda(11)「[第2版]Python機械学習プログラミング 達人データサイエンティストによる理論と実践」Sebastian Raschka, Vahid Mirjalili 著

Last updated at Posted at 2018-10-08

1.すぐに利用したい方へ(as soon as)

[第2版]Python機械学習プログラミング 達人データサイエンティストによる理論と実践 Sebastian Raschka, Vahid Mirjalili 著

1117101099-520x.jpg

https://book.impress.co.jp/books/1117101099
<この項は書きかけです。順次追記します。>

docker

dockerを導入し、Windows, Macではdockerを起動しておいてください。
Windowsでは、BiosでIntel Virtualizationをenableにしないとdockerが起動しない場合があります。
また、セキュリティの警告などが出ることがあります。

docker pull and run

現在、下記のdocker hubの登録に失敗しています。今、しばらくお待ちください。

$ docker pull kaizenjapan/anaconda-Sebastian 

$ docker run -it kaizenjapan/anaconda-Sebastian  /bin/bash

以下のshell sessionでは
(base) root@f19e2f06eabb:/#は入力促進記号(comman prompt)です。実際には数字の部分が違うかもしれません。この行の#の右側を入力してください。
それ以外の行は出力です。出力にエラー、違いがあれば、コメント欄などでご連絡くださると幸いです。
それぞれの章のフォルダに移動します。

dockerの中と、dockerを起動したOSのシェルとが表示が似ている場合には、どちらで捜査しているか間違えることがあります。dockerの入力促進記号(comman prompt)に気をつけてください。

ファイル共有または複写

dockerとdockerを起動したOSでは、ファイル共有をするか、ファイル複写するかして、生成したファイルをブラウザ等表示させてください。参考文献欄にやり方のURLを記載しています。

複写の場合は、dockerを起動したOS側コマンドを実行しました。お使いのdockerの番号で置き換えてください。複写したファイルをブラウザで表示し内容確認しました。

第3章

(base) root@70104baf94c5:/# cd fundamentals-of-deep-learning-ja/
(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja# ls
README.md  fdl_examples				 requirements.txt
data	   fundamentals-of-deep-learning-ja.png
(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja# cd fdl_examples/
(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja/fdl_examples# ls
__init__.py  chapter4  chapter6  chapter8  datatools
chapter3     chapter5  chapter7  chapter9
(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja/fdl_examples# cd chapter3
(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja/fdl_examples/chapter3# ls
README.md  img			   multilayer_perceptron.py  scope2.py
device.py  logistic_regression.py  scope1.py		     session.py
(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja/fdl_examples/chapter3# python device.py
/opt/conda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
2018-10-08 07:00:54.361703: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-10-08 07:00:54.370758: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Device mapping: no known devices.
2018-10-08 07:00:54.370943: I tensorflow/core/common_runtime/direct_session.cc:288] Device mapping:

chapter3/scope1.py

(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja/fdl_examples/chapter3# python scope1.py
Printing names of weight parameters
W_1:0 W_2:0 W_3:0
Printing names of bias parameters
biases_1:0 biases_2:0 biases_3:0
Printing names of weight parameters
W_1_1:0 W_2_1:0 W_3_1:0
Printing names of bias parameters
biases_1_1:0 biases_2_1:0 biases_3_1:0
(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja/fdl_examples/chapter3# python scope2.py

chapter3/logistic_regression.py

logistic_regression.py
(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja/fdl_examples/chapter3# python logistic_regression.py 
WARNING:tensorflow:From logistic_regression.py:5: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py:252: _internal_retry.<locals>.wrap.<locals>.wrapped_fn (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please use urllib or similar directly.
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting data/train-labels-idx1-ubyte.gz
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
2018-10-08 08:11:41.989335: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-10-08 08:11:41.989777: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
WARNING:tensorflow:Passing a `GraphDef` to the SummaryWriter is deprecated. Pass a `Graph` object instead, such as `sess.graph`.
Epoch: 0001 cost: 1.176761562
Validation Error: 0.149399995803833
Epoch: 0002 cost: 0.662642708
Validation Error: 0.12959998846054077
Epoch: 0003 cost: 0.550728443
Validation Error: 0.12099999189376831
Epoch: 0004 cost: 0.496845487
Validation Error: 0.11640000343322754
Epoch: 0005 cost: 0.463774768
Validation Error: 0.10979998111724854
Epoch: 0006 cost: 0.440914543
Validation Error: 0.107200026512146
Epoch: 0007 cost: 0.423964346
Validation Error: 0.10460001230239868
Epoch: 0008 cost: 0.410656857
Validation Error: 0.10240000486373901
Epoch: 0009 cost: 0.399871363
Validation Error: 0.10159999132156372
Epoch: 0010 cost: 0.390941541
Validation Error: 0.09799998998641968

(中略)

Epoch: 0098 cost: 0.282698083
Validation Error: 0.07539999485015869
Epoch: 0099 cost: 0.282420104
Validation Error: 0.0756000280380249
Epoch: 0100 cost: 0.282150275
Validation Error: 0.07539999485015869
Optimization Finished!
Test Accuracy: 0.9222000241279602

編集したpythonファイル

  1. PNGファイル出力のおまじない:2行追記
import matplotlib as mpl
mpl.use('Agg')
  1. ファイル出力操作:2行追記、1行注釈化
fig = plt.figure()
#plt.show()
fig.savefig('Nikhil.png')

結果はnikhil.pngファイルとして出力

2. deviceエラー

(base) root@70104baf94c5:/fundamentals-of-deep-learning-ja/fdl_examples/chapter3# python device.py
2018-10-08 07:34:27.876390: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-10-08 07:34:27.879592: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Device mapping: no known devices.


 tensorflow/core/common_runtime/placer.cc:886] MatMul: (MatMul)/job:localhost/replica:0/task:0/device:CPU:0

3. dockerを自力で構築する方へ

ここから下は、上記のpullしていただいたdockerをどういう方針で、どういう手順で作ったかを記録します。
上記のdockerを利用する上での参考資料です。本の続きを実行する上では必要ありません。
自力でdocker/anacondaを構築する場合の手順になります。
dockerfileを作る方法ではありません。ごめんなさい。

docker

ubuntu, debianなどのLinuxを、linux, windows, mac osから共通に利用できる仕組み。
利用するOSの設定を変更せずに利用できるのがよい。
同じ仕様で、大量の人が利用することができる。
ソフトウェアの開発元が公式に対応しているものと、利用者が便利に仕立てたものの両方が利用可能である。今回は、公式に配布しているものを、自分で仕立てて、他の人にも利用できるようにする。

python

DeepLearningの実習をPhthonで行って来た。
pythonを使う理由は、多くの機械学習の仕組みがpythonで利用できることと、Rなどの統計解析の仕組みもpythonから容易に利用できることがある。

anaconda

pythonには、2と3という版の違いと、配布方法の違いなどがある。
Anacondaでpython3をこの1年半利用してきた。
Anacondaを利用した理由は、統計解析のライブラリと、JupyterNotebookが初めから入っているからである。

docker公式配布

ubuntu, debianなどのOSの公式配布,gcc, anacondaなどの言語の公式配布などがある。
これらを利用し、docker-hubに登録することにより、公式配布の質の確認と、変更権を含む幅広い情報の共有ができる。dockerが公式配布するものではなく、それぞれのソフト提供者の公式配布という意味。

docker pull

docker公式配布の利用は、URLからpullすることで実現する。

docker Anaconda

anacondaが公式配布しているものを利用。

$  docker pull continuumio/anaconda3
Using default tag: latest
latest: Pulling from continuumio/anaconda3
Digest: sha256:e07b9ca98ac1eeb1179dbf0e0bbcebd87701f8654878d6d8ce164d71746964d1
Status: Image is up to date for continuumio/anaconda3:latest
OgawaKiyoshi-no-MacBook-Pro:docker-toppers ogawakiyoshi$ docker run -it -p 8888:8888 continuumio/anaconda3 /bin/bash

## apt

```shell-session
(base) root@778f01e56c2a:/# apt update

(base) root@778f01e56c2a:/# aptinstall -y procps vim apt-utils sudo git

Deep Learning git

(base) root@f19e2f06eabb:/# git clone https://github.com/oreilly-japan/fundamentals-of-deep-learning-ja
Cloning into 'deep-learning-from-scratch-2'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 320 (delta 4), reused 3 (delta 3), pack-reused 303
Receiving objects: 100% (320/320), 7.51 MiB | 1.93 MiB/s, done.
Resolving deltas: 100% (178/178), done.

conda

# conda update --prefix /opt/conda anaconda

# conda install tensorflow

pip

(base) root@f19e2f06eabb:/deep-learning-from-scratch-2/ch01# pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 2.0MB/s 
distributed 1.21.8 requires msgpack, which is not installed.
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-18.0

pyqt5

(base) root@f19e2f06eabb:/deep-learning-from-scratch-2/ch01# pip install pyqt5
Collecting pyqt5
  Downloading https://files.pythonhosted.org/packages/3a/c7/4a9bec78c864051051b41b4cc76672ecc232e6dc7dbb91a5f8ff6f20ff64/PyQt5-5.11.2-5.11.1-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl (117.9MB)
    100% |████████████████████████████████| 117.9MB 311kB/s 
Collecting PyQt5_sip<4.20,>=4.19.11 (from pyqt5)
  Downloading https://files.pythonhosted.org/packages/d7/db/06ad1f62a1f80a5df639c322066f03db381c1a6322c02087e75092427838/PyQt5_sip-4.19.12-cp36-cp36m-manylinux1_x86_64.whl (66kB)
    100% |████████████████████████████████| 71kB 30kB/s 
distributed 1.21.8 requires msgpack, which is not installed.
Installing collected packages: PyQt5-sip, pyqt5
Successfully installed PyQt5-sip-4.19.12 pyqt5-5.11.2

apt

libgl1-mesa-dev

(base) root@f19e2f06eabb:/deep-learning-from-scratch-2/ch01# apt install -y libgl1-mesa-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libdrm-amdgpu1 libdrm-dev libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libdrm2 libgl1-mesa-dri libgl1-mesa-glx libglapi-mesa
  libllvm3.9 libpciaccess0 libpthread-stubs0-dev libsensors4 libtxc-dxtn-s2tc libx11-dev libx11-doc libx11-xcb-dev libx11-xcb1
  libxau-dev libxcb-dri2-0 libxcb-dri2-0-dev libxcb-dri3-0 libxcb-dri3-dev libxcb-glx0 libxcb-glx0-dev libxcb-present-dev
  libxcb-present0 libxcb-randr0 libxcb-randr0-dev libxcb-render0 libxcb-render0-dev libxcb-shape0 libxcb-shape0-dev libxcb-sync-dev
  libxcb-sync1 libxcb-xfixes0 libxcb-xfixes0-dev libxcb1-dev libxdamage-dev libxdamage1 libxdmcp-dev libxext-dev libxfixes-dev
  libxfixes3 libxshmfence-dev libxshmfence1 libxxf86vm-dev libxxf86vm1 mesa-common-dev x11proto-core-dev x11proto-damage-dev
  x11proto-dri2-dev x11proto-fixes-dev x11proto-gl-dev x11proto-input-dev x11proto-kb-dev x11proto-xext-dev x11proto-xf86vidmode-dev
  xorg-sgml-doctools xtrans-dev
Suggested packages:
  pciutils lm-sensors libxcb-doc libxext-doc
The following NEW packages will be installed:
  libdrm-amdgpu1 libdrm-dev libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libdrm2 libgl1-mesa-dev libgl1-mesa-dri libgl1-mesa-glx
  libglapi-mesa libllvm3.9 libpciaccess0 libpthread-stubs0-dev libsensors4 libtxc-dxtn-s2tc libx11-dev libx11-doc libx11-xcb-dev
  libx11-xcb1 libxau-dev libxcb-dri2-0 libxcb-dri2-0-dev libxcb-dri3-0 libxcb-dri3-dev libxcb-glx0 libxcb-glx0-dev libxcb-present-dev
  libxcb-present0 libxcb-randr0 libxcb-randr0-dev libxcb-render0 libxcb-render0-dev libxcb-shape0 libxcb-shape0-dev libxcb-sync-dev
  libxcb-sync1 libxcb-xfixes0 libxcb-xfixes0-dev libxcb1-dev libxdamage-dev libxdamage1 libxdmcp-dev libxext-dev libxfixes-dev
  libxfixes3 libxshmfence-dev libxshmfence1 libxxf86vm-dev libxxf86vm1 mesa-common-dev x11proto-core-dev x11proto-damage-dev
  x11proto-dri2-dev x11proto-fixes-dev x11proto-gl-dev x11proto-input-dev x11proto-kb-dev x11proto-xext-dev x11proto-xf86vidmode-dev
  xorg-sgml-doctools xtrans-dev
0 upgraded, 61 newly installed, 0 to remove and 25 not upgraded.
Need to get 24.4 MB of archives.
After this operation, 193 MB of additional disk space will be used.
Get:1 http://deb.debian.org/debian stretch/main amd64 libdrm2 amd64 2.4.74-1 [36.2 kB]

3.docker hub 登録

ここからは、新たにソフトを導入したdockerを自分のhubに登録する方法です。
ご自身で何かソフトウェアを導入されたら、ぜひhubに登録することをお勧めします。
続きの作業を誰かに依頼したり、エラーがでてわからなくなったときに、対処方法を問い合わせるのにも役立ちます。

$ docker ps 
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                            NAMES
b28c065c8b45        continuumio/anaconda3            "/usr/bin/tini -- /b…"   21 hours ago        Up 21 hours                                                                                  elegant_gates       

$ docker commit b28c065c8b45  kaizenjapan/anaconda-deep-1
sha256:2d26c8621bf9cc3c2400477a9db98bb21a953fdbe53c22da395ca4a80b15261f

$ docker push  kaizenjapan/anaconda-deep-1
The push refers to repository [docker.io/kaizenjapan/anaconda-deep-1]
b6d479cc3a0f: Pushing [==>                                                ]  68.41MB/1.453GB
6410333f34cf: Mounted from kaizenjapan/anaconda-keras-ten 
cf342e34eca3: Mounted from kaizenjapan/anaconda-keras-ten 
cea95006e36a: Mounted from kaizenjapan/anaconda-keras-ten 
0f3a12fef684: Mounted from kaizenjapan/anaconda-keras-ten 

# ちょっと横入り

``` 
$ docker run -it -p 8888:8888 -p 6006:6006 kaizenjapan/anaconda-keras /bin/bash

参考資料(reference)

深層学習の学習【その2】[追記] Kuroyagi飼育日誌
http://kuroyagi.hatenablog.com/entry/2017/02/28/001233

dockerで機械学習 一覧
https://qiita.com/kaizen_nagoya/items/ddd12477544bf5ba85e2

docker どっかーら、どーやってもエラーばっかり。
https://qiita.com/kaizen_nagoya/items/690d806a4760d9b9e040

dockerで機械学習(python:anaconda)「直感Deep Learning」Antonio Gulli、Sujit Pal 第1章,第2章
https://qiita.com/kaizen_nagoya/items/483ae708c71c88419c32

OpenCVをPythonで動かそうとしてlibGL.soが無いって言われたけど解決した。
https://qiita.com/toshitanian/items/5da24c0c0bd473d514c8

サーバサイドにおけるmatplotlibによる作図Tips
https://qiita.com/TomokIshii/items/3a26ee4453f535a69e9e

Dockerでホストとコンテナ間でのファイルコピー
https://qiita.com/gologo13/items/7e4e404af80377b48fd5

Docker for Mac でファイル共有を利用する
https://qiita.com/seijimomoto/items/1992d68de8baa7e29bb5

「名古屋のIoTは名古屋のOSで」Dockerをどっかーらどうやって使えばいいんでしょう。TOPPERS/FMP on RaspberryPi with Macintosh編 5つの関門
https://qiita.com/kaizen_nagoya/items/9c46c6da8ceb64d2d7af

64bitCPUへの道 and/or 64歳の決意
https://qiita.com/kaizen_nagoya/items/cfb5ffa24ded23ab3f60

ゼロから作るDeepLearning2自然言語処理編 読書会の進め方(例)
https://qiita.com/kaizen_nagoya/items/025eb3f701b36209302e

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.10 初稿 20181008 午前
ver. 0.11 エラー 追記 20181009 午後
ver. 0.12 ありがとう追記 20230529

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

3
5
0

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
3
5