Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

TensorFlow 2.0を簡単に試してみる

こんにちは、LinuCエバンジェリストこと、鯨井貴博@opensourcetechです。

 

Googleの深層学習フレームワークTensorFlowの新バージョン 2.0α が公開されたとのことですので、簡単に試してみます。

https://medium.com/tensorflow/test-drive-tensorflow-2-0-alpha-b6dd1e522b01

https://www.tensorflow.org/alpha

 

 

なお、Python/pip や Jupyter Notebookなどが必要になるので、

事前にインストールください。

※参考情報も、一応貼っておきます。

Homebrew & Python3(pip3)インストール on MacOS High Sierra - Opensourcetechブログ

AIを学ぶ4 〜Jupyter Notebook を使ってみる〜 - Opensourcetechブログ

 

 

 今回やってみること

予めお断りしておくと、

基本的には以下にある初心者向けの例に従ってやってるだけです。

https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/r2/tutorials/quickstart/beginner.ipynb

 

 

 TensorFlow 2.0αのインストール

pipでインストールします。

※GPUを使用するケースではおそらく「pip install tensorflow==2.0.0-alpha0-gpu」でいいはず。https://www.tensorflow.org/install/pip#hardware-requirements

bash-3.2$ pip list | grep tensor
tensorboard 1.9.0
tensorflow 1.9.0
bash-3.2$ pip install tensorflow==2.0.0-alpha0
Collecting tensorflow==2.0.0-alpha0
Downloading https://files.pythonhosted.org/packages/de/be/65c6c6ab4660ca8c0068a5fed739944f34af02b42cb727cefe0a5383dc8e/tensorflow-2.0.0a0-cp36-cp36m-macosx_10_11_x86_64.whl (64.3MB)
100% |████████████████████████████████| 64.3MB 94kB/s
Collecting numpy<2.0,>=1.14.5 (from tensorflow==2.0.0-alpha0)
Downloading https://files.pythonhosted.org/packages/93/0e/30aaa357c3065957344b240482818eef31d4080f73dfa5f1ef7dcd8744d2/numpy-1.16.2-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (13.9MB)
100% |████████████████████████████████| 13.9MB 640kB/s
Requirement already satisfied: grpcio>=1.8.6 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==2.0.0-alpha0) (1.13.0)
Requirement already satisfied: wheel>=0.26 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==2.0.0-alpha0) (0.31.1)
Requirement already satisfied: gast>=0.2.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==2.0.0-alpha0) (0.2.0)
Requirement already satisfied: termcolor>=1.1.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==2.0.0-alpha0) (1.1.0)
Collecting tb-nightly<1.14.0a20190302,>=1.14.0a20190301 (from tensorflow==2.0.0-alpha0)
Downloading https://files.pythonhosted.org/packages/a9/51/aa1d756644bf4624c03844115e4ac4058eff77acd786b26315f051a4b195/tb_nightly-1.14.0a20190301-py3-none-any.whl (3.0MB)
100% |████████████████████████████████| 3.0MB 1.3MB/s
Collecting tf-estimator-nightly<1.14.0.dev2019030116,>=1.14.0.dev2019030115 (from tensorflow==2.0.0-alpha0)
Downloading https://files.pythonhosted.org/packages/13/82/f16063b4eed210dc2ab057930ac1da4fbe1e91b7b051a6c8370b401e6ae7/tf_estimator_nightly-1.14.0.dev2019030115-py2.py3-none-any.whl (411kB)
100% |████████████████████████████████| 419kB 1.4MB/s
Collecting keras-applications>=1.0.6 (from tensorflow==2.0.0-alpha0)
Downloading https://files.pythonhosted.org/packages/90/85/64c82949765cfb246bbdaf5aca2d55f400f792655927a017710a78445def/Keras_Applications-1.0.7-py2.py3-none-any.whl (51kB)
100% |████████████████████████████████| 61kB 1.5MB/s
Collecting protobuf>=3.6.1 (from tensorflow==2.0.0-alpha0)
Downloading https://files.pythonhosted.org/packages/e3/4b/46c4d78596f86aa07143328fd7ffbe3aabf61d47ee25337b7b173149bbdc/protobuf-3.7.0-cp36-cp36m-macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 1.6MB/s
Requirement already satisfied: astor>=0.6.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==2.0.0-alpha0) (0.7.1)
Collecting google-pasta>=0.1.2 (from tensorflow==2.0.0-alpha0)
Downloading https://files.pythonhosted.org/packages/8c/96/adbd4eafe72ce9b5ca6f168fbf109386e1b601f7c59926a11e9d7b7a5b44/google_pasta-0.1.4-py3-none-any.whl (51kB)
100% |████████████████████████████████| 61kB 3.6MB/s
Collecting absl-py>=0.7.0 (from tensorflow==2.0.0-alpha0)
Downloading https://files.pythonhosted.org/packages/31/bc/ab68120d1d89ae23b694a55fe2aece2f91194313b71f9b05a80b32d3c24b/absl-py-0.7.0.tar.gz (96kB)
100% |████████████████████████████████| 102kB 2.9MB/s
Collecting keras-preprocessing>=1.0.5 (from tensorflow==2.0.0-alpha0)
Downloading https://files.pythonhosted.org/packages/c0/bf/0315ef6a9fd3fc2346e85b0ff1f5f83ca17073f2c31ac719ab2e4da0d4a3/Keras_Preprocessing-1.0.9-py2.py3-none-any.whl (59kB)
100% |████████████████████████████████| 61kB 3.4MB/s
Requirement already satisfied: six>=1.10.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==2.0.0-alpha0) (1.11.0)
Requirement already satisfied: markdown>=2.6.8 in ./anaconda3/lib/python3.6/site-packages (from tb-nightly<1.14.0a20190302,>=1.14.0a20190301->tensorflow==2.0.0-alpha0) (2.6.11)
Requirement already satisfied: werkzeug>=0.11.15 in ./anaconda3/lib/python3.6/site-packages (from tb-nightly<1.14.0a20190302,>=1.14.0a20190301->tensorflow==2.0.0-alpha0) (0.14.1)
Requirement already satisfied: h5py in ./anaconda3/lib/python3.6/site-packages (from keras-applications>=1.0.6->tensorflow==2.0.0-alpha0) (2.7.1)
Requirement already satisfied: setuptools in ./anaconda3/lib/python3.6/site-packages (from protobuf>=3.6.1->tensorflow==2.0.0-alpha0) (39.1.0)
Building wheels for collected packages: absl-py
Running setup.py bdist_wheel for absl-py ... done
Stored in directory: /Users/kujiraitakahiro/Library/Caches/pip/wheels/90/db/f8/2c3101f72ef1ad434e4662853174126ce30201a3e163dcbeca
Successfully built absl-py
Installing collected packages: numpy, absl-py, protobuf, tb-nightly, tf-estimator-nightly, keras-applications, google-pasta, keras-preprocessing, tensorflow
Found existing installation: numpy 1.14.3
Uninstalling numpy-1.14.3:
Successfully uninstalled numpy-1.14.3
Found existing installation: protobuf 3.6.0
Uninstalling protobuf-3.6.0:
Successfully uninstalled protobuf-3.6.0
Found existing installation: tensorflow 1.9.0
Uninstalling tensorflow-1.9.0:
Successfully uninstalled tensorflow-1.9.0
Successfully installed absl-py-0.7.0 google-pasta-0.1.4 keras-applications-1.0.7 keras-preprocessing-1.0.9 numpy-1.16.2 protobuf-3.7.0 tb-nightly-1.14.0a20190301 tensorflow-2.0.0a0 tf-estimator-nightly-1.14.0.dev2019030115

