swiftmachine-learningcoreml

iOS Swift dynamic Machine Learning from user data


is it possible to use apple ML framework to dynamically learn from users behaviour in the app? I've trained a model using Create ML application, can I then update and retrain from the iOS device? This is how I'm currently using said model.

public func calculateMuscleRecoveryTime(_ workout: Workout) {
    do {
        
        let config = MLModelConfiguration()
        let model = try MuscleRecoveryModel(configuration: config)
        
        let allMuscleGroups = workout.exercises
            .compactMap { $0.muscles } // Flatten the muscles array from each exercise
            .reduce(Set<MuscleGroup>()) { $0.union($1) } // Union to remove duplicates

        let uniqueMuscleGroups = Array(allMuscleGroups)
        
        for muscleGroup in uniqueMuscleGroups {
            let trainingIntensity = Int64(workout.intensity.intValue)
            let lastTrainedTimestamp = workout.date
            let timeAgo = timeAgoInSeconds(from: lastTrainedTimestamp)
            let muscleName = muscleGroup.rawValue.lowercased()
            
            let prediction = try model.prediction(muscle: muscleName, intensity: trainingIntensity, lastTrained: timeAgo)
        }
    } catch let error {
        print("Error: ", error)
    }
}

Solution

  • you can collect data within your app, transmit it securely to your server, and periodically retrain your model using this aggregated data. The updated model can then be deployed to users through app updates or by downloading the new model file at runtime. Additionally, Apple's Personalization APIs introduced in iOS 13 allow for limited on-device model personalization, enabling adjustments based on individual user interactions without full model retraining.