Machine learning on mobile devices is often associated with inference: download a model, run predictions, and return results.
But what if the model could continue learning directly on the user's device?
In this article, I'll walk through a practical training strategy for on-device personalization in iOS using a lightweight Multilayer Perceptron (MLP). The goal is to create applications that adapt to individual users while keeping their data private and avoiding cloud infrastructure.
Why Train On Device?
Consider a habit-tracking application.
Two users may exhibit completely different behaviors:
- User A completes habits every morning.
- User B completes habits late at night.
- User A responds well to reminders.
- User B ignores reminders entirely.
A single global model cannot capture every user's unique patterns.
Instead, we can train a small neural network locally using each user's own interaction history.
Benefits include:
- Privacy-first personalization
- No server-side training costs
- Offline functionality
- Faster adaptation to user behavior
- Reduced regulatory concerns around user data
The Training Pipeline
A typical on-device learning pipeline looks like this:
User Events
↓
Feature Extraction
↓
Training Dataset
↓
MLP Training
↓
Updated Model
↓
Personalized Predictions
Every user effectively owns a customized model.
Step 1: Collect Behavioral Signals
Start by recording meaningful events.
struct UserEvent {
let timestamp: Date
let type: EventType
}
enum EventType {
case appOpened
case reminderTapped
case habitCompleted
case habitSkipped
}
These events can be stored using:
- SwiftData
- Core Data
- SQLite
The goal is to build a historical timeline of user behavior.
Step 2: Build Feature Vectors
Raw events aren't useful for neural networks.
We need numerical features.
Example:
struct HabitFeatures {
let currentStreak: Double
let completionRate30Days: Double
let appLaunchesToday: Double
let remindersOpenedToday: Double
let hourOfDay: Double
}
After normalization:
[
0.45,
0.80,
0.30,
0.10,
0.75
]
These values become the neural network's input.
Step 3: Generate Training Samples
Every day becomes a training example.
For example:
Features on Monday
↓
Completed Habit on Tuesday?
Represented as:
struct TrainingSample {
let inputs: [Float]
let target: Float
}
Where:
- 1 = completed habit
- 0 = missed habit
Over time the device accumulates hundreds of examples automatically.
Step 4: Keep the Model Small
On-device learning is not about training giant models.
A compact MLP is often sufficient:
10 Inputs
↓
16 Neurons
↓
8 Neurons
↓
1 Output
This architecture typically contains only a few hundred parameters.
Advantages:
- Fast training
- Tiny memory footprint
- Low battery usage
- Instant predictions
Step 5: Schedule Training Intelligently
One of the biggest mistakes in mobile ML is training too frequently.
Training should happen only under favorable conditions.
Recommended conditions:
- Device charging
- Connected to Wi-Fi
- Screen locked
- User inactive
Use BackgroundTasks:
BGProcessingTaskRequest(
identifier: "com.example.training"
)
Training should typically run:
- Once per day
- Once per week
- After collecting enough new samples
Example configuration:
epochs = 20
batchSize = 32
learningRate = 0.001
This usually completes in under a second for small datasets.
Step 6: Save Model Checkpoints
After training, persist the updated weights.
struct ModelCheckpoint: Codable {
let weights: [[Float]]
let biases: [Float]
let version: Int
}
Store checkpoints inside:
Application Support/
On launch:
model.loadCheckpoint()
The model immediately resumes from its previous state.
Step 7: Run Fast Local Inference
Predictions should happen in real time.
let probability =
model.predict(features)
Example output:
0.87
Meaning:
The user has an 87% probability of completing today's habit.
Inference latency for small MLPs is typically less than one millisecond on modern iPhones.
Step 8: Convert Predictions into Product Decisions
Predictions only become valuable when they drive experiences.
Examples:
if probability < 0.4 {
scheduleReminder()
}
Or:
if probability > 0.8 {
suppressReminder()
}
The application becomes adaptive rather than rule-based.
Continuous Learning
The most powerful aspect of on-device learning is the feedback loop.
Predict
↓
Observe Outcome
↓
Store Example
↓
Retrain
↓
Improve Predictions
Every interaction helps improve the model.
No data ever leaves the device.
Privacy by Design
Traditional personalization systems often require:
Device
↓
Cloud
↓
Training
↓
Predictions
An on-device system looks like:
Device
↓
Training
↓
Predictions
User behavior never leaves the phone.
This dramatically improves privacy while reducing infrastructure complexity.
Final Thoughts
Not every application needs a transformer, a recommendation engine, or a cloud-based ML platform.
Many personalization problems can be solved with a small neural network trained directly on the user's device.
For habit tracking, content recommendations, notification timing, fitness coaching, and user engagement prediction, a lightweight MLP combined with background training can deliver highly personalized experiences while remaining fast, private, and inexpensive to operate.
The future of mobile AI isn't only about larger models. Sometimes it's about making smaller models personal.























