Saturday, December 29, 2018

How to start machine learning with Keras (Windows)

Prerequisites

Install Python3.6

Tensorflow is necessary for machine learning with Keras but Tensorflow needs Python3.5 or 3.6. We will use Python3.6. Install it from here: https://www.python.org/downloads/release/python-363/
Scroll down until you find the following: 

Then you will get an installer. Double-click on it. 

Don't forget to tick "Add Python3.6 to PATH". It is not ticked by default. 

Open command prompt or windows powershell. If it was already open, please re-open the command prompt or powershell. Then run the following command:
python --version
If the version information is displayed like this, you installed the python successfully. 

Just in case, check if pip is working also.
pip --version

pip 10.0.1 from c:\users\shu\appdata\local\programs\python\python36\lib\site-packages\pip (python 3.6)

Install Tensorflow and Keras

You DO have Nvidia GPU?

If you have Nvidia GPU, you can use tensorflow GPU version. GPU version is much faster than the CPU version. If you don't have Nvidia GPU, install the CPU version (which is described below.) Cuda and cuDNN are not necessary for CPU version.

Cuda9.0

To use Nvidia GPU for tensorflow, you need Cuda9.0. Other versions of Cuda can not be used.So we will install Cuda9.0 from the official website. Don't install Cuda9.1 because it can not be used for Tensorflow. Always check the requirement on the website of Tensorflow.
Follow their guidance to install it.

cuDNNv7.0

Also you need cuDNN v7.0. Other versions can not be used, so don't download v7.1. Download it from here: https://developer.nvidia.com/rdp/cudnn-archive
Seems like cuDNN v7.0 for Windows8.1 is missing. I think cuDNN v7.0 for Windows7 can be used in that case.

Tensorflow-gpu

Install as follows:
pip install tensorflow-gpu
And install Keras also:
pip install keras

You DON'T have GPU?

If you don't have Nvidia GPU, we need to use CPU version of Tensorflow. We will install Tensorflow CPU version and Keras with pip. Please make sure you are using Python3's pip. You can check which pip is being used by "pip --version". Run the following command to install Tensorflow on command prompt or powershell:
pip install tensorflow
And install Keras also:
pip install keras

Other modules

Install other necessary modules with pip:
pip install matplotlib
pip install h5py
pip install numpy

Try first machine learning

Now we will run machine learning scripts. Save the following as "test.py":
#!/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])
On the command prompt or powershell, move to the directory and run the script:
python test.py
Then the machine learning (deep learning) will be started: 

It is classifying handwritten digit images. The accuracy is 88.94%. Great!

Other scripts of machine learning?

You can find more examples on the github page of keras-team.