初めまして。社員Dです。
今回は社長が何度か紹介しているDocker & TensorFlowを試してみましょう。
#社長はGPU版を使っていますが、今回は簡単なCPU版です!
環境
macOS Sierra 10.12.5
Docker Community Edition 17.03.1-ce-mac12 (17661)
手順
まずDockerをインストールします。
#ここは割愛します!
次にTensorFlowですが、公式ページの通りにやっていきます。
https://www.tensorflow.org/
「Install」をクリックします。
「Installing TensorFlow on Mac OS X」をクリックします。
この中の「Installing with Docker」に手順が書いてあります。
色々と書いてありますが、ターミナルから以下のコマンドを実行するだけです。
成功すると、コンテナのシェルが表示されます。
1 2 |
$ docker run -it gcr.io/tensorflow/tensorflow bash # |
公式ページに紹介されている以下のTensorFlowのサンプルコードを実行してみましょう。
1 2 3 4 |
>>> import tensorflow as tf >>> hello = tf.constant('Hello, TensorFlow!') >>> sess = tf.Session() >>> print(sess.run(hello)) |
まず、pythonのシェルを起動します。
1 |
# python |
以下、サンプルコードの実行結果です。
1 2 3 4 5 6 7 8 9 10 |
>>> import tensorflow as tf >>> hello = tf.constant('Hello, TensorFlow!') >>> sess = tf.Session() 2017-06-04 04:24:07.408628: W 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-06-04 04:24:07.408697: W 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-06-04 04:24:07.408719: W 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-06-04 04:24:07.408737: W 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-06-04 04:24:07.408760: W 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. >>> print(sess.run(hello)) Hello, TensorFlow! |
いくつか警告は表示されていますが、上記のように「Hello, TensorFlow!」が表示されれば成功です!
以下のコマンドで、pythonのシェルとコンテナのシェルを終了します。
1 2 |
>>> exit() # exit |
Jupyter notebookを利用した手順
公式ページにはJupyter notebookを利用した方法も紹介されているので、やってみます。
ターミナルから実行します。
1 |
$ docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow |
成功すると、以下のようなメッセージを表示されますので、ブラウザにコピペして表示してみましょう。
1 2 3 |
Copy/paste this URL into your browser when you connect for the first time, to login with a token: http://localhost:8888/?token=************************************************ |
Jupyter notebookが表示されます。
右の方にある「New」から「Python 2」を選択します。
Pythonのノートブックが作られます。
セルにPythonのコードを入力します。
control+enterで実行します。
上記のように「Hello, TensorFlow!」が表示されれば成功です!