bash-3.2$ pip list | grep tensor
tensorboard 1.9.0
tensorflow 2.0.0a0

 

 

Jupyter Notebookの起動

bash-3.2$ jupyter notebook --NotebookApp.allow_origin='https://colab.research.google.com' --port=8888 --NotebookApp.port_reties=0
[W 19:19:44.812 NotebookApp] Config option `port_reties` not recognized by `NotebookApp`. Did you mean `port_retries`?
[W 19:19:44.815 NotebookApp] Config option `port_reties` not recognized by `NotebookApp`. Did you mean `port_retries`?
[W 19:19:44.825 NotebookApp] Config option `port_reties` not recognized by `NotebookApp`. Did you mean `port_retries`?
[W 19:19:44.828 NotebookApp] Config option `port_reties` not recognized by `NotebookApp`. Did you mean `port_retries`?
jupyter_http_over_ws extension initialized. Listening on /http_over_websocket
[I 19:19:45.984 NotebookApp] JupyterLab beta preview extension loaded from /Users/Ryo/anaconda3/lib/python3.6/site-packages/jupyterlab
[I 19:19:45.984 NotebookApp] JupyterLab application directory is /Users/Ryo/anaconda3/share/jupyter/lab
[I 19:19:46.004 NotebookApp] Serving notebooks from local directory: /Users/Ryo
[I 19:19:46.005 NotebookApp] 0 active kernels
[I 19:19:46.005 NotebookApp] The Jupyter Notebook is running at:
[I 19:19:46.005 NotebookApp] http://localhost:8888/?token=5xyzcshinjuku7aa962b2d9858ecacf4a3178c5c20492736
[I 19:19:46.005 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 19:19:46.008 NotebookApp]

Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8888/?token=5xyzcshinjuku7aa962b2d9858ecacf4a3178c5c20492736&token=5xyzcshinjuku7aa962b2d9858ecacf4a3178c5c20492736
[I 19:19:47.284 NotebookApp] Accepting one-time-token-authenticated connection from ::1

 

 

