François Chollet, J. J. Allaire:R と Keras によるディープラーニング

作成日:2021-02-18
最終更新日:

概要

原題は「 Deep Learning with R」。 サポートページには、本書のコードが置かれている。
https://github.com/jjallaire/deep-learning-with-r-notebooks/

感想

3章 ニューラルネットワークによる問題解決の概要

3.2.2 Keras のインストールには次のように書いてある。

install.packages("keras") library(keras) install_keras()

このコマンドはどのような状況でおこなうのだろうか。まさかシェルのコマンドではないだろう。 ということは、R に入ってから打つのだろう。そう思って、 WSL2 の Ubuntu 20.04 でやってみた。

$ R

R version 4.0.4 (2021-02-15) -- "Lost Library Book"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
(中略)
> install.packages("keras")
 パッケージを ‘/home/username/R/x86_64-pc-linux-gnu-library/4.0’ 中にインストールします
 (‘lib’ が指定されていないため)
 依存対象 (dependency) ‘rappdirs’, ‘config’, ‘reticulate’, ‘tensorflow’, ‘tfruns’, ‘zeallot’ もインストールします
 (中略)
 * DONE (keras)

 ダウンロードされたパッケージは、以下にあります
        ‘/tmp/RtmpCCjpVV/downloaded_packages’
> library(keras)
> No non-system installation of Python could be found.
Would you like to download and install Miniconda?
Miniconda is an open source environment management system for Python.
See https://docs.conda.io/en/latest/miniconda.html for more details.

Would you like to install Miniconda? [Y/n]:Y
* Downloading 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh' ...
 URL 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh' を試しています
Content type 'application/x-sh' length 94235922 bytes (89.9 MB)
==================================================
downloaded 89.9 MB

* Installing Miniconda -- please wait a moment ...
PREFIX=/home/username/.local/share/r-miniconda
Unpacking payload ...
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/username/.local/share/r-miniconda

  added / updated specs:
(中略)
Installation complete

>

最後の(中略)としたところでは、ものすごい数のプログラムがインストールされる。

さて、ここまで終わったうえでデータセットのロードを試したが、失敗した。

$ R
(中略) > library(keras) > mnist < dataset_mnist() *** caught illegal operation *** address 0x7f171d0b83f0, cause 'illegal operand' Traceback: 1: py_module_import(module, convert = convert) 2: import(module) 3: doTryCatch(return(expr), name, parentenv, handler) 4: tryCatchOne(expr, names, parentenv, handlers[[1L]]) 5: tryCatchList(expr, classes, parentenv, handlers) 6: tryCatch(import(module), error = clear_error_handler()) 7: py_resolve_module_proxy(x) 8: `$.python.builtin.module`(keras, "datasets") 9: keras$datasets 10: dataset_mnist() Possible actions: 1: abort (with core dump, if enabled) 2: normal R exit 3: exit R without saving workspace 4: exit R saving workspace Selection: 2 Save workspace image? [y/n/c]: n $

stackoverflow の記事を見て、これは tensorflow の問題だろうと推測した。

$ R
(中略) > library(tensorflow) > tf_version() *** caught illegal operation *** address 0x7ff3203093f0, cause 'illegal operand' Traceback: 1: py_module_import(module, convert = convert) 2: import(module) 3: doTryCatch(return(expr), name, parentenv, handler) 4: tryCatchOne(expr, names, parentenv, handlers[[1L]]) 5: tryCatchList(expr, classes, parentenv, handlers) 6: tryCatch({ import(module) TRUE}, error = clear_error_handler(FALSE)) 7: py_module_available("tensorflow") 8: tf_config() 9: tf_version() Possible actions: 1: abort (with core dump, if enabled) 2: normal R exit 3: exit R without saving workspace 4: exit R saving workspace Selection: (後略)

やはり tensorflow の問題だろう。
https://github.com/rstudio/tensorflow/issues/228
には、tensorflow のバージョンを 1.5 に指定するといいようだ。

$ R

(中略)
> library("keras")
> install_keras(tensorflow = "1.5")
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

Collecting tensorflow==1.5
  Downloading tensorflow-1.5.0-cp36-cp36m-manylinux1_x86_64.whl (44.4 MB)
     |████████████████████████████████| 44.4 MB 7.4 MB/s
