CreateML usage and basic concepts of machine learning

1 Introduction

Before learning CreateML, let us first understand what is machine learning ? There is no widely accepted definition of exactly what machine learning is. The first definition of machine learning came from Arthur Samuel. He defined machine learning as a field that gives computers the ability to learn when specifically programmed. Samuel's definition goes back to the 1950s, when he wrote a chess program. The magic of this program is that the programmer is very good at playing chess by himself, but through programming, he let the chess program play tens of thousands of games with himself. Over time, this chess program understands what is a good layout and what is a bad layout. . After learning the program, his level of playing chess far exceeded that of Samuel. The above is a somewhat informal definition and is relatively old.

Another more recent definition, proposed by Tom Mitchell, comes from Carnegie Mellon University. Tom defines machine learning as a program that learns from experience E to solve a task T and achieve a performance measure P if and only if After having experience E and judging by P, the program's performance in processing T has improved. In the above example, experience E is the experience of the program practicing tens of thousands of times and task T is playing chess. The performance measure value P is the probability of winning the game when it competes with some new opponents.

Machine learning can be divided into supervised learning (Supervised Learning) and unsupervised learning (Unsupervised Learning). The basic idea of ​​supervised learning is that each sample in our data set has a corresponding output, and then predictions are made based on these samples. In unsupervised learning , a series of unlabeled training data is input into an algorithm, and then we tell the algorithm to find the intrinsic structure of the data for us.

2. What is CreateML?

Create ML is a tool for generating machine learning models launched by Apple at WWDC 2018. It uses machine learning algorithms to train models to solve practical problems. The training involved in CreateML is supervised learning, which mainly deals with classification problems, regression problems and recommendation, corresponding to MLClassifier, MLRegressor and MLRecommender. The following mainly introduces the corresponding background knowledge of machine learning to help everyone understand better.

Briefly introduce how CreateML trains the model. It can use the built-in machine learning infrastructure of the system (iOS 12 or macOS Mojave) for transfer learning (Transfer Learning). This kind of training requires a small amount of data and is enough. The trained model is The last few layers of Apple's built-in model are trained on our data. In this way, the training time is greatly reduced, and the training can even be completed in a few seconds. The model size can also be reduced to the kb level, making it easier to integrate into our App. Although a small amount of data is required, it cannot be created out of thin air. Students who want to find data can look here .
Transfer Learning

3. What is a classification problem?

Let's look at a set of data: In this diabetes prediction data set, the first 8 columns are features, the last column is label, and 1 and 0 indicate whether it is diabetes.
training data
So the question with machine learning is, can you estimate whether someone else has diabetes. In technical terms, this is a classification problem.

Classification means that we are trying to infer a discrete output value: 0 or 1 whether it is diseased or not, when in fact in a classification problem the output may be more than two values. For example, there may be two types of diabetes, so you want to predict the discrete output 0, 1, 2. 0 represents no diabetes, 1 represents type 1 diabetes, and 2 represents type 2 diabetes, but this is also a classification problem.

Therefore, when using CreateML, you need to pass in the training data, as well as the label column (labelColumn) and the feature column (featureColumn). According to the actual situation, multiple or more Column can be supported.

Of course, recognizing objects by looking at pictures is also a classification problem. Its input is all the pixels of a frame of a picture or video. In order to improve calculation efficiency and accuracy, the original features are processed through pooling layers, convolution layers, etc.

Different classifiers (Classifier) ​​in Create ML will set corresponding DataSource to describe features and labels. This is audio-related annotation information.

/// - featureColumn: The name of the column that contains the audio
// features.
///
/// - labelColumn: The name of the column that contains the audio
/// labels.

4. What is a regression problem?

For example, to estimate the housing price on Mars, we can apply a learning algorithm to draw a straight line in this set of data, or fit a straight line. Based on this line, we can infer the price when the number of solar panels is 3. Of course, This is not the only algorithm. There may be better ones. For example, we don't need to fit these data with a straight line. We can also use a quadratic equation or a higher-order equation to fit it according to the actual situation.

Assume that housing prices are only accurate to integers, so housing prices are actually a series of discrete values, but we usually regard housing prices as real numbers, that is, a continuous value.

The word regression means that we are trying to infer this series of continuous-valued attributes. This is the regression problem, which is to derive a continuous output through regression. Insert image description here
For regression problems, when training the model in CreateML, the data that needs to be passed in is basically the same, that is, the training data needs to be passed in, as well as the label column (named here as targetColumn, the essence is still the same) and the feature column (featureColumn)

///   - targetColumn: A String specifying the target column name in the trainingData
///   - featureColumns: An optional list of Strings specifying feature columns to be
///                     used to predict the target, if not provided, default to use all the
///                     other columns in the trainingData, except the one specified by targetColumn

5. Recommendation system

