Prerequisites
- 64-bit desktops or laptops
- Ubuntu 16.04
- Tensorflow official, Keras official
See here about the requirements.
Python3.5 or 3.6
We will use Python3.5. Why? Because it is the easiest. Python3.5 is pre-installed by default. Check what version of python is in your ubuntu:
$ python3.5 --version
Python 3.5.2
Also check pip too:
$ sudo python3.5 -m pip install --upgrade pip
$ sudo python3.5 -m pip --version
pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
Maybe python3.6 is pre-installed in Ubuntu18. Python3.6 is also ok.
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.
Although there are some bugs related to Nvidia GPU driver in Ubuntu...(like nomodeset things) You can google with "Ubuntu nomodeset nvidia" to see what the bug is. To install Nvidia driver in Ubuntu, you need to add nomodeset option in the boot option (otherwise ubuntu is not booting with Nvidia card), and after that, install a proper driver, then re-boot ubuntu.
Maybe you can see here for how to install Ubuntu along with Nvidia GPU driver.
Cuda9.0
To use Nvidia GPU for tensorflow, you need Cuda9.0. Other versions of Cuda can not be used.So we will download & 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.
If you use deb file, maybe you need to install it like this:
$ sudo apt-get install cuda-9.0
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
Tensorflow-gpu
Install as follows:
$ sudo python3.5 -m pip install tensorflow-gpu
And install Keras also:
$ sudo python3.5 -m 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:
$ sudo python3.5 -m pip install tensorflow
And install Keras also:
$ sudo python3.5 -m pip install keras
Other modules
Install other necessary modules with pip:
$ sudo python3.5 -m pip install matplotlib
$ sudo python3.5 -m pip install h5py
$ sudo python3.5 -m 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:
$ python3.5 test.py
Then the machine learning (deep learning) will be started:
It is classifying handwritten digit images. The accuracy is 88.85%. Great!
Other scripts of machine learning?
You can find more examples on the github page of keras-team.