Deep Learningをいまひとつわかっていないが、とりあえずなにか動かしてみたかったので、環境をつくって動かしてみた
実行環境
- Windows 10 Pro 1903
- GeForce GTX 1070 (Game Ready Driver: 431.60)
- Anaconda3 2019.07
- Keras 2.2.4
- TnesorFlow 1.14.0
Windowsで環境構築をしていますが、Anacondaを使用していればMacでもあまりかわらないはず
Python環境構築
Anaconda3で専用環境を構築する
$ conda create -n keras python=3.7 anaconda
~snip~
done
#
# To activate this environment, use
#
# $ conda activate keras
#
# To deactivate an active environment, use
#
# $ conda deactivate
環境ができたら、Kerasをインストール
$ conda activate keras
$ pip install keras
TensorFlowのインストール
TensorFlowをGPUで動かす場合は、condaコマンドでcuDNNとCUDA Toolkitをインストールする。CPUのみで動かす場合は不要。
$ conda install cudnn cudatoolkit=10.0.130
TensorFlow-1.14.0ではCUDA 10.0に依存していたため、CUDA Toolkitのバージョンを指定した
その後、GPU版TensorFlowを入れる
$ pip install tensorflow-gpu
※CPUのみで動かす場合は
$ pip install tensorflow
サンプル実行
KerasのGitHubリポジトリに手書き文字のサンプルがあるのでそれをダウンロードして実行する
https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py
コメントによると、12エポック後に99.25%のtest accuracyなるらしい(よくわかってない
$ python mnist_cnn.py
Using TensorFlow backend.
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples
Epoch 1/12
60000/60000 [==============================] - 13s 214us/step - loss: 0.2791 - acc: 0.9128 - val_loss: 0.0559 - val_acc: 0.9811
Epoch 2/12
60000/60000 [==============================] - 7s 116us/step - loss: 0.0877 - acc: 0.9739 - val_loss: 0.0445 - val_acc: 0.9851
Epoch 3/12
60000/60000 [==============================] - 7s 125us/step - loss: 0.0662 - acc: 0.9793 - val_loss: 0.0351 - val_acc: 0.9872
Epoch 4/12
60000/60000 [==============================] - 7s 117us/step - loss: 0.0535 - acc: 0.9842 - val_loss: 0.0311 - val_acc: 0.9887
Epoch 5/12
60000/60000 [==============================] - 7s 117us/step - loss: 0.0478 - acc: 0.9858 - val_loss: 0.0285 - val_acc: 0.9893
Epoch 6/12
60000/60000 [==============================] - 7s 116us/step - loss: 0.0434 - acc: 0.9875 - val_loss: 0.0285 - val_acc: 0.9901
Epoch 7/12
60000/60000 [==============================] - 8s 127us/step - loss: 0.0377 - acc: 0.9884 - val_loss: 0.0324 - val_acc: 0.9893
Epoch 8/12
60000/60000 [==============================] - 7s 124us/step - loss: 0.0354 - acc: 0.9891 - val_loss: 0.0256 - val_acc: 0.9918
Epoch 9/12
60000/60000 [==============================] - 7s 115us/step - loss: 0.0319 - acc: 0.9902 - val_loss: 0.0270 - val_acc: 0.9918
Epoch 10/12
60000/60000 [==============================] - 7s 121us/step - loss: 0.0296 - acc: 0.9906 - val_loss: 0.0268 - val_acc: 0.9917
Epoch 11/12
60000/60000 [==============================] - 7s 124us/step - loss: 0.0286 - acc: 0.9917 - val_loss: 0.0289 - val_acc: 0.9909
Epoch 12/12
60000/60000 [==============================] - 7s 115us/step - loss: 0.0271 - acc: 0.9917 - val_loss: 0.0287 - val_acc: 0.9910
Test loss: 0.0286549659186252
Test accuracy: 0.991
99.1%とちょっと低いが一応動作していることが確認出来た
ちなみに、CPUで動かすと
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples
Epoch 1/12
60000/60000 [==============================] - 39s 645us/step - loss: 0.2616 - acc: 0.9200 - val_loss: 0.0581 - val_acc: 0.9811
Epoch 2/12
60000/60000 [==============================] - 38s 641us/step - loss: 0.0890 - acc: 0.9743 - val_loss: 0.0434 - val_acc: 0.9864
Epoch 3/12
60000/60000 [==============================] - 39s 646us/step - loss: 0.0676 - acc: 0.9798 - val_loss: 0.0339 - val_acc: 0.9879
Epoch 4/12
60000/60000 [==============================] - 39s 650us/step - loss: 0.0565 - acc: 0.9836 - val_loss: 0.0373 - val_acc: 0.9875
Epoch 5/12
60000/60000 [==============================] - 39s 645us/step - loss: 0.0460 - acc: 0.9858 - val_loss: 0.0313 - val_acc: 0.9898
Epoch 6/12
60000/60000 [==============================] - 38s 641us/step - loss: 0.0414 - acc: 0.9872 - val_loss: 0.0295 - val_acc: 0.9901
Epoch 7/12
60000/60000 [==============================] - 39s 644us/step - loss: 0.0369 - acc: 0.9887 - val_loss: 0.0297 - val_acc: 0.9905
Epoch 8/12
60000/60000 [==============================] - 39s 644us/step - loss: 0.0352 - acc: 0.9898 - val_loss: 0.0269 - val_acc: 0.9915
Epoch 9/12
60000/60000 [==============================] - 39s 647us/step - loss: 0.0304 - acc: 0.9908 - val_loss: 0.0291 - val_acc: 0.9909
Epoch 10/12
60000/60000 [==============================] - 39s 645us/step - loss: 0.0284 - acc: 0.9912 - val_loss: 0.0268 - val_acc: 0.9915
Epoch 11/12
60000/60000 [==============================] - 39s 642us/step - loss: 0.0280 - acc: 0.9916 - val_loss: 0.0279 - val_acc: 0.9906
Epoch 12/12
60000/60000 [==============================] - 38s 640us/step - loss: 0.0242 - acc: 0.9922 - val_loss: 0.0246 - val_acc: 0.9922
Test loss: 0.02456222619962118
Test accuracy: 0.9922