【第三回（最終回）】CentOSにPythonの機械学習開発環境を構築する（Tensorflow + Kerasインストール編）

# 前書き
Linuxディストリビューションの**CenOS**に**Jupyter Notebook**を使用して、**Python**、Pythonのライブラリである**scikit-learn**、**Tensorflow**や**keras**等を使って機械学習のプログラムを作成できる機械学習の開発環境を作成する手順を紹介したいと思います。

[「【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）」](https://qiita.com/yasushi-jp/items/7cef8447062fb7dd200c "【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）")では、**Anaconda**をインストールし、機械学習用環境（ml_env）を作成しました。
[「【第二回】CentOSにPythonの機械学習開発環境を構築する（Jupyter Notebook編）」](https://qiita.com/yasushi-jp/items/d06efa771af7a0d8921d "【第二回】CentOSにPythonの機械学習開発環境を構築する（Jupyter Notebook編")では、**Jupyter Notebook**を他のPC等からブラウザで使用できるように設定しました。

今回は**Tensorflow**や**keras**等の機械学習ライブラリをインストールし、サンプルソースが**Jupyter Notebook**で動作することを確認したいと思います。

# 環境
+ OS：CentOS Linux release 7.7.1908

# 前提条件
+ **CentOS**に**Anaconda3**（`Anaconda3-2019.07-Linux-x86_64.sh`）がインストールされていること
+ 機械学習用の環境（ml_env）が作成されていること

（[「【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）」](https://qiita.com/yasushi-jp/items/7cef8447062fb7dd200c "【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）")で紹介）

+ **Jupyter Notebook**を使用可能な設定が完了していること

（[「【第二回】CentOSにPythonの機械学習開発環境を構築する（Jupyter Notebook編）」](https://qiita.com/yasushi-jp/items/d06efa771af7a0d8921d "【第二回】CentOSにPythonの機械学習開発環境を構築する（Jupyter Notebook編）")で紹介）

# 機械学習で使うためインストールしたいパッケージ
|パッケージ名|概要|インストール済みかどうか
|:--------------|:-----------------------------------------------|:--------------------------|
|ipython        |Pythonの対話型インタプリタを拡張したもの|インストール済み|
|jupyter        |プログラムを実行し、実行結果を記録できるツール（Jupyter Notebook）|インストール済み|
|tensorflow     |Google社が提供する機械学習用のオープンソースソフトウェアライブラリ|未インストール|
|keras          |TensorFlowのラッパーライブラリ|未インストール|
|numpy          |数値計算を効率的に行うための拡張モジュール|インストール済み|
|scipy          |プログラミング数学、科学、工学のための数値解析ソフトウェア|インストール済み|
|matplotlib     |プログラミング言語Pythonおよびその科学計算用ライブラリNumPyのためのグラフ描画ライブラリ|インストール済み|
|scikit-learn   |Pythonのオープンソース機械学習ライブラリ|インストール済み|
|pandas         |データ解析を支援する機能を提供するライブラリ|インストール済み|
|pillow         |画像処理ライブラリ|インストール済み|
|msgpack-python |MessagePackフォーマットを使用するためのライブラリ|インストール済み|
|opencv         |画像処理ライブラリ|未インストール|
|mglearn        |コードの簡略化に役立つライブラリ（「Pythonではじめる機械学習」の著者が作成）|未インストール|

# 手順
### 1.   インストール済みパッケージの確認
#### 1.1 anacondaユーザーでログイン
CentOSにanacondaユーザーでログインまたはスイッチします。
`su - anaconda`

```console:コマンド実行結果
[root@CENTOS7 ~]# su - anaconda
最終ログイン: 2019/10/20 (日) 15:32:56 JST日時 pts/0
[anaconda@CENTOS7 ~]$
```

#### 1.2 環境変数の読み込み
[「【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）」](https://qiita.com/yasushi-jp/items/7cef8447062fb7dd200c "【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）")で作成した環境変数`.anaconda.env`を読み込みます。

`source ./.anaconda.env`

```console:コマンド実行結果
[anaconda@CENTOS7 ~]$ source ./.anaconda.env
[anaconda@CENTOS7 ~]$
```

#### 1.3 機械学習用環境（ml_env）をアクティブにする
[「【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）」](https://qiita.com/yasushi-jp/items/7cef8447062fb7dd200c "【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）")で作成した機械学習用環境（ml_env）をアクティブにします。

`conda activate ml_env`

```console:コマンド実行結果
[anaconda@CENTOS7 ~]$ conda activate ml_env
(ml_env) [anaconda@CENTOS7 ~]$
```

#### 1.4 機械学習用環境（ml_env）のインストール済みパッケージの一覧を確認
機械学習用環境（ml_env）にインストールされているパッケージの確認をします。
`conda list`
`conda list | grep <名前>`

```console:コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ conda list
# packages in environment at /home/anaconda/anaconda3/envs/ml_env:
#
# Name                    Version                   Build  Channel
_anaconda_depends         2019.03                  py37_0
_libgcc_mutex             0.1                        main
alabaster                 0.7.12                   py37_0
anaconda                  custom                   py37_1
anaconda-client           1.7.2                    py37_0
anaconda-project          0.8.3                      py_0
asn1crypto                1.0.1                    py37_0
astroid                   2.3.1                    py37_0
astropy                   3.2.2            py37h7b6447c_0
atomicwrites              1.3.0                    py37_1
attrs                     19.2.0                     py_0
babel                     2.7.0                      py_0
backcall                  0.1.0                    py37_0
backports                 1.0                        py_2
backports.os              0.1.1                    py37_0
backports.shutil_get_terminal_size 1.0.0                    py37_2
beautifulsoup4            4.8.0                    py37_0
bitarray                  1.0.1            py37h7b6447c_0
bkcharts                  0.2                      py37_0
blas                      1.0                         mkl
bleach                    3.1.0                    py37_0
blosc                     1.16.3               hd408876_0
bokeh                     1.3.4                    py37_0
boto                      2.49.0                   py37_0
bottleneck                1.2.1            py37h035aef0_1
bzip2                     1.0.8                h7b6447c_0
ca-certificates           2019.8.28                     0
cairo                     1.14.12              h8948797_3
certifi                   2019.9.11                py37_0
cffi                      1.12.3           py37h2e261b9_0
chardet                   3.0.4                 py37_1003
click                     7.0                      py37_0
cloudpickle               1.2.2                      py_0
clyent                    1.2.2                    py37_1
colorama                  0.4.1                    py37_0
contextlib2               0.6.0                      py_0
cryptography              2.7              py37h1ba5d50_0
curl                      7.65.3               hbc83047_0
cycler                    0.10.0                   py37_0
cython                    0.29.13          py37he6710b0_0
cytoolz                   0.10.0           py37h7b6447c_0
dask                      2.5.2                      py_0
dask-core                 2.5.2                      py_0
dbus                      1.13.6               h746ee38_0
decorator                 4.4.0                    py37_1
defusedxml                0.6.0                      py_0
distributed               2.5.2                      py_0
docutils                  0.15.2                   py37_0
entrypoints               0.3                      py37_0
et_xmlfile                1.0.1                    py37_0
expat                     2.2.6                he6710b0_0
fastcache                 1.1.0            py37h7b6447c_0
filelock                  3.0.12                     py_0
flask                     1.1.1                      py_0
fontconfig                2.13.0               h9420a91_0
freetype                  2.9.1                h8a8886c_1
fribidi                   1.0.5                h7b6447c_0
fsspec                    0.5.2                      py_0
get_terminal_size         1.0.0                haa9412d_0
gevent                    1.4.0            py37h7b6447c_0
glib                      2.56.2               hd408876_0
glob2                     0.7                        py_0
gmp                       6.1.2                h6c8ec71_1
gmpy2                     2.0.8            py37h10f8cd9_2
graphite2                 1.3.13               h23475e2_0
greenlet                  0.4.15           py37h7b6447c_0
gst-plugins-base          1.14.0               hbbd80ab_1
gstreamer                 1.14.0               hb453b48_1
h5py                      2.9.0            py37h7918eee_0
harfbuzz                  1.8.8                hffaf4a1_0
hdf5                      1.10.4               hb1b8bf9_0
heapdict                  1.0.1                      py_0
html5lib                  1.0.1                    py37_0
icu                       58.2                 h9c2bf20_1
idna                      2.8                      py37_0
imageio                   2.6.0                    py37_0
imagesize                 1.1.0                    py37_0
importlib_metadata        0.23                     py37_0
intel-openmp              2019.4                      243
ipykernel                 5.1.2            py37h39e3cac_0
ipython                   7.8.0            py37h39e3cac_0
ipython_genutils          0.2.0                    py37_0
ipywidgets                7.5.1                      py_0
isort                     4.3.21                   py37_0
itsdangerous              1.1.0                    py37_0
jbig                      2.1                  hdba287a_0
jdcal                     1.4.1                      py_0
jedi                      0.15.1                   py37_0
jeepney                   0.4.1                      py_0
jinja2                    2.10.3                     py_0
joblib                    0.13.2                   py37_0
jpeg                      9b                   h024ee3a_2
json5                     0.8.5                      py_0
jsonschema                3.0.2                    py37_0
jupyter                   1.0.0                    py37_7
jupyter_client            5.3.3                    py37_1
jupyter_console           6.0.0                    py37_0
jupyter_core              4.5.0                      py_0
jupyterlab                1.1.4              pyhf63ae98_0
jupyterlab_server         1.0.6                      py_0
keyring                   18.0.0                   py37_0
kiwisolver                1.1.0            py37he6710b0_0
krb5                      1.16.1               h173b8e3_7
lazy-object-proxy         1.4.2            py37h7b6447c_0
libarchive                3.3.3                h5d8350f_5
libcurl                   7.65.3               h20c2e04_0
libedit                   3.1.20181209         hc058e9b_0
libffi                    3.2.1                hd88cf55_4
libgcc-ng                 9.1.0                hdf63c60_0
libgfortran-ng            7.3.0                hdf63c60_0
liblief                   0.9.0                h7725739_2
libpng                    1.6.37               hbc83047_0
libsodium                 1.0.16               h1bed415_0
libssh2                   1.8.2                h1ba5d50_0
libstdcxx-ng              9.1.0                hdf63c60_0
libtiff                   4.0.10               h2733197_2
libtool                   2.4.6                h7b6447c_5
libuuid                   1.0.3                h1bed415_2
libxcb                    1.13                 h1bed415_1
libxml2                   2.9.9                hea5a465_1
libxslt                   1.1.33               h7d1a2b0_0
llvmlite                  0.29.0           py37hd408876_0
locket                    0.2.0                    py37_1
lxml                      4.4.1            py37hefd8a0e_0
lz4-c                     1.8.1.2              h14c3975_0
lzo                       2.10                 h49e0be7_2
markupsafe                1.1.1            py37h7b6447c_0
matplotlib                3.1.1            py37h5429711_0
mccabe                    0.6.1                    py37_1
mistune                   0.8.4            py37h7b6447c_0
mkl                       2019.4                      243
mkl-service               2.3.0            py37he904b0f_0
mkl_fft                   1.0.14           py37ha843d7b_0
mkl_random                1.1.0            py37hd6b4f25_0
mock                      3.0.5                    py37_0
more-itertools            7.2.0                    py37_0
mpc                       1.1.0                h10f8cd9_1
mpfr                      4.0.1                hdf1c602_3
mpmath                    1.1.0                    py37_0
msgpack-python            0.6.1            py37hfd86e86_1
multipledispatch          0.6.0                    py37_0
nbconvert                 5.6.0                    py37_1
nbformat                  4.4.0                    py37_0
ncurses                   6.1                  he6710b0_1
networkx                  2.3                        py_0
nltk                      3.4.5                    py37_0
nose                      1.3.7                    py37_2
notebook                  6.0.1                    py37_0
numba                     0.45.1           py37h962f231_0
numexpr                   2.7.0            py37h9e4a6bb_0
numpy                     1.17.2           py37haad9e8e_0
numpy-base                1.17.2           py37hde5b4d6_0
numpydoc                  0.9.1                      py_0
olefile                   0.46                     py37_0
openpyxl                  3.0.0                      py_0
openssl                   1.1.1d               h7b6447c_2
packaging                 19.2                       py_0
pandas                    0.25.1           py37he6710b0_0
pandoc                    2.2.3.2                       0
pandocfilters             1.4.2                    py37_1
pango                     1.42.4               h049681c_0
parso                     0.5.1                      py_0
partd                     1.0.0                      py_0
patchelf                  0.9                  he6710b0_3
path.py                   12.0.1                     py_0
pathlib2                  2.3.5                    py37_0
patsy                     0.5.1                    py37_0
pcre                      8.43                 he6710b0_0
pep8                      1.7.1                    py37_0
pexpect                   4.7.0                    py37_0
pickleshare               0.7.5                    py37_0
pillow                    6.2.0            py37h34e0f95_0
pip                       19.2.3                   py37_0
pixman                    0.38.0               h7b6447c_0
pkginfo                   1.5.0.1                  py37_0
pluggy                    0.13.0                   py37_0
ply                       3.11                     py37_0
prometheus_client         0.7.1                      py_0
prompt_toolkit            2.0.10                     py_0
psutil                    5.6.3            py37h7b6447c_0
ptyprocess                0.6.0                    py37_0
py                        1.8.0                    py37_0
py-lief                   0.9.0            py37h7725739_2
pycodestyle               2.5.0                    py37_0
pycosat                   0.6.3            py37h14c3975_0
pycparser                 2.19                     py37_0
pycrypto                  2.6.1            py37h14c3975_9
pycurl                    7.43.0.3         py37h1ba5d50_0
pyflakes                  2.1.1                    py37_0
pygments                  2.4.2                      py_0
pylint                    2.4.2                    py37_0
pyodbc                    4.0.27           py37he6710b0_0
pyopenssl                 19.0.0                   py37_0
pyparsing                 2.4.2                      py_0
pyqt                      5.9.2            py37h05f1152_2
pyrsistent                0.15.4           py37h7b6447c_0
pysocks                   1.7.1                    py37_0
pytables                  3.5.2            py37h71ec239_1
pytest                    5.2.1                    py37_0
pytest-arraydiff          0.3              py37h39e3cac_0
pytest-astropy            0.5.0                    py37_0
pytest-doctestplus        0.4.0                      py_0
pytest-openfiles          0.4.0                      py_0
pytest-remotedata         0.3.2                    py37_0
python                    3.7.4                h265db76_1
python-dateutil           2.8.0                    py37_0
python-libarchive-c       2.8                     py37_13
pytz                      2019.3                     py_0
pywavelets                1.0.3            py37hdd07704_1
pyyaml                    5.1.2            py37h7b6447c_0
pyzmq                     18.1.0           py37he6710b0_0
qt                        5.9.7                h5867ecd_1
qtawesome                 0.6.0                      py_0
qtconsole                 4.5.5                      py_0
qtpy                      1.9.0                      py_0
readline                  7.0                  h7b6447c_5
requests                  2.22.0                   py37_0
rope                      0.14.0                     py_0
ruamel_yaml               0.15.46          py37h14c3975_0
scikit-image              0.15.0           py37he6710b0_0
scikit-learn              0.21.3           py37hd81dba3_0
scipy                     1.3.1            py37h7c811a0_0
seaborn                   0.9.0                    py37_0
secretstorage             3.1.1                    py37_0
send2trash                1.5.0                    py37_0
setuptools                41.4.0                   py37_0
simplegeneric             0.8.1                    py37_2
singledispatch            3.4.0.3                  py37_0
sip                       4.19.8           py37hf484d3e_0
six                       1.12.0                   py37_0
snappy                    1.1.7                hbae5bb6_3
snowballstemmer           2.0.0                      py_0
sortedcollections         1.1.2                    py37_0
sortedcontainers          2.1.0                    py37_0
soupsieve                 1.9.3                    py37_0
sphinx                    2.2.0                      py_0
sphinxcontrib             1.0                      py37_1
sphinxcontrib-applehelp   1.0.1                      py_0
sphinxcontrib-devhelp     1.0.1                      py_0
sphinxcontrib-htmlhelp    1.0.2                      py_0
sphinxcontrib-jsmath      1.0.1                      py_0
sphinxcontrib-qthelp      1.0.2                      py_0
sphinxcontrib-serializinghtml 1.1.3                      py_0
sphinxcontrib-websupport  1.1.2                      py_0
spyder                    3.3.6                    py37_0
spyder-kernels            0.5.2                    py37_0
sqlalchemy                1.3.9            py37h7b6447c_0
sqlite                    3.30.0               h7b6447c_0
statsmodels               0.10.1           py37hdd07704_0
sympy                     1.4                      py37_0
tbb                       2019.8               hfd86e86_0
tblib                     1.4.0                      py_0
terminado                 0.8.2                    py37_0
testpath                  0.4.2                    py37_0
tk                        8.6.8                hbc83047_0
toolz                     0.10.0                     py_0
tornado                   6.0.3            py37h7b6447c_0
tqdm                      4.36.1                     py_0
traitlets                 4.3.3                    py37_0
unicodecsv                0.14.1                   py37_0
unixodbc                  2.3.7                h14c3975_0
urllib3                   1.24.2                   py37_0
wcwidth                   0.1.7                    py37_0
webencodings              0.5.1                    py37_1
werkzeug                  0.16.0                     py_0
wheel                     0.33.6                   py37_0
widgetsnbextension        3.5.1                    py37_0
wrapt                     1.11.2           py37h7b6447c_0
wurlitzer                 1.0.3                    py37_0
xlrd                      1.2.0                    py37_0
xlsxwriter                1.2.1                      py_0
xlwt                      1.3.0                    py37_0
xz                        5.2.4                h14c3975_4
yaml                      0.1.7                had09818_2
zeromq                    4.3.1                he6710b0_3
zict                      1.0.0                      py_0
zipp                      0.6.0                      py_0
zlib                      1.2.11               h7b6447c_3
zstd                      1.3.7                h0b5b093_0
(ml_env) [anaconda@CENTOS7 ~]$
```

`ipython`、`jupyter`、`numpy`、`scipy`、`matplotlib`、`scikit-learn`、`pandas`、`pillow`、`msgpack-python`はインストール済みとなっていることがわかります。

従って、`tensorflow`、`keras`、`opencv`、`mglearn`をインストールします。

### 2. `tensorflow`のインストール
`tensorflow`をインストールします。
`conda install tensorflow`

```console:コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ conda install tensorflow
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/anaconda/anaconda3/envs/ml_env

  added / updated specs:
    - tensorflow


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _tflow_select-2.3.0        |              mkl           2 KB
    absl-py-0.8.0              |           py37_0         164 KB
    astor-0.8.0                |           py37_0          46 KB
    c-ares-1.15.0              |    h7b6447c_1001          89 KB
    gast-0.3.2                 |             py_0          13 KB
    google-pasta-0.1.7         |             py_0          41 KB
    grpcio-1.16.1              |   py37hf8bcb03_1         984 KB
    keras-applications-1.0.8   |             py_0          33 KB
    keras-preprocessing-1.1.0  |             py_1          36 KB
    libprotobuf-3.9.2          |       hd408876_0         2.9 MB
    markdown-3.1.1             |           py37_0         118 KB
    openssl-1.1.1d             |       h7b6447c_3         3.7 MB
    protobuf-3.9.2             |   py37he6710b0_0         631 KB
    tensorboard-1.14.0         |   py37hf484d3e_0         3.1 MB
    tensorflow-1.14.0          |mkl_py37h45c423b_0           4 KB
    tensorflow-base-1.14.0     |mkl_py37h7ce6ba3_0        84.4 MB
    tensorflow-estimator-1.14.0|             py_0         291 KB
    termcolor-1.1.0            |           py37_1           8 KB
    ------------------------------------------------------------
                                           Total:        96.5 MB

The following NEW packages will be INSTALLED:

  _tflow_select      pkgs/main/linux-64::_tflow_select-2.3.0-mkl
  absl-py            pkgs/main/linux-64::absl-py-0.8.0-py37_0
  astor              pkgs/main/linux-64::astor-0.8.0-py37_0
  c-ares             pkgs/main/linux-64::c-ares-1.15.0-h7b6447c_1001
  gast               pkgs/main/noarch::gast-0.3.2-py_0
  google-pasta       pkgs/main/noarch::google-pasta-0.1.7-py_0
  grpcio             pkgs/main/linux-64::grpcio-1.16.1-py37hf8bcb03_1
  keras-applications pkgs/main/noarch::keras-applications-1.0.8-py_0
  keras-preprocessi~ pkgs/main/noarch::keras-preprocessing-1.1.0-py_1
  libprotobuf        pkgs/main/linux-64::libprotobuf-3.9.2-hd408876_0
  markdown           pkgs/main/linux-64::markdown-3.1.1-py37_0
  protobuf           pkgs/main/linux-64::protobuf-3.9.2-py37he6710b0_0
  tensorboard        pkgs/main/linux-64::tensorboard-1.14.0-py37hf484d3e_0
  tensorflow         pkgs/main/linux-64::tensorflow-1.14.0-mkl_py37h45c423b_0
  tensorflow-base    pkgs/main/linux-64::tensorflow-base-1.14.0-mkl_py37h7ce6ba3_0
  tensorflow-estima~ pkgs/main/noarch::tensorflow-estimator-1.14.0-py_0
  termcolor          pkgs/main/linux-64::termcolor-1.1.0-py37_1

The following packages will be UPDATED:

  openssl                                 1.1.1d-h7b6447c_2 --> 1.1.1d-h7b6447c_3


Proceed ([y]/n)?
```

インストール、アップデートされるパッケージが表示され、続行するか聞かれるので、`y`を入力します。
`y`

```console:コマンド実行結果
Proceed ([y]/n)? y


Downloading and Extracting Packages
gast-0.3.2           | 13 KB     | ##################################### | 100%
absl-py-0.8.0        | 164 KB    | ##################################### | 100%
openssl-1.1.1d       | 3.7 MB    | ##################################### | 100%
libprotobuf-3.9.2    | 2.9 MB    | ##################################### | 100%
tensorflow-estimator | 291 KB    | ##################################### | 100%
protobuf-3.9.2       | 631 KB    | ##################################### | 100%
keras-applications-1 | 33 KB     | ##################################### | 100%
tensorflow-1.14.0    | 4 KB      | ##################################### | 100%
keras-preprocessing- | 36 KB     | ##################################### | 100%
_tflow_select-2.3.0  | 2 KB      | ##################################### | 100%
google-pasta-0.1.7   | 41 KB     | ##################################### | 100%
termcolor-1.1.0      | 8 KB      | ##################################### | 100%
grpcio-1.16.1        | 984 KB    | ##################################### | 100%
astor-0.8.0          | 46 KB     | ##################################### | 100%
markdown-3.1.1       | 118 KB    | ##################################### | 100%
tensorboard-1.14.0   | 3.1 MB    | ##################################### | 100%
c-ares-1.15.0        | 89 KB     | ##################################### | 100%
tensorflow-base-1.14 | 84.4 MB   | ##################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(ml_env) [anaconda@CENTOS7 ~]$
```
 
`tensorflow`がインストールされました。

### 3. `keras`のインストール
`keras`をインストールします。
`conda install keras`

```console:コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ conda install keras
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/anaconda/anaconda3/envs/ml_env

  added / updated specs:
    - keras


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    keras-2.2.4                |                0           5 KB
    keras-base-2.2.4           |           py37_0         421 KB
    ------------------------------------------------------------
                                           Total:         426 KB

The following NEW packages will be INSTALLED:

  keras              pkgs/main/linux-64::keras-2.2.4-0
  keras-base         pkgs/main/linux-64::keras-base-2.2.4-py37_0


Proceed ([y]/n)?
```

インストールされるパッケージが表示され、続行するか聞かれるので、`y`を入力します。
`y`

```console:コマンド実行結果
Proceed ([y]/n)? y


Downloading and Extracting Packages
keras-2.2.4          | 5 KB      | ##################################### | 100%
keras-base-2.2.4     | 421 KB    | ##################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(ml_env) [anaconda@CENTOS7 ~]$
```
 
`keras`がインストールされました。

### 4. `opencv`のインストール
`opencv`をインストールします。
`conda install opencv`

```console:コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ conda install opencv
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/anaconda/anaconda3/envs/ml_env

  added / updated specs:
    - opencv


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ffmpeg-4.0                 |       hcdf2ecd_0        53.3 MB
    freeglut-3.0.0             |       hf484d3e_5         176 KB
    h5py-2.8.0                 |   py37h989c5e5_3         910 KB
    hdf5-1.10.2                |       hba1933b_1         3.8 MB
    jasper-2.0.14              |       h07fcdf6_1         707 KB
    libglu-9.0.0               |       hf484d3e_1         271 KB
    libopencv-3.4.2            |       hb342d67_1        21.8 MB
    libopus-1.3                |       h7b6447c_0         436 KB
    libvpx-1.7.0               |       h439df22_0         1.2 MB
    opencv-3.4.2               |   py37h6fd60c2_1          11 KB
    py-opencv-3.4.2            |   py37hb342d67_1         1.0 MB
    pytables-3.4.4             |   py37ha205bf6_0         1.2 MB
    ------------------------------------------------------------
                                           Total:        84.9 MB

The following NEW packages will be INSTALLED:

  ffmpeg             pkgs/main/linux-64::ffmpeg-4.0-hcdf2ecd_0
  freeglut           pkgs/main/linux-64::freeglut-3.0.0-hf484d3e_5
  jasper             pkgs/main/linux-64::jasper-2.0.14-h07fcdf6_1
  libglu             pkgs/main/linux-64::libglu-9.0.0-hf484d3e_1
  libopencv          pkgs/main/linux-64::libopencv-3.4.2-hb342d67_1
  libopus            pkgs/main/linux-64::libopus-1.3-h7b6447c_0
  libvpx             pkgs/main/linux-64::libvpx-1.7.0-h439df22_0
  opencv             pkgs/main/linux-64::opencv-3.4.2-py37h6fd60c2_1
  py-opencv          pkgs/main/linux-64::py-opencv-3.4.2-py37hb342d67_1

The following packages will be DOWNGRADED:

  h5py                                 2.9.0-py37h7918eee_0 --> 2.8.0-py37h989c5e5_3
  hdf5                                    1.10.4-hb1b8bf9_0 --> 1.10.2-hba1933b_1
  pytables                             3.5.2-py37h71ec239_1 --> 3.4.4-py37ha205bf6_0


Proceed ([y]/n)?
```

インストール、アップデート、ダウングレードされるパッケージが表示され、続行するか聞かれるので、`y`を入力します。
`y`


```console:コマンド実行結果
Proceed ([y]/n)? y


Downloading and Extracting Packages
ffmpeg-4.0           | 53.3 MB   | ##################################### | 100%
libopencv-3.4.2      | 21.8 MB   | ##################################### | 100%
libglu-9.0.0         | 271 KB    | ##################################### | 100%
freeglut-3.0.0       | 176 KB    | ##################################### | 100%
hdf5-1.10.2          | 3.8 MB    | ##################################### | 100%
h5py-2.8.0           | 910 KB    | ##################################### | 100%
py-opencv-3.4.2      | 1.0 MB    | ##################################### | 100%
jasper-2.0.14        | 707 KB    | ##################################### | 100%
opencv-3.4.2         | 11 KB     | ##################################### | 100%
libvpx-1.7.0         | 1.2 MB    | ##################################### | 100%
libopus-1.3          | 436 KB    | ##################################### | 100%
pytables-3.4.4       | 1.2 MB    | ##################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(ml_env) [anaconda@CENTOS7 ~]$
```

`opencv`がインストールされました。


### 5. `mglearn`のインストール
`mglern`をインストールします。`mglern`は`conda`ではインストールできないので、`pip`でインストールします。
`pip install mglearn`

```console:コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ pip install mglearn
Collecting mglearn
  Downloading https://files.pythonhosted.org/packages/fb/01/8d3630ecc767c9de96a9c46e055f2a3a5f9e14a47d3d0348a36a5005fe67/mglearn-0.1.7.tar.gz (540kB)
     |????????????????????????????????| 542kB 533kB/s
Requirement already satisfied: numpy in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from mglearn) (1.17.2)
Requirement already satisfied: matplotlib in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from mglearn) (3.1.1)
Requirement already satisfied: scikit-learn in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from mglearn) (0.21.3)
Requirement already satisfied: pandas in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from mglearn) (0.25.1)
Requirement already satisfied: pillow in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from mglearn) (6.2.0)
Requirement already satisfied: cycler in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from mglearn) (0.10.0)
Requirement already satisfied: imageio in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from mglearn) (2.6.0)
Requirement already satisfied: kiwisolver>=1.0.1 in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from matplotlib->mglearn) (1.1.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from matplotlib->mglearn) (2.4.2)
Requirement already satisfied: python-dateutil>=2.1 in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from matplotlib->mglearn) (2.8.0)
Requirement already satisfied: scipy>=0.17.0 in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from scikit-learn->mglearn) (1.3.1)
Requirement already satisfied: joblib>=0.11 in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from scikit-learn->mglearn) (0.13.2)
Requirement already satisfied: pytz>=2017.2 in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from pandas->mglearn) (2019.3)
Requirement already satisfied: six in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from cycler->mglearn) (1.12.0)
Requirement already satisfied: setuptools in ./anaconda3/envs/ml_env/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib->mglearn) (41.4.0)
Building wheels for collected packages: mglearn
  Building wheel for mglearn (setup.py) ... done
  Created wheel for mglearn: filename=mglearn-0.1.7-py2.py3-none-any.whl size=582705 sha256=8469eec58ee79211eb68796729d3aab8f0e2ef43f7797a424b5d53d864b4e234
  Stored in directory: /home/anaconda/.cache/pip/wheels/74/cf/8d/04f4932d15854a36726c6210763c7127e62de28f5c8ddfcf3b
Successfully built mglearn
Installing collected packages: mglearn
Successfully installed mglearn-0.1.7
(ml_env) [anaconda@CENTOS7 ~]$
```

`mglearn`がインストールされました。




### 6. `Jupyter Notebook`でインストールの確認
`Jupyter Notebook`でライブラリが`import`できるか確認します。

#### 6.1 `Jupyter Notebook`を起動します。
`jupyter notebook`

```console:コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ jupyter notebook
[W 10:57:39.026 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 10:57:39.053 NotebookApp] JupyterLab extension loaded from /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/jupyterlab
[I 10:57:39.053 NotebookApp] JupyterLab application directory is /home/anaconda/anaconda3/envs/ml_env/share/jupyter/lab
[I 10:57:39.054 NotebookApp] Serving notebooks from local directory: /home/anaconda/work
[I 10:57:39.055 NotebookApp] The Jupyter Notebook is running at:
[I 10:57:39.055 NotebookApp] http://CENTOS7:8888/
[I 10:57:39.055 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
```

### 6.2 `jupyter notebook`ライブラリをインポート
`jupyter notebook`で以下のソースを実行し、ライブラリがインポートできることを確認します。

```py:test01.py
import tensorflow
import keras
import cv2
import mglearn
```

以下の警告が出力されますが、エラーが発生していなければインストールはOKです。

```console:実行結果
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:516: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:517: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:518: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:519: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:520: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:525: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: 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)])
Using TensorFlow backend.
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/sklearn/externals/six.py:31: DeprecationWarning: The module is deprecated in version 0.21 and will be removed in version 0.23 since we've dropped support for Python 2.7. Please rely on the official version of six (https://pypi.org/project/six/).
  "(https://pypi.org/project/six/).", DeprecationWarning)
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/sklearn/externals/joblib/__init__.py:15: DeprecationWarning: sklearn.externals.joblib is deprecated in 0.21 and will be removed in 0.23. Please import this functionality directly from joblib, which can be installed with: pip install joblib. If this warning is raised when loading pickled models, you may need to re-serialize those models with scikit-learn 0.21+.
  warnings.warn(msg, category=DeprecationWarning)
```

【jupyter notebookの実行結果画面】
![J01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/292434/944197f5-1695-3bc6-0af6-501013788f6e.png)


### 7. `Jupyter Notebook`でサンプルソースの動作確認
#### 7.1 `scikit-learn`による機械学習のサンプルソースの動作確認
`jupyter notebook`で`scikit-learn`を使用した以下のソースが実行できることを確認します。
（警告は無視してよいです。）

```py:test02.py
from sklearn.model_selection import train_test_split
from sklearn import datasets, svm, metrics
from sklearn.metrics import accuracy_score

# データを読み込む 
digits = datasets.load_digits()
x = digits.images
y = digits.target
x = x.reshape((-1, 64)) # 二次元配列を一次元配列に変換 

# データを学習用とテスト用に分割する 
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)

# データを学習  （アルゴリズムSVM：（サポートベクトルマシン/サポートベクターマシン））
clf = svm.LinearSVC()
clf.fit(x_train, y_train)

# 予測して精度を確認する
y_pred = clf.predict(x_test)
print(accuracy_score(y_test, y_pred))
```
【jupyter notebookの実行結果画面】
![J02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/292434/e8a5da6d-b1bc-8d64-df55-e14ffcf19bf3.png)


####7.2 `tensorflow + keras`による深層学習のサンプルソースの動作確認
`jupyter notebook`で`tensorflow + keras`を使用した以下のソースが実行できることを確認します。
（警告は無視してよいです。）

```py:test03.py
%matplotlib inline
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
from keras.datasets import mnist
import matplotlib.pyplot as plt

# 入力と出力を指定
in_size = 28 * 28
out_size = 10

# MNISTのデータを読み込み 
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# データを28*28=784の一次元配列に変換
X_train = X_train.reshape(-1, 784).astype('float32') / 255
X_test = X_test.reshape(-1, 784).astype('float32') / 255
# ラベルデータをone-hotベクトルに直す
y_train = keras.utils.np_utils.to_categorical(y_train.astype('int32'),10)
y_test = keras.utils.np_utils.to_categorical(y_test.astype('int32'),10)

# MLPモデル構造を定義 
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(in_size,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(out_size, activation='softmax'))

# モデルをコンパイル 
model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy'])

# 学習を実行 
hist = model.fit(X_train, y_train, batch_size=128, epochs=50, verbose=1, validation_data=(X_test, y_test))

# モデルを評価 
score = model.evaluate(X_test, y_test, verbose=1)
print('正解率=', score[1], 'loss=', score[0])

# 学習の様子をグラフへ描画
# 正解率の推移をプロット
plt.plot(hist.history['acc'])
plt.plot(hist.history['val_acc'])
plt.title('Accuracy')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# ロスの推移をプロット
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Loss')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
```

```console:実行結果
Using TensorFlow backend.
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:516: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:517: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:518: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:519: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:520: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:525: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: 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)])
/home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: 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)])
WARNING:tensorflow:From /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

WARNING:tensorflow:From /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

WARNING:tensorflow:From /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.

WARNING:tensorflow:From /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
WARNING:tensorflow:From /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

WARNING:tensorflow:From /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3295: The name tf.log is deprecated. Please use tf.math.log instead.

WARNING:tensorflow:From /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/tensorflow/python/ops/math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Train on 60000 samples, validate on 10000 samples
Epoch 1/50
60000/60000 [==============================] - 10s 161us/step - loss: 0.2458 - acc: 0.9236 - val_loss: 0.1026 - val_acc: 0.9684
Epoch 2/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.1012 - acc: 0.9695 - val_loss: 0.0773 - val_acc: 0.9760
Epoch 3/50
60000/60000 [==============================] - 9s 156us/step - loss: 0.0725 - acc: 0.9787 - val_loss: 0.0696 - val_acc: 0.9793
Epoch 4/50
60000/60000 [==============================] - 9s 158us/step - loss: 0.0598 - acc: 0.9824 - val_loss: 0.0682 - val_acc: 0.9809
Epoch 5/50
60000/60000 [==============================] - 9s 155us/step - loss: 0.0504 - acc: 0.9851 - val_loss: 0.0790 - val_acc: 0.9797
Epoch 6/50
60000/60000 [==============================] - 9s 158us/step - loss: 0.0435 - acc: 0.9872 - val_loss: 0.0722 - val_acc: 0.9811
Epoch 7/50
60000/60000 [==============================] - 9s 154us/step - loss: 0.0377 - acc: 0.9885 - val_loss: 0.0849 - val_acc: 0.9799
Epoch 8/50
60000/60000 [==============================] - 9s 156us/step - loss: 0.0364 - acc: 0.9892 - val_loss: 0.0740 - val_acc: 0.9840
Epoch 9/50
60000/60000 [==============================] - 9s 155us/step - loss: 0.0314 - acc: 0.9912 - val_loss: 0.0766 - val_acc: 0.9836
Epoch 10/50
60000/60000 [==============================] - 9s 153us/step - loss: 0.0295 - acc: 0.9909 - val_loss: 0.0764 - val_acc: 0.9828
Epoch 11/50
60000/60000 [==============================] - 10s 159us/step - loss: 0.0258 - acc: 0.9928 - val_loss: 0.0865 - val_acc: 0.9843
Epoch 12/50
60000/60000 [==============================] - 9s 151us/step - loss: 0.0246 - acc: 0.9930 - val_loss: 0.0901 - val_acc: 0.9832
Epoch 13/50
60000/60000 [==============================] - 9s 153us/step - loss: 0.0225 - acc: 0.9936 - val_loss: 0.0983 - val_acc: 0.9823
Epoch 14/50
60000/60000 [==============================] - 9s 155us/step - loss: 0.0214 - acc: 0.9944 - val_loss: 0.1021 - val_acc: 0.9825
Epoch 15/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0220 - acc: 0.9940 - val_loss: 0.0872 - val_acc: 0.9845
Epoch 16/50
60000/60000 [==============================] - 10s 172us/step - loss: 0.0218 - acc: 0.9943 - val_loss: 0.1009 - val_acc: 0.9827
Epoch 17/50
60000/60000 [==============================] - 10s 159us/step - loss: 0.0199 - acc: 0.9944 - val_loss: 0.0966 - val_acc: 0.9837
Epoch 18/50
60000/60000 [==============================] - 9s 156us/step - loss: 0.0197 - acc: 0.9950 - val_loss: 0.1083 - val_acc: 0.9829
Epoch 19/50
60000/60000 [==============================] - 9s 155us/step - loss: 0.0159 - acc: 0.9955 - val_loss: 0.1198 - val_acc: 0.9819
Epoch 20/50
60000/60000 [==============================] - 9s 156us/step - loss: 0.0175 - acc: 0.9949 - val_loss: 0.1089 - val_acc: 0.9849
Epoch 21/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0172 - acc: 0.9954 - val_loss: 0.1113 - val_acc: 0.9843
Epoch 22/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0159 - acc: 0.9959 - val_loss: 0.1062 - val_acc: 0.9848
Epoch 23/50
60000/60000 [==============================] - 9s 151us/step - loss: 0.0162 - acc: 0.9960 - val_loss: 0.1157 - val_acc: 0.9838
Epoch 24/50
60000/60000 [==============================] - 9s 155us/step - loss: 0.0143 - acc: 0.9963 - val_loss: 0.1195 - val_acc: 0.9841
Epoch 25/50
60000/60000 [==============================] - 9s 156us/step - loss: 0.0171 - acc: 0.9958 - val_loss: 0.1180 - val_acc: 0.9840
Epoch 26/50
60000/60000 [==============================] - 9s 153us/step - loss: 0.0172 - acc: 0.9960 - val_loss: 0.1198 - val_acc: 0.9848
Epoch 27/50
60000/60000 [==============================] - 9s 158us/step - loss: 0.0136 - acc: 0.9964 - val_loss: 0.1101 - val_acc: 0.9861
Epoch 28/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0145 - acc: 0.9968 - val_loss: 0.1194 - val_acc: 0.9850
Epoch 29/50
60000/60000 [==============================] - 10s 164us/step - loss: 0.0145 - acc: 0.9964 - val_loss: 0.1306 - val_acc: 0.9825
Epoch 30/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0142 - acc: 0.9966 - val_loss: 0.1237 - val_acc: 0.9856
Epoch 31/50
60000/60000 [==============================] - 9s 153us/step - loss: 0.0148 - acc: 0.9967 - val_loss: 0.1366 - val_acc: 0.9832
Epoch 32/50
60000/60000 [==============================] - 9s 156us/step - loss: 0.0148 - acc: 0.9965 - val_loss: 0.1185 - val_acc: 0.9857
Epoch 33/50
60000/60000 [==============================] - 10s 158us/step - loss: 0.0122 - acc: 0.9972 - val_loss: 0.1239 - val_acc: 0.9837
Epoch 34/50
60000/60000 [==============================] - 9s 157us/step - loss: 0.0150 - acc: 0.9965 - val_loss: 0.1289 - val_acc: 0.9826
Epoch 35/50
60000/60000 [==============================] - 10s 162us/step - loss: 0.0131 - acc: 0.9970 - val_loss: 0.1375 - val_acc: 0.9829
Epoch 36/50
60000/60000 [==============================] - 9s 158us/step - loss: 0.0133 - acc: 0.9971 - val_loss: 0.1265 - val_acc: 0.9845
Epoch 37/50
60000/60000 [==============================] - 10s 159us/step - loss: 0.0107 - acc: 0.9974 - val_loss: 0.1365 - val_acc: 0.9842
Epoch 38/50
60000/60000 [==============================] - 10s 159us/step - loss: 0.0134 - acc: 0.9970 - val_loss: 0.1377 - val_acc: 0.9846
Epoch 39/50
60000/60000 [==============================] - 9s 157us/step - loss: 0.0139 - acc: 0.9971 - val_loss: 0.1560 - val_acc: 0.9815
Epoch 40/50
60000/60000 [==============================] - 9s 157us/step - loss: 0.0121 - acc: 0.9969 - val_loss: 0.1388 - val_acc: 0.9839
Epoch 41/50
60000/60000 [==============================] - 9s 157us/step - loss: 0.0163 - acc: 0.9971 - val_loss: 0.1379 - val_acc: 0.9843
Epoch 42/50
60000/60000 [==============================] - 9s 157us/step - loss: 0.0126 - acc: 0.9973 - val_loss: 0.1313 - val_acc: 0.9844
Epoch 43/50
60000/60000 [==============================] - 9s 157us/step - loss: 0.0139 - acc: 0.9972 - val_loss: 0.1480 - val_acc: 0.9825
Epoch 44/50
60000/60000 [==============================] - 9s 157us/step - loss: 0.0122 - acc: 0.9974 - val_loss: 0.1434 - val_acc: 0.9840
Epoch 45/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0120 - acc: 0.9975 - val_loss: 0.1459 - val_acc: 0.9843
Epoch 46/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0139 - acc: 0.9971 - val_loss: 0.1318 - val_acc: 0.9851
Epoch 47/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0110 - acc: 0.9977 - val_loss: 0.1363 - val_acc: 0.9850
Epoch 48/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0108 - acc: 0.9977 - val_loss: 0.1417 - val_acc: 0.9832
Epoch 49/50
60000/60000 [==============================] - 9s 152us/step - loss: 0.0144 - acc: 0.9971 - val_loss: 0.1478 - val_acc: 0.9835
Epoch 50/50
60000/60000 [==============================] - 9s 151us/step - loss: 0.0107 - acc: 0.9978 - val_loss: 0.1319 - val_acc: 0.9852
10000/10000 [==============================] - 1s 75us/step
正解率= 0.9852 loss= 0.13187411594858459
```
![J03.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/292434/4594cda7-275f-8e27-02f8-0742c3a2ab38.png)
![J04.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/292434/d04b1f55-a6cf-d824-d99f-53361bbad6ae.png)

動作が確認できました。

以上

# リンク
[【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）](https://qiita.com/yasushi-jp/items/7cef8447062fb7dd200c "【第一回】CentOSにPythonの機械学習開発環境を構築する（Anacondaインストール編）")
[【第二回】CentOSにPythonの機械学習開発環境を構築する（Jupyter Notebook編）](https://qiita.com/yasushi-jp/items/d06efa771af7a0d8921d "【第二回】CentOSにPythonの機械学習開発環境を構築する（Jupyter Notebook編）")
[【番外編】CentOSにPythonの機械学習開発環境を構築する（systemdでのJupyter Notebookの自動起動編）](https://qiita.com/yasushi-jp/items/77eabdfb237ea1de99ca "【番外編】CentOSにPythonの機械学習開発環境を構築する（systemdでのJupyter Notebookの自動起動編）")