Table Of ContentMachine Learning by Tutorials Machine Learning by Tutorials
Machine Learning by Tutorials
By Matthijs Hollemans, Chris LaPollo and Audrey Tam
Copyright ©2018 Razeware LLC.
Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without prior
written permission of the copyright owner.
Notice of Liability
This book and all corresponding materials (such as source code) are provided on an “as
is” basis, without warranty of any kind, express of implied, including but not limited to
the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in the
software.
Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.
raywenderlich.com 2
Machine Learning by Tutorials Machine Learning by Tutorials
Dedications
"To Floortje, my familiar. Thanks for all the cuddles!"
— Matthijs Hollemans
"To Bram, Darwin and Archana: All my love — go ahead and divvy
that up amongst yourselves. (◠‿◠) To our future machine overlords:
I was on your side. I mean, c’mon, beep boop beep, amirite? (O∼O)"
— Chris LaPollo
"To my parents and teachers, who set me on the path that led me to
the here and now."
— Audrey Tam
raywenderlich.com 3
Machine Learning by Tutorials Machine Learning by Tutorials
About the Authors
Matthijs Hollemans is an author on this book. After many years of
handcrafting his logic with if-then-else statements, Matthijs finally
saw the light and switched to machine learning, teaching computers
to come up with those if-then-else statements by themselves. Why
write programs when computers can write their own? Matthijs also
lifts heavy things in the gym and plays heavy things on his guitar.
Matthijs blogs about iOS machine learning at machinethink.net. You
can find him on Twitter as @mhollemans.
Chris LaPollo is an author of this book. He's told software what to do
for over two decades, but lately he tells software to go figure it out
itself. An independent developer and consultant focused on machine
learning, he also writes video games for fun. Nowadays he spends free
time with family and learning to cook. He's kept his basil plants alive
for several months – it's a pretty big deal. Find him on Twitter at
@chrislapollo.
Audrey Tam is an author on this book. As a retired computer science
academic, she's a technology generalist with expertise in translating
new knowledge into learning materials. Audrey has a PhD in applied
math, so is especially good at dis-intimidating machine learning
math. Audrey now teaches short courses in iOS app development to
non-programmers and attends nearly all Melbourne Cocoaheads
monthly meetings. She also enjoys long train journeys, knitting and
trekking in the Aussie wilderness.
raywenderlich.com 4
Machine Learning by Tutorials Machine Learning by Tutorials
About the Editors
Jeff Biggus is a tech editor of this book. Jeff is an independent
researcher, consultant and engineer, currently focused on scientific
and GPU computing. When not programming, he has his nose stuck in
too many books, writing, recording classical and experimental music,
and general nonsense.
Phil J. Laszkowicz is a tech editor of this book. Phil's been delivering
large-scale software solutions for many years, as well as working with
startups as a board member, mentor, and coach. He's worked with
neural networks for over a decade, and enjoys combining deep
learning with intuitive and elegant user experiences across mobile
and web. In his spare time he writes music, drinks coffee at a
professional level, and can be found scaling cliff walls, sea kayaking,
or taking part in competitive archery.
Manda Frederick is an editor of this book. She has been involved in
publishing for over ten years through various creative, educational,
medical and technical print and digital publications, and is thrilled to
bring her experience to the raywenderlich.com family as Managing
Editor. In her free time, you can find her at the climbing gym,
backpacking in the backcountry, hanging with her dog, working on
poems, playing guitar and exploring breweries.
Vijay Sharma is the final pass editor of this book. Vijay is a husband,
a father and a senior mobile engineer. Based out of Canada's capital,
Vijay has worked on dozens of apps for both Android and iOS. When
not in front of his laptop, you can find him in front of a TV, behind a
book, or chasing after his kids. You can reach out to him on Twitter
@vijaysharm or on LinkedIn @vijaysharm
raywenderlich.com 5
Machine Learning by Tutorials Machine Learning by Tutorials
About the Artist
Vicki Wenderlich is the designer and artist of the cover of this book.
She is Ray’s wife and business partner. She is a digital artist who
creates illustrations, game art and a lot of other art or design work for
the tutorials and books on raywenderlich.com. When she’s not
making art, she loves hiking, a good glass of wine and attempting to
create the perfect cheese plate.
raywenderlich.com 6
Machine Learning by Tutorials
Table of Contents: Overview
Early Access Edition.............................................................. 11
What You Need....................................................................... 12
Book License ............................................................................ 13
Book Source Code & Forums............................................. 14
About the Cover ..................................................................... 15
Chapter 1: Machine Learning, iOS & You...................... 16
Chapter 2: Getting Started with Image
Classification............................................................................ 45
Chapter 3: Training the Image Classifier....................... 83
Chapter 4: Getting Started With Python & Turi
Create....................................................................................... 109
Chapter 5: Digging Deeper Into Turi Create ............ 147
Chapter 6: Training with Keras ...................................... 174
Chapter 7: Beyond Image Classification .................... 175
Chapter 8: Sequence Classification ............................. 176
Chapter 9: Sequence Predictions.................................. 238
Chapter 10: NLP Classification...................................... 239
Chapter 11: Text-to-Text Transform ............................ 240
Want to Grow Your Skills? ............................................... 241
raywenderlich.com 7
Machine Learning by Tutorials
Table of Contents: Extended
Early Access Edition.............................................................. 11
What You Need....................................................................... 12
Book License ............................................................................ 13
Book Source Code & Forums............................................. 14
About the Cover ..................................................................... 15
Chapter 1: Machine Learning, iOS & You...................... 16
What is machine learning?............................................................................................. 17
Deep learning...................................................................................................................... 19
ML in a nutshell................................................................................................................... 22
Can mobile devices really do machine learning?................................................... 31
Frameworks, tools and APIs.......................................................................................... 32
ML all the things?............................................................................................................... 39
The ethics of machine learning..................................................................................... 41
Key points............................................................................................................................. 43
Where to go from here?.................................................................................................. 44
Chapter 2: Getting Started with Image
Classification............................................................................ 45
Is that snack healthy?....................................................................................................... 46
Core ML................................................................................................................................. 51
Vision...................................................................................................................................... 54
Creating the VNCoreML request................................................................................ 55
Performing the request................................................................................................... 58
Showing the results........................................................................................................... 61
How does it work?............................................................................................................. 67
Multi-class classification................................................................................................. 73
Key points............................................................................................................................. 78
Bonus: Using Core ML without Vision...................................................................... 78
raywenderlich.com 8
Machine Learning by Tutorials
Challenges............................................................................................................................ 82
Chapter 3: Training the Image Classifier....................... 83
The dataset........................................................................................................................... 83
Create ML............................................................................................................................. 85
How we created the dataset......................................................................................... 87
Transfer learning................................................................................................................ 89
Logistic regression............................................................................................................. 93
Looking for validation...................................................................................................... 95
More metrics and the test set.................................................................................... 102
Exporting to Core ML.................................................................................................... 105
Recap.................................................................................................................................... 107
Key points........................................................................................................................... 108
Challenge............................................................................................................................ 108
Chapter 4: Getting Started With Python & Turi
Create....................................................................................... 109
Starter folder.................................................................................................................... 109
Python................................................................................................................................. 110
Packages and environments....................................................................................... 111
Anaconda............................................................................................................................ 112
Setting up a base ML environment........................................................................... 114
Jupyter notebooks.......................................................................................................... 119
Transfer learning with Turi Create........................................................................... 127
Shutting down Jupyter.................................................................................................. 140
Useful Conda commands............................................................................................. 141
Docker and Colab............................................................................................................ 143
Key points........................................................................................................................... 145
Challenges.......................................................................................................................... 145
Where to go from here?................................................................................................ 146
Chapter 5: Digging Deeper Into Turi Create ............ 147
Getting started................................................................................................................. 147
Transfer learning with SqueezeNet......................................................................... 147
Getting individual predictions................................................................................... 149
raywenderlich.com 9
Machine Learning by Tutorials
Increasing max iterations............................................................................................. 154
Confusing apples with oranges?................................................................................ 156
Wrangling Turi Create code........................................................................................ 160
A peek behind the curtain............................................................................................ 169
Key points........................................................................................................................... 170
Challenges.......................................................................................................................... 171
Chapter 6: Training with Keras ...................................... 174
Chapter 7: Beyond Image Classification .................... 175
Chapter 8: Sequence Classification ............................. 176
Building a dataset............................................................................................................ 178
Analyzing and preparing your data.......................................................................... 188
Creating a model............................................................................................................. 199
Getting to know your model....................................................................................... 211
Classifying human activity in your app................................................................... 218
Key points........................................................................................................................... 236
Challenges.......................................................................................................................... 237
Chapter 9: Sequence Predictions.................................. 238
Chapter 10: NLP Classification...................................... 239
Chapter 11: Text-to-Text Transform ............................ 240
Want to Grow Your Skills? ............................................... 241
raywenderlich.com 10