[MUSIC PLAYING]
YUFENG GUO: Machine learning is awesome,
except when it forces you to do advanced math.
The tools for machine learning have
gotten dramatically better, and training your own model
has never been easier.
We'll utilize our understanding of our dataset,
rather than an understanding of the raw mathematics,
to build a model that gets us insights.
Welcome to Cloud AI Adventures.
My name is Yufeng Guo.
On this show, we'll explore the art, science,
and tools of machine learning.
While machine learning might be a great conversation
topic at dinner parties, it's even more useful with code.
In this episode, we're going to train a simple classifier using
only a handful lines of code.
In fact, here's all the code that we'll look at today.
To train our classifier, we'll use
TensorFlow, Google's open source machine learning library.
TensorFlow has a pretty large API surface,
but the part we care about today is just the high level
APIs called estimators.
Estimators package up the training loop for us
so that we can train a model by configuring it, rather
than coding it by hand.
This takes away much of the boilerplate,
allowing us to think at a higher level of abstraction.
And this means we'll get to play with the interesting parts
of machine learning, and not get too bogged down in the details.
Since we've only covered linear models so far in the series,
we will stick with that here, and revisit this example
in the future to extend its capabilities.
So with that, let's dive right in and
go to our Jupiter notebook.
OK, so here we are in our Jupiter notebook.
And the first thing that we'll do
is import TensorFlow as tf and numpy as mp.
I like to print out the version number of TensorFlow
below to confirm which version I'm using.
This week, we'll be building a model
to distinguish between three different types of very
similar flowers.
I realize that this may be less interesting than the beer
and wine from the previous episode,
but these flowers are a bit more difficult to distinguish,
making this a more interesting challenge.
In particular, we'll be classifying different species
of iris flowers.
Now, I'm not sure I could pick out an iris flower from a field
of roses, but our model is aiming to distinguish iris
setosa, iris versicolour, and iris virginica.
We have a dataset of measurements
of the height and width of these flowers as petals and sepals.
These four columns will serve as our features.
So let's load our dataset in using TensorFlow's Load
CSV with Header function.
The data or features are presented as floating point
numbers, and the label for each row of data or target
is recorded as an integer--
0, 1, or 2-- corresponding to our three species of flowers.
Now I've printed out the results for our loading,
and we can see that we are now able to access the trained
data and the associated labels or targets
using named attributes.
Next, we'll build the model.
To do this, we'll first set up the feature columns.
Feature columns define the types of data coming into the model.
We are using a four-dimensional feature column
to represent our features, and calling them flower features.
Building our model using estimators is super simple.
Using tf.estimators.LinearClassifier,
we can instantiate the model by passing in the future columns
we just created.
The number of different outputs that the model predicts--
in this case, 3--
and a directory to store the model's training
progress and the output files.
This allows TensorFlow to pick up training
later on from where it left off, if needed.
This classifier object will keep track of state for us.
And we are now almost ready to move on to the training.
There is just one final piece to connect
our model to the training data, and that is the input function.
The job of the input function is to create
the TensorFlow operations that generate data for the model.
So we go from raw data to the input function,
which passes that data that is then mapped by the feature
columns to go into the model.
Notice that we use the same name for the features as we
did in defining the feature column.
This is how the data is associated.
Now it's time to run our training.
To train our model, we'll just run classifier.train
with the input function passed in as an argument.
This is how we connect our dataset to the model.
The train function handles the training loop and iterates
over the dataset, improving its performance with each step.
And just like that, we've completed 1,000 training steps.
Our dataset's not huge, so this completed rather quickly.
Now it's time to evaluate our results.
We can do this using the same classifier object from before,
as it holds the trained state of the model.
To determine how good our model is,
we run classifier.evaluate and pass in our test dataset.
Then we can extract the accuracy from the metrics
that are returned.
96.6%.
OK.
Let's pause here and review what we've achieved so far.
The estimator's API has given us a nice workflow
of getting our raw data and passing it through an input
function, setting up our feature columns and model structure,
running our training, and finally,
running our evaluation.
This easy-to-understand framework
allows us to think about our data and its properties,
rather than the underlying math, which is a great place to be.
Today, we looked at a very simple version
of TensorFlow's high level API using a canned estimator.
In future episodes, we'll look at how
to augment this model with more details, use more complex data,
and even more advanced features.
Thanks for watching that episode of Cloud AI Adventures.
You can catch the whole series here.
And if you have any thoughts and comments,
feel free to leave them down below.