Is it possible to use coreML to implement a multi-label text classification?
UPDATE: The documentation (referred to above) is confusing. To make predictions, we have to initialise an NLModel with an MLTextClassifier then call the predictedLabel or predictedLabelHypotheses method. Here's my (partial) code:
// Load previously trained and saved category (label) model
func loadCategoryModel() {
let modelURL = docsURL!.appendingPathComponent("CategoryModel.mlmodelc")
let modelConfig = MLModelConfiguration()
modelConfig.parameters = [:]
MLModel.load(contentsOf: modelURL, configuration: modelConfig) { result in
switch result {
case let .success(model):
DispatchQueue.main.async {
self.mlModel = model
}
self.getlatestData()
case let .failure(error):
print("ERROR: loading model failed \(error)")
}
}
}
func predictCategory(_ venue: String) -> String {
// trying to predict category of an entity from the venue name (usually business name) - demo code
do {
let categoryPredictor = try NLModel(mlModel: mlModel!)
let predictedLabel = categoryPredictor.predictedLabel(for: venue) ?? "n/a"
let possibleLabels = categoryPredictor.predictedLabelHypotheses(for: venue, maximumCount: 2)
print(venue,"Predictions: ",predictedLabel,possibleLabels)
// prints ==> "Costco Warehouse Predictions: General Store ["Pharmacy": 0.17649094106070892, "General Store": 0.2769075597746391]"
return predictedLabel
} catch {
print("ERROR: prediction failed")
return "unknown"
}
}
So, for your needs, you could train your data with multiple labels (as per earlier suggestion) then call predictedLabelHypotheses and check the probabilities provided, using those outputs (labels) that pass whatever threshold you determine.
Regards, Michaela