その後、自動的にブラウザで以下のような画面が立ち上がります。

f:id:opensourcetech:20190311190003p:plain

 

Firefoxユーザーは、

about:config から「network.websocket.allowinsecureFromHTTPS」という設定を「true」に変更しないと表示されないです。

f:id:opensourcetech:20190311190126p:plain

 

 

TensorFlow2.0のテスト

あとは、以下のリンクにあるコードを実施していくだけ。

https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/r2/tutorials/quickstart/beginner.ipynb

 

右上にある接続から「ローカル ランチタイムに接続」を選択し、

f:id:opensourcetech:20190311190457p:plain

 

ローカル接続の設定を確認しつつ、「接続」をクリック。

f:id:opensourcetech:20190311190512p:plain

 

接続後は、以下のように「接続済み(ローカル)」となります。

f:id:opensourcetech:20190311190800p:plain

 

そのあとは手順のコード部分にマウスを持っていくと、

以下のようにフォーカスされ左側に実行ボタンができ、クリックすると実行されます。

f:id:opensourcetech:20190311190904p:plain

 

なお、pipでtensorflow2.0をインストールした際にpip install tensorflow==2.0.0-alpha0とした方は、以下のように「-gpu」という文字を消しましょう!!

f:id:opensourcetech:20190311191052p:plain

 

 

同様に、コードをぽちぽちクリックしていくと、テストは完了します。

f:id:opensourcetech:20190311191444p:plain

f:id:opensourcetech:20190311191506p:plain

f:id:opensourcetech:20190311191516p:plain

f:id:opensourcetech:20190311191528p:plain

 

 

実施結果を見ると、以下でやったテストと同様のことであることに気づきましたw

AIを学ぶ5 〜TensorFlow & Keras を使ってみる〜 - Opensourcetechブログ

 

 

まだまだα版のTensorFlow2.0、これからの進化に期待!!

 

 

www.slideshare.net

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

 

Opensourcetech by Takahiro Kujirai