Recommendation systems may be unfamiliar to everyone. Let’s take an example to define the problem of recommendation systems.

Suppose we are a movie provider, we have 5 movies and 4 users, and we ask the users to rate the movies.
Insert image description here
The first three movies are romance movies, and the last two are action movies. We can see that Alice and Bob seem to be more inclined to romance movies, while Carol and Dave seem to be more inclined to action movies. And no single user rated all the movies. We want to build an algorithm to predict what rating each of them might give to a movie they haven't seen, and use that as a basis for recommendations.

In this example, it is a form of recommendation problem. We assume that each movie has two features, such as x 1 represents the romance index of the movie, and x 2 represents the action index of the movie. [x 1 , x 2 ] are the characteristics of the movie. We can perform linear regression on each person through the characteristic data to supplement the ratings of other movies. This is a content-based recommendation system (Content Based Recommendations). If you want to know more about it, you can View from here .

In the above example, we have user parameters and movie characteristics. In fact, there is another form of recommendation problem, which is that we have neither of these two. How to solve this problem. Currently, there is a collaborative filtering algorithm (Collaborative Filtering) that can learn both at the same time, which is equivalent to self-learning features. Not all the data learned can be read, but these data can be used as a basis for recommending movies to users. If you want to know more details, you can read here .

From the above introduction, we can see that the training data includes three parts, recommended elements, users and ratings. In CreateML, our training data also needs to include these three columns of data.

///  - itemColumn: Name of the Int or String typed column in the training data containing item identifiers.
///
///  - userColumn: Name of the Int or String typed column in the training data containing user identifiers.
///
///  - ratingColumn: Name of an Int or Double typed column optionally in the training data containing scores or ratings.
///                        The default is nil, which corresponds to no rating column.

6 How to train a recommendation model

6.1 Using CreateML App

CreateML App is used for training. It is a simple and easy-to-use visual operation. Just drag the data. Here is an article introducing each model type and examples of how to use it. You can read it here. I will also add a link on how to train image
CreateML App
classification .

6.1 Training using Swift code

The code is also relatively convenient. Here is an example of MLTextClassifier. Except for the different data formats, the usage of other types is basically the same. For logistic regression code examples, you can view the reference article [5]. The data preparation will be different, but the usage is basically the same.

The purpose of training this model is to analyze a sentence and determine whether the attitude of the sentence is positive or negative.

The first step is to prepare the data. The format of the data file is csv, and the data is divided into two columns. The class is the label column, and the text is the feature column. The names of the label column and feature column can be defined according to the needs, and the order can be arbitrary.
Insert image description here
The second step is to train and evaluate the model. After the model training is completed, the accuracy of the model needs to be evaluated. Therefore, our training data can be divided into two parts, a large part is used for training, and a small part is used for evaluation and verification. In this example, the ratio of training and verification data is 9:1, which can be flexibly adjusted according to the amount of data.
Among them, metrics.classificationError represents the error rate of the data in the verification set. If the error rate is relatively high, you can adjust the data set or update the algorithm to adjust.

Currently, the training API has been updated. The following example uses the MLDataTable data format, which has been deprecated in macOS 13.0 and iOS 16.0. The specific interface can be selected according to the system version of the device. The new version of MLTextClassifier.DataSource can also support multiple Labels can be randomly assigned to multiple groups for cross-validation to reduce over-fitting, under-fitting and other problems.

guard let url = mlFileURL,
      let allData = try? MLDataTable(contentsOf: url) else {
    
    
    return
}

let data: TrainingData = allData.randomSplit(by: 0.9)
guard let classfier = try? MLTextClassifier(trainingData: data.trainingData, textColumn: "text", labelColumn: "class", parameters: MLTextClassifier.ModelParameters()) else {
    
    
    return
}
let metrics = classfier.evaluation(on: data.testData, textColumn: "text", labelColumn: "class")
print("classificationError: \(String(describing: metrics.classificationError))")

The third step is to save the model if it is useful.

// 保存模型
func saveModel(toFile: String) {
    
    
    try? classfier?.write(toFile: toFile)
}

Step 4 : Use the model to predict the data

This is as simple as calling a function. Just enter a piece of text and check the prediction results.

func prediction(_ text: String) {
    
    

    guard let ret = try? classfier?.prediction(from: text) else {
    
    
        print("prediction error")
        return
    }
    print("prediction result: \(String(describing: ret))")
}

6. Reference articles:

  1. WWDC 2018: A first look at Create ML
  2. CreateML: Start Your Adventure in Machine Learning with Swift
  3. MLDataTable: The Panda For iOS Developers
  4. Introduction to the use of CreateML and its application in iOS
  5. Build a Core ML Movie Recommender SwiftUI App Using CreateML

Guess you like

Origin blog.csdn.net/ID314846818/article/details/130935312