Machine Learning in Swift: Core ML, Create ML, TensorFlow Swift, and Turi Create

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Machine Learning in Swift: Libraries for iOS/macOS

Swift is a powerful and modern language from Apple that is increasingly used not only for classic application development but also for machine learning tasks. Thanks to tight integration with the Apple ecosystem, Swift allows you to run ML models directly on the device (iOS, macOS, watchOS, tvOS) without sending data to a server. In this article, we will explore four key tools: Core ML, Create ML, Swift for TensorFlow, and Turi Create. You will learn how they work, their strengths, and how to get started with them.



1. Core ML — The Foundation for Model Integration

Core ML is a framework from Apple that allows you to embed trained machine learning models into Swift applications. It supports models from TensorFlow, PyTorch, Keras, scikit-learn, and other popular libraries (via conversion to the .mlmodel format).

The main advantage of Core ML is performance: models run on the device, using the CPU, GPU, or Neural Engine (on devices with an A12+ chip). This ensures low latency and data privacy.

Example of using Core ML in Swift:

import CoreMLimport Vision

// Load the model (e.g., MobileNetV2)guard let model = try? VNCoreMLModel(for: MobileNetV2().model) else { fatalError("Failed to load model")}

// Create a requestlet request = VNCoreMLRequest(model: model) { request, error in guard let results = request.results as? [VNClassificationObservation], let topResult = results.first else { return } print("Recognized: \\(topResult.identifier) with confidence \\(topResult.confidence)")}

// Perform the request on an imagelet image = UIImage(named: "cat.jpg")!let handler = VNImageRequestHandler(cgImage: image.cgImage!, options: [:])try? handler.perform([request])


2. Create ML — Training Models Without Code (Almost)

Create ML is a tool from Apple for training machine learning models with minimal coding. It runs on macOS (via Swift Playgrounds or Xcode) and supports tasks such as image classification, text classification, tabular data, recommendation systems, and more.

You can train a model simply by providing a folder with labeled data. After training, the model is automatically converted to the Core ML format and is ready to use.

Example of training an image classifier in Swift:

import CreateMLimport Foundation

// Path to the training data folder (structure: /train/class1/, /train/class2/)let trainingData = try MLImageClassifier.DataSource.labeledDirectories(at: URL(fileURLWithPath: "/path/to/train"))

// Create and train the modellet classifier = try MLImageClassifier(trainingData: trainingData)

// Evaluate accuracy on test datalet evaluationMetrics = classifier.evaluation(on: trainingData)print("Accuracy: \\(evaluationMetrics.classificationError)")

// Save the model in .mlmodel formattry classifier.write(to: URL(fileURLWithPath: "/path/to/MyImageClassifier.mlmodel"))


3. Swift for TensorFlow (S4TF) — Scientific Experiments

Swift for TensorFlow is an experimental project by Google that brings TensorFlow capabilities to the Swift world. It allows you to write differentiable computations and train neural networks in Swift, leveraging the full power of automatic differentiation.

Although the project is officially frozen, its ideas live on in the community, and the code can be used for research and prototyping. S4TF is especially useful if you want to create custom layers, loss functions, or experiment with architectures.

Example of a simple neural network in Swift for TensorFlow:

import TensorFlow

// Define the model: a fully connected network with one

Recommendations