machine-learningtext-classificationcoremlmaximum-entropy

How can I get the confidence variable from a CoreML prediction


I am using the CreateML tool to train a text classifier, when I use the preview feature and put in a sentence it will give me a prediction along with a confidence variableenter image description here

Here is how I am using the model on the app

import CoreML
...

    func predict(phrase:String) -> String {
        guard let rollModel = try? Roll(configuration: MLModelConfiguration()) else {
            return "Failed to load the Roll Model."
        }

        let rollModelInput = RollInput(text: phrase)

        guard let prediction = try? rollModel.prediction(input: rollModelInput, options: MLPredictionOptions()) else {
            return "Roll Model Prediction Failed"
        }
        
        return prediction.label
    }

This is working, it's providing a prediction.

My data is in the standard text/label format

Even when I export the model to xcode and run the preview in xcode the confidence variable is present.

When I run the prediction on the device I want to know what the confidence variable is, how can I get access?

enter image description here


Solution

  • To best use the text classifiers, you should be utilising NLModel to generate the predictions.

    The key is using predictedLabelHypotheses(for:maximumCount:)

    Your new code would look a little similar to;

    let model: FVCompeitionTextClassifier? = {
        let config = MLModelConfiguration()
        return try? Roll(configuration: config)
    }()
    
    func predict(phrase:String) throws -> String {
        let rollModel = try NLModel(mlModel: model!.model)
        
        for labelHypothese in rollModel.predictedLabelHypotheses(for: phrase, maximumCount: 1) {
            print("Label: \(labelHypothese.key), Confidence: \(labelHypothese.value)")
            // Do logic that you need to do ie sort, then return the label you need
        }
        
    }