Google’s TensorFlow 101
Google’s TensorFlow is making waves in a wide range of applications – but what actually is it and how do you use it? Darren Yates writes this simple guide.
It’s a pretty exciting time to get involved in machine-learning. While there’s certainly way too much hype surrounding it for my liking, there are many practical applications where it’s making a real difference. Health and agriculture, bushfire management and prediction, even protecting wildlife and endangered species are all seeing machine-learning applied in real value ways. It’s even finished Beethoven’s Tenth Symphony.
This boom in application has come from a rapid increase in open-source machine-learning tools and cloud computing. All the big tech brands now have ML tools, many are even opensource. One of the major players is Google’s TensorFlow. This month, we’ll take a brief tour of what TensorFlow is and does, and how to use it on your PC.
WHAT’S A TENSOR?
ML is predominantly about mathematics, but don’t’ let that freak you out - we’ll keep things broad enough not to scare you off, but deep enough to give some understanding. Take a spreadsheet in Excel – it has rows and columns of individual cells. In mathematics, a single cell’s value is a ‘scalar’. Programmers represent a row of a spreadsheet as an ‘array’ of numbers and mathematicians call this a ‘vector’. So, a spreadsheet is a collection of vectors. Again, mathematicians call this two-dimensional (2D) collection of vectors a ‘matrix’.
A tensor can act like these ‘data structures’ – for example, a scalar, a vector and a matrix are zero-, one- and two-dimensional tensors, respectively, but you can extend this to higher dimensions and this is where tensors really start to fly. In tensor-speak, the dimension of the tensor is referred to as its ‘rank’, so a matrix is a rank-two tensor.
However, a tensor is not only a generalised data structure, it also supports different mathematical transformations of its data and this makes tensors ideal for a particular style of machine-learning known as ‘deep learning’.
NEURAL NETWORKS
When you hear mention of ‘deep learning’, what’s actually being described is usually some form of
The deep-learning model predicts the clothing type with 90% accuracy.
‘neural network’, which are algorithms that loosely follow how the brain works, with neurons connected together that allow data to flow between them.
We don’t have space to cover them all here but there are numerous categories of neural networks, including recurrent neural networks (RNNs) and convolutional neural networks (CNNs). RNNs are often used in natural language processing (think chatbots, for example), while CNNs often appear in image processing applications, where you’re trying to detect objects.
In the right application and with lots of data, neural networks are great – but in spite of overwhelming hype, they’re not perfect. In most cases, it’s difficult to understand why NNs react to specific data because essentially, they act as a ‘black box’ where you can’t see what’s going on inside. They typically need more data and take longer to build than traditional machine-learning types such as decision trees and forests. Don’t get me wrong – I’m not against NNs, but I am equally not a fan of treating neural networks like a shiny new hammer and every machine-learning problem as a nail to be hit with it.
TENSORFLOW
With that as a background, Google’s TensorFlow is a machine-learning library that’s designed to make the many tensor calculations faster and easier to
work with. What’s more, its opensource and you can use it with the Python programming language.
Google has a decent tutorial and beginners guide on its tensorflow.org/ overview website and it’s well worth a look. You can even run the examples online at Google’s ‘Colaboratory’ using just your browser. For simplicity’s sake, that’s what we’ll do.
FASHION EXAMPLE
First thing, head over to http:// tensorflow.org/tutorials and click on the ‘Keras basics’ tile in the ‘For beginners’ area. Keras is a neural network library that runs in Python and makes use of TensorFlow to handle data processing – but we’ll be using it in a browser.
This example is a classification task that learns how to identify different images of clothing, from shoes to tops and bags. Click on the ‘Run in Google Colab’ button and this will bring up the tutorial in an online notebook you can run through your browser. At the top-left of the new window, you’ll see a basic menu – File, Edit etc. On the right, you’ll either see a ‘CONNECT’ button or a green tick next to RAM and Disk gauges. If you don’t have the latter, click the Connect button. Once it’s sorted, you can now run the example’s source code by selecting from the top-left menu, ‘Runtime’ and ‘Run all’.
The cool thing about this example is you can read through the description and see the code execute as you go. The first step it executes is installing the Fashion MNIST dataset, which contains 70,000 images, each 28x28pixels, of various items of clothing. These images are grouped or ‘classed’ by type, with each belong to one of 10 classifications – T-shirt, trousers, pullover, dress, coat, sandal, shirt, sneaker, bag and ankle-boot.
After that, it’ll preprocess the images – that means get them into a form that the Keras library can understand.
The next step is building the model – this is where the deep learning takes place, finding the patterns within images that categorises a coat from a bag, or sandal from trousers, for example. Once the model is built, it’s then tested for accuracy – if you scroll down you should see that the test accuracy is around 88.3%.
Finally, the model is used to make predictions on unclassified images. You’ll see the images are shown in pairs – the original clothing item and a chart which shows the predicted clothing type it is. Note that the predictions are not always definitive, or predicted with no uncertainty. For example, the model can struggle to choose between a shirt and a top (which is understandable – I do too from tiny 28x28-pixel images).
If you got this far, congrats – you’ve built your first deep learning model.
LEARNING THE FUTURE
Even better, you can change the code in the notebook, re-run it and see what happens – there is no better way to learning than rolling up your sleeves, trying things, making mistakes and learning from those mistakes. Just remember Deep Learning is only one way to perform this classification task and there are many others well worth trying.
The sheer breadth of application areas machine-learning has is just mind-blowing. Deep learning won’t be ideal for every application, but that’s why Australia needs more people who understand machine-learning. The opportunities are immense.
“When you hear mention of ‘deep learning,’ what’s actually being described is usually some form of ‘neural network,’ which are algorithms that loosely follow how the brain works. ”