Requirement already satisfied: keras in /home/username/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages (2.4.3)
(中略)
Installing collected packages: html5lib, cached-property, bleach, tensorflow-tensorboard, scipy, h5py, tensorflow
  Attempting uninstall: scipy
    Found existing installation: scipy 1.4.1
    Uninstalling scipy-1.4.1:
      Successfully uninstalled scipy-1.4.1
  Attempting uninstall: h5py
    Found existing installation: h5py 2.10.0
    Uninstalling h5py-2.10.0:
      Successfully uninstalled h5py-2.10.0
  Attempting uninstall: tensorflow
    Found existing installation: tensorflow 2.2.0
    Uninstalling tensorflow-2.2.0:
      Successfully uninstalled tensorflow-2.2.0
Successfully installed bleach-1.5.0 cached-property-1.5.2 h5py-3.1.0 html5lib-0.9999999 scipy-1.5.4 tensorflow-1.5.0 tensorflow-tensorboard-1.5.1

Installation complete.
> library(tensorflow)
> tf_version()
/home/username/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:493:→
 FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated;→
  in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
(中略)
[1] ‘1.5’
> library(keras)
  エラー:

それでもエラーか。一度 R を抜けて、やり直そう。

$ R
(中略)
> library(keras)
> mnist <- dataset_mnist()
/home/username/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:493:→
 FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated;→
  in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
(中略)
 エラー:
>

この、ただの「エラー」では、その後をさぐりようがない。あきらめた。

Windows 10 Home でもどうなるか試した。R 4.0.4 を入れて試してみた。以下は R Console の結果である:

> library(keras)
> mnist <- dataset_mnist()
 エラー: Installation of TensorFlow not found.

Python environments searched for 'tensorflow' package:
 C:\Users\username\AppData\Local\r-miniconda\envs\r-reticulate\python.exe

You can install TensorFlow using the install_tensorflow() function.
> install_tensorflow()
 install_tensorflow() でエラー: 
   関数 "install_tensorflow" を見つけることができませんでした 

この install_tensorflow() 関数というのは R Console から入力するものではないと思うが、 かといってどうしたらよいのか、わからない。では、WSL2 の Ubuntu 20.04 と同じようにやるとどうなるか。

> library("keras")
> install_keras(tensorflow = "1.5")
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done

# All requested packages already installed.

Collecting tensorflow==1.5
  Downloading tensorflow-1.5.0-cp36-cp36m-win_amd64.whl (31.1 MB)
(中略)
Installation complete.
(後略)
> mnist <- dataset_mnist()
C:\Users\satosi\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\framework\dtypes.py:493: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Users\satosi\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\framework\dtypes.py:494: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Users\satosi\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\framework\dtypes.py:495: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Users\satosi\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\framework\dtypes.py:496: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Users\satosi\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\framework\dtypes.py:497: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Users\satosi\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\framework\dtypes.py:502: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
 エラー: 
>

これは Ubuntu と同じである。なお、tensorflow=1.4 としたときも同じエラーとなった。 一方、tensorflow=1.6 を指定すると、
エラー: Installation of TensorFlow not found.
から始まるメッセージと同じである。結局エラーは解消できなかった。あきらめる。

3.3.3 ディープラーニングにもっとも適した GPU は何か、という節がある。

2017 年中ごろの時点では、ディープラーニング用の最高の市販カードとして NVIDIA TITAN Xp をお勧めします。 もう少し安いものがよいという場合は、GTX 1070 を検討してください。新モデルは毎年発売されるので、 2018 年以降にこの部分を読んでいる方は、時間を割いてオンラインで新しい推奨カードを調べてください。

現在は 2021 年 2 月である。新しい推奨カードを探しているが、わからない。 少し古いが、 NVIDIA GeForceデスクトップGPU性能比較早見表 (pcinformation.info) というページがある。

書誌情報

書 名R と Keras によるディープラーニング
著 者François Chollet, J. J. Allaire
発行日2018 年 10 月 11 日(初版第1刷)
発行所オライリー・ジャパン
発売元オーム社
定 価4000 円
サイズ
ISBN978-4-87311-857-4
その他越谷市立図書館で借りて読む

まりんきょ学問所コンピュータの部屋コンピュータの本ニューロコンピューティング・人工知能 > François Chollet, J. J. Allaire:R と Keras によるディープラーニング


MARUYAMA Satosi