Machine learning with Tensorflow & Python (English)

tensorflow machine learning

How to make your first machine learning script with Python!

TensorFlow is a machine learning framework developped by Google Brain. It is easily available in Python and it’s free to use!

To start with Tensorflow you can use one of the many available datasets. One of these datasets is MNIST, a collection of handwritten digits. The goal of this dataset is to train a model that can correctly predict which digit is written.

(With many styles of handwriting this prediction may be harder than you think…)

Below you’ll find all the necessary steps and a Pyhton script. Try it yourself!

* You’ll need the matplotlib and numpy libaries to run the script.


PIP: Install the Tensorflow Library

To install the Tensorflow Library run the following pip command:

pip install -upgrade tensorflow

Notebook: Making your first Tensorflow script

Let the (easy) coding begin. In this example a Jupyter Notebook is used, but you can use any type of editor you want.

1 – Import

In the first step we’ll import the TensorFlow library including the MNIST dataset:

import tensorflow as tf

mnist = tf.keras.datasets.mnist

As you can see Tensorflow will be callable with “tf” and the dataset is named “mnist”.

2 – Train & Test set

Splitting data in a train and test set is a very important step in making a machine learning model. The train set is used by the model to learn the already existing answers and related data values. This way it can predict the answers for new records, where only the data values are known.

That’s why we have a test set; This set is not used in the training of the model. Therefore the data values are new. Next, the model predicts the answers of the test set. By comparing the predicted answers with the real answers of the test set, we’ll find out how the model performs.

Thanks to the Tensorflow library splitting data is an easy task. The result is 4 datasets:

– y_train = the value you are going to be predicting (the answer)

– x_train = the columns (data values) upon which the prediction is made

– x_test =  the columns (data values) that are used to test the model after it has been trained

– y_test = this are the (real) answers related to the data from x_test. The y_test answers will be compared to the predicted answers given by the model to see how the model performs.

The code to make these 4 datasets:

(x_train, y_train), (x_test, y_test) = mnist.load_data()

3 – Example of the dataset

Let’s check the first row of x_train and y_train:

y_train row 1 =  [5]

x_train row 1 =  28 rows x 28 columns with values between 0 and 255

This may sounds weird, but it’s just a table view of an image: 28×28 pixels with 255 color codes. So x_train contains the pixels of the image and y_train contains the digit that is written in the image.

In the next step we’ll transform the values between 0 and 255 to a smaller range. After this step all values will be between 0 and 1. This is called normalization. Normalization makes it easier to train a model and predict the right digit.

x_train = tf.keras.utils.normalize(x_train, axis=1)

x_test = tf.keras.utils.normalize(x_test, axis=1)

Side note: As you can see, the data set MNIST is completely prepared for these Tensorflow tasks. Normally this is not the case and you’ll need to do a lot of data pre-processing before data can be used for these tasks.

4 – Check

Do you want to know the first digit of the x_train set? Run the following code:

import matplotlib.pyplot as plt

plt.imshow(x_train[0], cmap = plt.cm.binary)

plt.show

tensorflow-mnist
The first row ([0]) of x_train contains an image of a handwritten 5.

5 – Model

After the data is prepared for the model, the training can begin!

The following code creates a model and adds 4 layers. The layers create the neural network. A neural network multiplies the values an corrects it with weights to generate the right output. In this case, the output is the answer which digit is written.

In the .compile statement the model is given instructions and settings. You can choose a lot of different settings to tune the model.

The last step is to provide the model with the training data. The last line of code sets the amount of iterations (epochs) to 3. After loading the training data 3 times the model is completed. This code may takes a while to run.

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))

model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))

model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=3)

Let’s check the result:

tensorflow epochs

The screenshot shows that the model is trained by 6000 samples. These samples have trained the model in 3 iterations. At each iteration the accuracy of the model increased.

Good job, you’ve trained your first machine learning model!

6 – Evaluation & Saving the model

In the next step we are going to check the performance of the model. The x_test and y_test sets are used to calculate the accuracy. The code below calculates and shows the accuracy results.

val_loss, val_acc = model.evaluate(x_test,y_test)

print(val_loss, val_acc)

You can also save the model if the performance is good enough for your requirements:

model.save('model_test_123')

new_model= tf.keras.models.load_model('model_test_123')

7 – Making predictions

The model is now named: new_model. This model can be used for new datasets. Let’s make a list of predictions based upon the x_test dataset.

predictions = new_model.predict([x_test])

Check

Now that we’ve got a list with predictions, we can check if it’s the right prediction. Numpy will help us with this task. The code below picks en prints row [7] of the predictions list. (This is the 8th row of the list, because Pyhton starts counting at 0)

import numpy as np

print(np.argmax(predictions[7]))

Result = Prediction: 9

Now we must check if the image is also a handwritten 9. This can be done by printing x_test[7]. This row must be visualised by Matplotplib, because it’s the data of an image.

plt.imshow(x_test[7])

plt.show

Result = Also a 9 !

Success!

That’s how easy it is! The main purpose of these steps is to understand the basic principles of Tensorflow. After a few lines of codes we are now able to predict handwritten digits.

Try reading the code en see if it is clear to you what the functions do. This example has not gone through lots of details and the dataset was perfectly pre-processed. This hardly ever happens in real life. But this tutorial is a nice step to more complex projects.

You can look up the Google documentation about Tensorflow or check Kaggle and YouTube for more inspiration.

More about Tensorflow: www.tensorflow.org


The full code

Below you’ll find the full code for a Jupyter notebook.

If you are using a different editor, the Matplotlib functions may need a bit of extra code to run the visualisation.


import tensorflow as tf

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = tf.keras.utils.normalize(x_train, axis=1)

x_test = tf.keras.utils.normalize(x_test, axis=1)

import matplotlib.pyplot as plt

plt.imshow(x_train[0], cmap = plt.cm.binary)

plt.show

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))

model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))

model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=3)

val_loss, val_acc = model.evaluate(x_test,y_test)

print(val_loss, val_acc)

model.save('model_test_123')

new_model= tf.keras.models.load_model('model_test_123')

predictions = new_model.predict([x_test])

import numpy as np

print(np.argmax(predictions[7]))

plt.imshow(x_test[7])

plt.show


Deze tutorial is ook beschikbaar in het Nederlands / This tutorial is also available in Dutch.