How to Train a Core ML Model for an iOS App

Core ML makes it easy for iOS developers to add deep machine learning to their apps. In this post, I'll show you how you can train a Core ML model to derive intelligent insights.

Machine learning has undoubtedly been one of the hottest topics over the past year, with companies of all kinds trying to make their products more intelligent to improve user experiences and differentiate their offerings. Google invested between $20B and $30B in artificial intelligence just last year alone, according to McKinsey’s State Of Machine Learning And AI, 2017

AI is turning into a race for patents and intellectual property (IP) among the world’s leading tech companies...The report cites many examples of internal development including Amazon’s investments in robotics and speech recognition, and Salesforce on virtual agents and machine learning. BMW, Tesla, and Toyota lead auto manufacturers in their investments in robotics and machine learning for use in driverless cars. Toyota is planning to invest $1B in establishing a new research institute devoted to AI for robotics and driverless vehicles. (source: Forbes)

Apple is no exception to this trend, having utilized Machine Learning in their own apps. For example, the Photos app for iOS can recognize faces, objects and landmarks, and Siri infers intent and meaning from speech. Messages for iOS intelligently suggests and predicts words based on previous user behaviors. 

In this tutorial, you will learn about how to apply machine learning algorithms to a set of training data, to create a trained model which will subsequently make predictions based on new input. All thanks to Apple’s new Core ML framework. 

Objectives of This Tutorial

This tutorial will introduce you to a subset of Machine Learning.  You'll train and integrate a machine learning model in a simple iOS app, using a popular deep learning algorithm framework. In this tutorial, you will:

  • learn some of the basic Machine Learning concepts 
  • train your model using sample data
  • integrate the trained model in an iOS app

After going through the theory of NLP, we'll put our knowledge to practice by working through a simple twitter client, analyzing tweet messages. Go ahead and clone the tutorial’s GitHub repo and take a look at the final version of the app we will create from scratch. 

Assumed Knowledge

This tutorial assumes you are a seasoned iOS developer, but although you will be working with machine learning, you don’t need to have any background on the subject. You'll be using a bit of Python to create your trained model, but you can follow through the tutorial example without prior knowledge of Python. 

Machine Learning 101

The goal of machine learning is for a computer to do tasks without being explicitly programmed to do so—the ability to think or interpret autonomously. A high-profile contemporary use-case is autonomous driving: giving cars the ability to visually interpret their environment and drive unaided. 

Machine Learning is today leveraged by large companies to make better business decisions based on historical data, by using deep learning algorithms to identify patterns and correlations, which allow them to make better predictions of the future. For instance, you can resolve problems such as “How likely it is for a specific customer to purchase a specific product or service?” with greater confidence based on prior behavior. 

Machine learning is best applied to problems where you have a history of answers, as you will discover later in this tutorial when we go through our sample problem. An example of machine learning in action would be your email spam filter, which uses supervised learning (as you flag items as spam or not) to better filter spam over time. The machine learning model encodes all of this knowledge about past results and makes it available to the algorithm for efficient use at run-time.

It may all sound a bit overwhelming at first, but it isn’t complicated, and we will walk you through how to create a trained model shortly. Once you have devised a trained model via an algorithm, you will then convert it to a model that can be consumed by iOS, thanks to Core ML.


 is new to Apple’s family of SDKs, introduced as part of iOS 11 to allow developers to implement a vast variety of machine learning modes and deep learning layer types. 

Natural Language Processing (NLP) logically sits within the Core ML framework alongside two other powerful libraries, Vision and GameplayKit. Vision provides developers with the ability to implement computer vision machine learning to accomplish things such as detecting faces, landmarks, or other objects, while GameplayKit provides game developers with tools for authoring games and specific gameplay features. 

The benefits of CoreML compared to other solutions is that Apple has optimized machine learning to run on-device, which means reduced memory power consumption and reduced latency. This also containerizes user information within the device, improving privacy.

Read the complete tutorial, exclusively on tuts+.
Doron KatzCoreML, iOS, iOS 11