2016年10月16日日曜日

TensorFlow for Rを使ってみる(2)

前回はこちら

RStudioからTensorflowを使う

  • 前回、PythonからTensorflowを使う環境ができた。
  • 今度はRStudioからTensorflowを使う。
  • RStudioから以下のコードでtensorflow for Rパッケージをインストール
    install.packages("devtools")  ## install_githubを使うため
    library(devtools)
        install_github("rstudio/tensorflow") ## tensorflowをインストール
          library(tensorflow)

          • パッケージのインストールが終わったらこちらのコマンドできちんと利用できているかを確認

          sess = tf$Session()

          結果

          > sess = tf$Session()
           エラー:  関数でないものを適用しようとしました 
          うお、エラーが出てしまいました。
          今のところここまでで止まっていて、まだRStudioからTensorflowを使えていません…。

          2016年10月5日水曜日

          TensorFlow for R を使ってみる (1)

          ※メモなので、随時内容追加

          更新情報

          • 2016/10/10:「なんか動いた」以降更新
          • 2016/10/05:初版公開

          TensorFlow for Rとは

          • TensorFlow:Googleが開発した機械学習/ディープラーニング/多層ニューラルネットワークの天ライブラリ
          • このライブラリをR/RStudioから使えるようにしたパッケージ

          モチベーション

          • ディープラーニングを試してみたい。ただそれだけ。
          • Rしか使えないし、Pythonは使ったことない。

          テスト環境

          • MacOSX El Capitan(10.11.2)
          • RStudio 0.99.903
            • > sessionInfo()
            • R version 3.3.1 (2016-06-21)
            • Platform: x86_64-apple-darwin13.4.0 (64-bit)
            • Running under: OS X 10.11.2 (El Capitan)

            • locale:
            • [1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8

            • attached base packages:
            • [1] stats     graphics  grDevices utils     datasets  methods   base     

            • other attached packages:
            • [1] rmarkdown_1.0 stringi_1.1.2 knitr_1.14   

            • loaded via a namespace (and not attached):
            • [1] magrittr_1.5    htmltools_0.3.5 tools_3.3.1     Rcpp_0.12.7    
            • [5] stringr_1.1.0   digest_0.6.10   evaluate_0.9   

          初期設定

          • まずはTensorFlow for R のドキュメントを読む
            • TensorFlowのインストール
            • # Mac OS X
              $ sudo easy_install pip
              $ sudo easy_install --upgrade six
              • # Mac OS X, CPU only, Python 2.7:
                $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl
                
                # Mac OS X, GPU enabled, Python 2.7:
                $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.11.0rc0-py2-none-any.whl
            • 期待していたとおり、エラー
            • $ sudo pip install -- upgrade $TF_BINARY_URL
              The directory '/Users/user/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.The directory '/Users/user/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.Collecting upgrate  Could not find a version that satisfies the requirement upgrate (from versions: )No matching distribution found for upgrate

            • なんか動いた。大量のエラーログ…
              • $ sudo pip install --upgrade $TF_BINARY_URL
                The directory '/Users/user/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
                The directory '/Users/user/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
                Collecting tensorflow==0.11.0rc0 from https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl
                  Downloading https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl (35.5MB)
                    100% |████████████████████████████████| 35.5MB 17kB/s 
                Collecting mock>=2.0.0 (from tensorflow==0.11.0rc0)
                  Downloading mock-2.0.0-py2.py3-none-any.whl (56kB)
                    100% |████████████████████████████████| 61kB 773kB/s 
                Collecting protobuf==3.0.0 (from tensorflow==0.11.0rc0)
                  Downloading protobuf-3.0.0-py2.py3-none-any.whl (342kB)
                    100% |████████████████████████████████| 348kB 488kB/s 
                Collecting numpy>=1.11.0 (from tensorflow==0.11.0rc0)
                  Downloading numpy-1.11.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.9MB)
                    100% |████████████████████████████████| 3.9MB 114kB/s 
                Collecting wheel (from tensorflow==0.11.0rc0)
                  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
                    100% |████████████████████████████████| 71kB 2.8MB/s 
                Requirement already up-to-date: six>=1.10.0 in /Library/Python/2.7/site-packages/six-1.10.0-py2.7.egg (from tensorflow==0.11.0rc0)
                Collecting funcsigs>=1; python_version < "3.3" (from mock>=2.0.0->tensorflow==0.11.0rc0)
                  Downloading funcsigs-1.0.2-py2.py3-none-any.whl
                Collecting pbr>=0.11 (from mock>=2.0.0->tensorflow==0.11.0rc0)
                  Downloading pbr-1.10.0-py2.py3-none-any.whl (96kB)
                    100% |████████████████████████████████| 102kB 2.7MB/s 
                Collecting setuptools (from protobuf==3.0.0->tensorflow==0.11.0rc0)
                  Downloading setuptools-28.3.0-py2.py3-none-any.whl (467kB)
                    100% |████████████████████████████████| 471kB 613kB/s 
                Installing collected packages: funcsigs, pbr, mock, setuptools, protobuf, numpy, wheel, tensorflow
                  Found existing installation: setuptools 1.1.6
                    Uninstalling setuptools-1.1.6:
                Exception:
                Traceback (most recent call last):
                  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/basecommand.py", line 215, in main
                    status = self.run(options, args)
                  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/commands/install.py", line 317, in run
                    prefix=options.prefix_path,
                  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_set.py", line 736, in install
                    requirement.uninstall(auto_confirm=True)
                  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_install.py", line 742, in uninstall
                    paths_to_remove.remove(auto_confirm)
                  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove
                    renames(path, new_path)
                  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/utils/__init__.py", line 267, in renames
                    shutil.move(old, new)
                  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 299, in move
                    copytree(src, real_dst, symlinks=True)
                  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 208, in copytree
                    raise Error, errors
                Error: [('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', "[Errno 1] Operation not permitted: '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', "[Errno 1] Operation not permitted: '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py', '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py', "[Errno 1] Operation not permitted: '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc', '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc', "[Errno 1] Operation not permitted: '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib', '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib', "[Errno 1] Operation not permitted: '/tmp/pip-aojXT0-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib'")]

          インストールできない問題を解消

          • どうもパッケージのところでコケてる模様
            • 調べてみたところ、Yosemiteまでだと問題なく公式の言うとおりにしていけば導入できるとのこと。
              原因はEl Capitanから導入されたシステム整合性保護(System Integrity Protection (SIP)やrootlessなど)
              と呼ばれる新しいセキュリティ技術のためだそうです。
          • /usr/local のセキュリティ設定(El Capitan)悪さをしていた
            • http://qiita.com/aoioooii/items/c14922eede6a83a750da
            • これで解消
          • Brew経由でpythonを最新版に。(python2.7.10 -> python2.7.12)
          • 以下のコマンドでインストール完了
          • $sudo -H pip install tensorflow  

          Python環境での確認

          python
          ...
          >>> import tensorflow as tf
          >>> hello = tf.constant('Hello, TensorFlow!')
          >>> sess = tf.Session()
          >>> print(sess.run(hello))
          Hello, TensorFlow!
          >>> a = tf.constant(10)
          >>> b = tf.constant(32)
          >>> print(sess.run(a + b))
          42
          >>>

          • python環境でコマンドを叩くと、上記結果に。
          • 「Hellow, TensorFlow!」、「42」が結果として表示されれば、OK
          • 次はやっと本命のR環境からTensorFlowを使ってみる。

          余談

          ドキュメントリソース