External Memory

プログラミング周辺知識の備忘録メイン

開発環境の構築(1) Python

Anaconda環境にTensorFlowをインストー

人工知能の勉強として主に使うプログラミング言語はメジャーなPythonにした。

C++という選択肢も考えたが、
お手軽さとデータ収集や深層学習で便利なライブラリがよく揃っているのは魅力的。

TensorFlow等を使用したいのでdocker for windowsを使ってubuntu環境で構築しようかと思ったら、windows版のTensorFlowがあるみたいなのでそちらをインストール。


Installing TensorFlow on Windows  |  TensorFlow
の説明に従い、

Download Anaconda Now! | Continuum
でAnaconda 4.4.0 For Windowsをダウンロード。

Anacondaは目的別に環境が分けられる点で便利。
tensorflowという名前の環境を作る。

(C:\Users\[username]\Anaconda3) C:\Users\[username]>conda create -n tensorflow python=3.5
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment C:\Users\[username]\Anaconda3\envs\tensorflow:

The following NEW packages will be INSTALLED:

    pip:            9.0.1-py35_1
    python:         3.5.3-3
    setuptools:     27.2.0-py35_1
    vs2015_runtime: 14.0.25420-0
    wheel:          0.29.0-py35_0

Proceed ([y]/n)? y

vs2015_runtime 100% |###############################| Time: 0:00:02 780.38 kB/s
python-3.5.3-3 100% |###############################| Time: 0:00:52 612.76 kB/s
setuptools-27. 100% |###############################| Time: 0:00:00   1.03 MB/s
wheel-0.29.0-p 100% |###############################| Time: 0:00:00   1.36 MB/s
pip-9.0.1-py35 100% |###############################| Time: 0:00:01   1.05 MB/s
#
# To activate this environment, use:
# > activate tensorflow
#
# To deactivate this environment, use:
# > deactivate tensorflow
#
# * for power-users using bash, you must source

tensorflow環境が構築できたことを確認する。

(C:\Users\[username]\Anaconda3) C:\Users\[username]>conda info -e
# conda environments:
#
tensorflow               C:\Users\[username]\Anaconda3\envs\tensorflow
root                  *  C:\Users\[username]\Anaconda3

tensorflow環境をactivate

(C:\Users\[username]\Anaconda3) C:\Users\[username]>activate tensorflow

TensorFlow対応GPUは持っておらず、
勉強用なのでGPU版ではなく、CPU版をダウンロード。

(tensorflow) C:\Users\[username]>pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.2.1-cp35-cp35m-win_amd64.whl

(tensorflow) C:\Users\[username]>python
Python 3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> sess = tf.Session()
2017-07-22 21:00:06.035945: W c:\tf_jenkins\home\workspace\release-win\m\windows\py\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 21:00:06.036057: W c:\tf_jenkins\home\workspace\release-win\m\windows\py\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 21:00:06.036379: W c:\tf_jenkins\home\workspace\release-win\m\windows\py\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 21:00:06.036672: W c:\tf_jenkins\home\workspace\release-win\m\windows\py\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 21:00:06.037029: W c:\tf_jenkins\home\workspace\release-win\m\windows\py\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 21:00:06.037343: W c:\tf_jenkins\home\workspace\release-win\m\windows\py\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 21:00:06.037609: W c:\tf_jenkins\home\workspace\release-win\m\windows\py\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 21:00:06.037675: W c:\tf_jenkins\home\workspace\release-win\m\windows\py\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
>>> hello = tf.constant('Hello,TensorFlow')
>>> sess.run(hello)
b'Hello,TensorFlow'
(省略)
>>> a = tf.constant(7)
>>> b = tf.constant(13)
>>> add = a + b
>>> print(sess2.run(add))
20

警告が出るが今は無視。

python - TensorFlow wasn't compiled to use SSE (etc.) instructions, but these are available - Stack Overflow

AVXとSSE拡張命令が使えて、高速化するみたい。

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
...

で一応警告は消せる。


Anacondaで作成した環境からJupyter Notebookが反映されなかった。
もし使う場合は、作成環境内でもjupyterをインストールして以下を試す。

pip install environment_kernels
jupyter notebook --generate-config

設定ファイルに以下を追加する。

c.EnvironmentKernelSpecManager.conda_env_dirs=['/Users/[username]/anaconda3/envs/']

必要になったら以下を参考にする。

GitHub - Cadair/jupyter_environment_kernels: An Jupyter plugin to enable the automatic detection of conda environments as kernels.github.com

qiita.com


ライブラリの使用法のお勉強だけで終わらないようにすること。