How to create ML program from Swift?

I would like to generate and run ML program inside an app.

I got familiar with the coremltools and MIL format, however I can't seem to find any resources on how to generate mlmodel/mlpackage files using Swift on the device.

Is there any Swift equivalent of coremltools? Or is there a way to translate MIL description of a ML program into instance of a MLModel? Or something similar.

Assuming that you're targeting iOS, then you can develop apps that generate and use ML Models by using Xcode 13 (currently beta) and iOS15 (currently beta). Key documentation is here https://developer.apple.com/documentation/createml but be aware that it hasn't yet been updated to reflect the ability to create models directly in an iOS app. The main steps are:

  1. Determine what type of model you need (e.g. Text classifier, Image classifier, tabular data regression), understand your data source and collect an appropriate amount of training data, apply labels (classifications) to the training data as needs be (e.g. review text strings and apply a label to each)
  2. In your app, load the training data using a method from the CreateML framework (e.g. MLDataTable(contentsOf:) - importing from JSON or CSV is easy. How you provide the data to the device/app is another issue, eg via the bundle then to documents directory - or read-only from the bundle.
  3. Create your model from the training dataset, e..g. MLTextClassifier(trainingData: trainingData, textColumn: "MyText", labelColumn: "Category"). Check the model using an evaluation dataset (as needs be). Save the model to your documents directory then compile the model using MLModel.compileModel(at: NOTE: the resulting file has to be copied to your documents directory for later use.
  4. Put some sort of logic into your app to create the model only once, or on demand.
  5. Some models (e.g. linear regression) require a special class (MLFeatureProovider) to associate model inputs with model output. If so, create such a class in your app - ref https://developer.apple.com/forums/thread/685494
  6. For the "normal" use of the app, load the compiled model from the documents directory (e.g. MLModel.load(contentsOf: ) and do any other initiation (e.g initialise an NLModel from the MLModel).
  7. Present data to the model for making a prediction: how this happens depends on the type of model and whether an MLFeatureProvider is required. Some models (e.g. MLTextClassifier) can provide a number of hypothetical classifications with their confidence (probability) scores for a single input request. It can be wise to use this method to check the confidence and ignore "suggestions" below a certain threshold, e.g. 50%.

Regards, Michaela

Thank you, AncientCoder. I had a different use case in mind. You proposed using pre-made architectures that can be trained on the device (classifiers, regressors, etc.), but I meant something much lower-level than this.

For example, I would like to be able to create *.mlmodel that takes 2 inputs and just adds them together. Or any architecture using available ops. I can write MIL definition, but there is no way of turning it to the mlmodel/mlpackage in Swift, only using coremltools in Python.

It seems that MPSGraph is probably the best fit for my needs right now.

How to create ML program from Swift?
 
 
Q