Sunday, August 13, 2017

Install Keras and test deep learning

Deep Learning

Preparation
1.  Virtual box and Vagrant

2.  Install Apache

3.  Install MySQL

4.  Install Python

5.  Hello World with Python

Deep learning programming
1. Install Keras and test deep learning

2. Save learned parameters and load the parameters

3. Save and load at a same time

4. Use own dataset

Install Keras and test deep learning


At first, we need to install Keras.
If your PC has GPU:
$ python3.6 -m pip install tensorflow-gpu
$ python3.6 -m pip install keras
(If your pc has nvidia gpu, you need cuda for tesnforflow. Install it from here like this: $ sudo apt-get install cuda-9.0)

If your PC doesn't have GPU:
$ python3.6 -m pip install tensorflow
$ python3.6 -m pip install keras

And matplotlib to visualize the learning.
$ sudo yum -y install gcc gcc-c++ kernel-devel
$ sudo yum -y install python-devel libxslt-devel libffi-devel openssl-devel
$ python3.6 -m pip install matplotlib

h5py also. This is to save the model.
$ python3.6 -m pip install h5py

Write code like this
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.utils import np_utils

# Get the default MNIST data
(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(60000, 784) / 255
X_test = X_test.reshape(10000, 784) / 255

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

# Network
model = Sequential([
        Dense(512, input_shape=(784,)),
        Activation('sigmoid'),
        Dense(10),
        Activation('softmax')
    ])

# Compile
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

# Learning
model.fit(X_train, y_train, batch_size=200, verbose=1, epochs=20, validation_split=0.1)

# Forecast
score = model.evaluate(X_test, y_test, verbose=1)
print('test accuracy : ', score[1])

And save it as "dplrn.py" in the shared folder.

Then we will execute it as follows:
$ python3.6 /vagrant/dplrn.py

Then deep learning starts:

And in the end, the test accuracy is displayed:


Seems like my AI's test accuracy is about 89%. wow :)

Other examples can be found here.