Time to try My ML “Hello World”
In my journey to better understand AI , I decided to look into how to create a basic neural network by leveraging TensorFlow, an open-source software library for Machine Intelligence. For this I used a well known Data Set (in a spreasheet) where each row of the table represents an iris flower, including its species and dimensions of its botanical parts, sepal and petal, in centimeters.
The goal was to construct a neural network classifier and train it on the Iris data set to predict flower species based on sepal/petal geometry.
What the following is showing is how to :
- Load CSVs containing Iris training/test data into a TensorFlow Dataset
- Construct a neural network classifier
- Train the model using the training data
- Evaluate the accuracy of the model
The iris dataset is a classification task consisting in identifying 3 different types of irises (Setosa, Versicolour, and Virginica) from their petal and sepal length and width
Step by Step Tutorial
Pre-requisite:
TensorFlow installed
-TensorFlow with CPU support only
OR
- TensorFlow with GPU support (if you have GPU)
Python
- I use Python 3.5.0
Numpy a fundamental package needed for scientific computing with Python.
- I use numpy‑1.13.1+mkl‑cp35‑cp35m‑win_amd64.whl
SciPy a software for mathematics, science, and engineering.
- I use scipy‑0.19.1‑cp35‑cp35m‑win_amd64.whl
Installation type : native pip (for non virtualised environment)
STEP 1 : Install Python
Open the link here >>> Python and select the right version for you.
I use Python 3.5.0 for Windows 10
Once Python is successfully installed you can check by opening your consol and type “pip” , the python tool to install programs. The output should ask you for parameter rather than telling you that the command does not exist.
Example (ouput)
Usage:
pip <command> [options]
STEP 2 : Install TensorFlow from Python
In your consol type the command :
>pip3 install — upgrade tensorflow
Getting an error about the pip version? Fine just upgrade pip to version 9.x.x with this command:
>python -m pip install — upgrade pip
STEP 3 : Validate your installation
Invoke python from your shell as follows:
>python
Enter the following short program inside the python interactive shell:
>>> import tensorflow as tf
>>> hello = tf.constant(‘Hello, TensorFlow!’)
>>> sess = tf.Session()
>>> print(sess.run(hello))
If the system outputs the following, then you are ready to begin writing TensorFlow programs:
Hello, TensorFlow!
STEP 4 : Write the code
For this step I literally resused the code from :
https://www.tensorflow.org/get_started/estimator
And amended few lines on the code to make it compatible with python 3.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import urllib.request
import numpy as np
import tensorflow as tf
# Data sets
IRIS_TRAINING = “iris_training.csv”
IRIS_TRAINING_URL = “http://download.tensorflow.org/data/iris_training.csv"
IRIS_TEST = “iris_test.csv”
IRIS_TEST_URL = “http://download.tensorflow.org/data/iris_test.csv"
if not os.path.exists(IRIS_TRAINING):
raw = urllib.request.urlopen(IRIS_TRAINING_URL).read().decode()
with open(IRIS_TRAINING, ‘w’) as f:
f.write(raw)
if not os.path.exists(IRIS_TEST):
raw = urllib.request.urlopen(IRIS_TEST_URL).read().decode()
with open(IRIS_TEST, ‘w’) as f:
f.write(raw)
# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING,
target_dtype=np.int,
features_dtype=np.float32)
test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TEST,
target_dtype=np.int,
features_dtype=np.float32)
# Specify that all features have real-value data
feature_columns = [tf.feature_column.numeric_column(“x”, shape=[4])]
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir=”/tmp/iris_model”)
# Define the training inputs
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={“x”: np.array(training_set.data)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True)
# Train model.
classifier.train(input_fn=train_input_fn, steps=2000)
# Define the test inputs
test_input_fn = tf.estimator.inputs.numpy_input_fn(
x={“x”: np.array(test_set.data)},
y=np.array(test_set.target),
num_epochs=1,
shuffle=False)
# Evaluate accuracy.
accuracy_score = classifier.evaluate(input_fn=test_input_fn)[“accuracy”]
print(“\n Accuracy: {0:f}\n”.format(accuracy_score))
Result is : 0.96
Based on what we thaught the algorithim with the training set the prediction that the test data is containing iris flower comes with an accuracy of 96% which is not too bad.