O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Machine Learning with Swift

Book Description

Leverage the power of machine learning and Swift programming to build intelligent iOS applications with ease

About This Book

  • Implement effective machine learning solutions for your iOS applications
  • Use Swift and Core ML to build and deploy popular machine learning models
  • Develop neural networks for natural language processing and computer vision

Who This Book Is For

iOS developers who wish to create smarter iOS applications using the power of machine learning will find this book to be useful. This book will also benefit data science professionals who are interested in performing machine learning on mobile devices. Familiarity with Swift programming is all you need to get started with this book.

What You Will Learn

  • Learn rapid model prototyping with Python and Swift
  • Deploy pre-trained models to iOS using Core ML
  • Find hidden patterns in the data using unsupervised learning
  • Get a deeper understanding of the clustering techniques
  • Learn modern compact architectures of neural networks for iOS devices
  • Train neural networks for image processing and natural language processing

In Detail

Machine learning as a field promises to bring increased intelligence to the software by helping us learn and analyse information efficiently and discover certain patterns that humans cannot. This book will be your guide as you embark on an exciting journey in machine learning using the popular Swift language.

We'll start with machine learning basics in the first part of the book to develop a lasting intuition about fundamental machine learning concepts. We explore various supervised and unsupervised statistical learning techniques and how to implement them in Swift, while the third section walks you through deep learning techniques with the help of typical real-world cases. In the last section, we will dive into some hard core topics such as model compression, GPU acceleration and provide some recommendations to avoid common mistakes during machine learning application development.

By the end of the book, you'll be able to develop intelligent applications written in Swift that can learn for themselves.

Style and approach

