A practical example on TensorFlow

In this tutorial I wrote an example to use TensorFlow in machine learning. It is a classic example of classification and recognition, known as iris model.

Prerequisites. To run this tutorial you must have already installed python3 and TensorFlow on your computer. Unfortunately, Google often changes the name of the methods to the new TensorFlow releases. Therefore, if the instructions in this tutorial go wrong, replace them with the latest ones. Fortunately, the method to replace them is indicated in the error messages themselves.

How TensorFlow works

At the beginning of the python program, I insert the import statement of the numpy and tensorflow libraries.

import numpy as np
import tensorflow as tf

In a table in CSV format there are the measures of four characteristics of the flowers.

These are the independent x variables of the problem.

  • Sepal length
  • Sepal width
  • Petal length
  • Petal width

le 4 caratteristiche delle 3 specie di fiori Iris

In each row there is also indicated the species code (silky, virginica, versicolor) of the Iris flower.

It is the dependent variable y of the problem.

  • 0 = Iris setosa
  • 1 = Iris virginica
  • 2 = Iris versicolor

This table is called dataset training or training set.

la tabella training set del modello IRIS

Note. The training set consists of 120 examples. The CSV files must be downloaded ( iris_training.csv ) and saved in a computer folder or on a pen drive.

In the python program I indicate the name and the path (path) of the csv table on the PC.

IRIS_TRAINING = "../_data/iris_training.csv"

Then I load the dataset into the training_set variable

training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING,
target_dtype=np.int,
features_dtype=np.float32)

From this table TensorFlow must build a "function" y≅f(x).

This function is a neural network and is called a model.

la funzione obiettivo del modello

How does the model work? It is a prediction model. By providing 4 inputs as inputs (sepal length, sepal width, petal length, petal width) the model returns the most probable species of the flower.

How to build the model

In the python program I specify the number of data characteristics.

In this case there are four (sepal length, sepal width, petal length, petal width).

feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]

Then I configure the characteristics of the deep neural network (DNN).

I choose to build a neural network with 3 sets of hidden internal nodes (hidden_units).

The first series has 10 nodes, the second series has 20 nodes, the third series has 10 nodes.

classifier = tf.contrib.learn.DNNClassifier(
feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="/tmp/iris_model")

In the code I also indicate the number of classes ( n_classes ) in which to classify the data.

In this case they are 3 ( iris setosa, iris virginica, iris versicolor ).

It is also indicated the folder of the computer where to save the model (eg / tmp / iris_model).

Why save the model? It is very useful to save the model because the calculation can last a long time. If I want to reuse it, it's better to save it somewhere, so I do not have to recalculate every time.

Now I define the data area to build the model from the training_set.

def get_train_inputs():
x = tf.constant(training_set.data)
y = tf.constant(training_set.target)
return x, y

Finally I start the elaboration of TensorFlow to build the model.

classifier.fit(input_fn=get_train_inputs, steps=2000)

In this example, the process takes only a few minutes.

In other cases it can take many hours.

What does the processing time depend on? Processing time depends on the amount of neural network node levels, the number of features and the number of iterations to calculate a model with at least acceptable accuracy.

How to calculate the predictive accuracy of the model

Once found the model, I insert on TensorFlow another table with other examples and same characteristics.

This second table is called a dataset test ( or test set ).

la tabella test set

Note. The test set consists of 30 examples. They are different from the training set. The CSV files must be downloaded ( iris_test.csv ) and saved in a folder on your computer or on a pen drive.

On the python program I indicate the test set table.

IRIS_TEST = "../_data/iris_test.csv"

Then I load the data into memory in the test_set variable

test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TEST,
target_dtype=np.int,
features_dtype=np.float32)

In this case TensorFlow only sees the four characteristics (x) of the test set. He does not see the species (y).

TF uses the model to find the name of the flower (y).

In the python program I add the data area of the test set.

def get_test_inputs():
x = tf.constant(test_set.data)
y = tf.constant(test_set.target)
return x, y

Then I calculate the accuracy of the model

accuracy_score = classifier.evaluate(input_fn=get_test_inputs, steps=1)["accuracy"]

If the model has been built well, it should still find the name of the flowers (y) with acceptable accuracy.

print(accuracy_score)

In this case the model returns an accuracy of 96.7%.

How to use the prediction model

At this point I do a practical test. I create an instance with two sets of values of my choice.

On python I define the data area of the instance.

def new_samples():
return np.array(
[[6.4, 3.2, 4.5, 1.5],
[5.8, 3.1, 5.0, 1.7]], dtype=np.float32)

Then I use the model to find the species of the flower.

The model reads the measurements of the examples and classifies them in one of the three classes ( setosa, virginica o versicolor ).

predictions = list(classifier.predict_classes(input_fn=new_samples))

Next, I print the result that the model has extrapolated.

print(predictions)

If all goes well, the model should return to output [1,1]

The model has classified the instance in category 1 ( virginica ).

Sometimes it could also return [1,2] which means category 1 ( virginica ) for the first example and category 2 ( versicolor ) for the second example of the instance.

And so on.

 


 

Segnalami un errore, un refuso o un suggerimento per migliorare gli appunti

FacebookTwitterLinkedinLinkedin
knowledge base

Artificial Intelligence