A comprehensive guide that teaches how to implement machine learning apps for iOS from scratc

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Machine Learning with Swift
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Getting Started with Machine Learning
    1. What is AI?
    2. The motivation behind ML
    3. What is ML ?
    4. Applications of ML
      1. Digital signal processing (DSP)
      2. Computer vision
      3. Natural language processing (NLP)
      4. Other applications of ML
    5. Using ML to build smarter iOS applications
    6. Getting to know your data
      1. Features
        1. Types of features
        2. Choosing a good set of features
      2. Getting the dataset
      3. Data preprocessing
    7. Choosing a model
      1. Types of ML algorithms
      2. Supervised learning
      3. Unsupervised learning
      4. Reinforcement learning
      5. Mathematical optimization – how learning works
      6. Mobile versus server-side ML
      7. Understanding mobile platform limitations
    8. Summary
    9. Bibliography
  7. Classification – Decision Tree Learning
    1. Machine learning toolbox
    2. Prototyping the first machine learning app
      1. Tools
      2. Setting up a machine learning environment
    3. IPython notebook crash course
    4. Time to practice
    5. Machine learning for extra-terrestrial life explorers
    6. Loading the dataset
    7. Exploratory data analysis
    8. Data preprocessing
      1. Converting categorical variables
      2. Separating features from labels
      3. One-hot encoding
      4. Splitting the data
    9. Decision trees everywhere
    10. Training the decision tree classifier
      1. Tree visualization
      2. Making predictions
      3. Evaluating accuracy
      4. Tuning hyperparameters
      5. Understanding model capacity trade-offs
    11. How decision tree learning works
      1. Building a tree automatically from data
      2. Combinatorial entropy
      3. Evaluating performance of the model with data
        1. Precision, recall, and F1-score
        2. K-fold cross-validation
        3. Confusion matrix
    12. Implementing first machine learning app in Swift
    13. Introducing Core ML
      1. Core ML features
      2. Exporting the model for iOS
      3. Ensemble learning random forest
      4. Training the random forest
      5. Random forest accuracy evaluation
      6. Importing the Core ML model into an iOS project
      7. Evaluating performance of the model on iOS
        1. Calculating the confusion matrix
      8. Decision tree learning pros and cons
    14. Summary
  8. K-Nearest Neighbors Classifier
    1. Calculating the distance
      1. DTW
      2. Implementing DTW in Swift
    2. Using instance-based models for classification and clustering
    3. People motion recognition using inertial sensors
    4. Understanding the KNN algorithm
      1. Implementing KNN in Swift
    5. Recognizing human motion using KNN
      1. Cold start problem
      2. Balanced dataset
      3. Choosing a good k
    6. Reasoning in high-dimensional spaces
    7. KNN pros
    8. KNN cons
    9. Improving our solution
      1. Probabilistic interpretation
      2. More data sources
      3. Smarter time series chunking
      4. Hardware acceleration
      5. Trees to speed up the inference
      6. Utilizing state transitions
    10. Summary
    11. Bibliography
  9. K-Means Clustering
    1. Unsupervised learning
    2. K-means clustering
    3. Implementing k-means in Swift
      1. Update step
      2. Assignment step
    4. Clustering objects on a map
    5. Choosing the number of clusters
    6. K-means clustering – problems
    7. K-means++
    8. Image segmentation using k-means
    9. Summary
  10. Association Rule Learning
    1. Seeing association rules
    2. Defining data structures
    3. Using association measures to assess rules
      1. Supporting association measures
      2. Confidence association measures
      3. Lift association measures
      4. Conviction association measures
    4. Decomposing the problem
    5. Generating all possible rules
    6. Finding frequent item sets
    7. The Apriori algorithm
    8. Implementing Apriori in Swift
    9. Running Apriori
    10. Running Apriori on real-world data
    11. The pros and cons of Apriori
    12. Building an adaptable user experience
    13. Summary
    14. Bibliography
  11. Linear Regression and Gradient Descent
    1. Understanding the regression task
    2. Introducing simple linear regression
      1. Fitting a regression line using the least squares method
        1. Where to use GD and normal equation
        2. Using gradient descent for function minimization
      2. Forecasting the future with simple linear regression
    3. Feature scaling
    4. Feature standardization
      1. Multiple linear regression
    5. Implementing multiple linear regression in Swift
      1. Gradient descent for multiple linear regression
        1. Training multiple regression
        2. Linear algebra operations
      2. Feature-wise standardization
        1. Normal equation for multiple linear regression
      3. Understanding and overcoming the limitations of linear regression
    6. Fixing linear regression problems with regularization
      1. Ridge regression and Tikhonov regularization
        1. LASSO regression
      2. ElasticNet regression
    7. Summary
    8. Bibliography
  12. Linear Classifier and Logistic Regression
    1. Revisiting the classification task
      1. Linear classifier
      2. Logistic regression
    2. Implementing logistic regression in Swift
      1. The prediction part of logistic regression
      2. Training the logistic regression
      3. Cost function
    3. Predicting user intents
      1. Handling dates
    4. Choosing the regression model for your problem
    5. Bias-variance trade-off
    6. Summary
  13. Neural Networks
    1. What are artificial NNs anyway?
    2. Building the neuron
      1. Non-linearity function
        1. Step-like activation functions
        2. Rectifier-like activation functions
    3. Building the network
    4. Building a neural layer in Swift
    5. Using neurons to build logical functions
    6. Implementing layers in Swift
    7. Training the network
      1. Vanishing gradient problem
      2. Seeing biological analogies
    8. Basic neural network subroutines (BNNS)
      1. BNNS example
    9. Summary
  14. Convolutional Neural Networks
    1. Understanding users emotions
    2. Introducing computer vision problems
    3. Introducing convolutional neural networks
    4. Pooling operation
    5. Convolution operation
      1. Convolutions in CNNs
    6. Building the network
      1. Input layer
      2. Convolutional layer
      3. Fully-connected layers
      4. Nonlinearity layers
      5. Pooling layer
      6. Regularization layers
        1. Dropout
        2. Batch normalization
    7. Loss functions
    8. Training the network
    9. Training the CNN for facial expression recognition
    10. Environment setup
    11. Deep learning frameworks
      1. Keras
    12. Loading the data
    13. Splitting the data
    14. Data augmentation
    15. Creating the network
    16. Plotting the network structure
    17. Training the network
    18. Plotting loss
    19. Making predictions
    20. Saving the model in HDF5 format
    21. Converting to Core ML format
    22. Visualizing convolution filters
    23. Deploying CNN to iOS
    24. Summary
    25. Bibliography
  15. Natural Language Processing
    1. NLP in the mobile development world
    2. Word Association game
    3. Python NLP libraries
    4. Textual corpuses
    5. Common NLP approaches and subtasks
      1. Tokenization
      2. Stemming
      3. Lemmatization
      4. Part-of-speech (POS) tagging
      5. Named entity recognition (NER)
      6. Removing stop words and punctuation
    6. Distributional semantics hypothesis
    7. Word vector representations
    8. Autoencoder neural networks
    9. Word2Vec
    10. Word2Vec in Gensim
    11. Vector space properties
    12. iOS application
      1. Chatbot anatomy
      2. Voice input
      3. NSLinguisticTagger and friends
      4. Word2Vec on iOS
      5. Text-to-speech output
      6. UIReferenceLibraryViewController
      7. Putting it all together
    13. Word2Vec friends and relatives
    14. Where to go from here?
    15. Summary
  16. Machine Learning Libraries
    1. Machine learning and AI APIs
    2. Libraries
    3. General-purpose machine learning libraries
      1. AIToolbox
      2. BrainCore
      3. Caffe
      4. Caffe2
      5. dlib
      6. FANN
      7. LearnKit
      8. MLKit
      9. Multilinear-math
      10. MXNet
      11. Shark
      12. TensorFlow
      13. tiny-dnn
      14. Torch
      15. YCML
    4. Inference-only libraries
      1. Keras
      2. LibSVM
      3. Scikit-learn
      4. XGBoost
    5. NLP libraries
      1. Word2Vec
      2. Twitter text
    6. Speech recognition
      1. TLSphinx
      2. OpenEars
    7. Computer vision
      1. OpenCV
      2. ccv
      3. OpenFace
      4. Tesseract
    8. Low-level subroutine libraries
      1. Eigen
      2. fmincg-c
      3. IntuneFeatures
      4. SigmaSwiftStatistics
      5. STEM
      6. Swix
      7. LibXtract
      8. libLBFGS
      9. NNPACK
      10. Upsurge
      11. YCMatrix
    9. Choosing a deep learning framework
    10. Summary
  17. Optimizing Neural Networks for Mobile Devices
    1. Delivering perfect user experience
    2. Calculating the size of a convolutional neural network
    3. Lossless compression
    4. Compact CNN architectures
      1. SqueezeNet
      2. MobileNets
      3. ShuffleNet
      4. CondenseNet
    5. Preventing a neural network from growing big
    6. Lossy compression
      1. Optimizing for inference
        1. Network pruning
        2. Weights quantization
        3. Reducing precision
        4. Other approaches
          1. Facebook's approach in Caffe2
      2. Knowledge distillation
      3. Tools
    7. An example of the network compression
    8. Summary
    9. Bibliography
  18. Best Practices
    1. Mobile machine learning project life cycle
      1. Preparatory stage
        1. Formulate the problem
        2. Define the constraints
        3. Research the existing approaches
        4. Research the data
        5. Make design choices
      2. Prototype creation
        1. Data preprocessing
        2. Model training, evaluation, and selection
        3. Field testing
      3. Porting or deployment for a mobile platform
      4. Production
    2. Best practices
      1. Benchmarking
      2. Privacy and differential privacy
      3. Debugging and visualization
      4. Documentation
    3. Machine learning gremlins
      1. Data kobolds
        1. Tough data
        2. Biased data
        3. Batch effects
      2. Goblins of training
      3. Product design ogres
        1. Magical thinking
        2. Cargo cult
        3. Feedback loops
        4. Uncanny valley effect
    4. Recommended learning resources
      1. Mathematical background
        1. Machine learning
        2. Computer vision
        3. NLP
    5. Summary