Initializer

init(trainingData:parameters:)

Creates a sound classifier from a training data set represented by a dictionary.

Declaration

init(trainingData: [String : [URL]], parameters: MLSoundClassifier.ModelParameters = ModelParameters()) throws

Parameters

trainingData

The labeled audio files that you use to train the model. The keys of this dictionary are the labels, and the values are the associated audio-file URLs.

parameters

That parameters that you use to configure the model training.

Discussion

You create an instance of this class by passing it a dictionary of training data. The following example shows how to define a training data set to classify audio as either laughter or applause.

// Get the Documents directory URL.
guard let documentsURL = FileManager.default.urls(for: .documentDirectory,
                                               in: .userDomainMask).first else {
    fatalError("Can't find Documents directory.")
}

// Get the URL of the directory containing the training data.
let url = documentsURL.appendingPathComponent("Sounds")

// For a real classifier, use at least 10 sounds per label. More is better.
let trainingData = ["Laughter": [url.appendingPathComponent("Laughter.1.aif"),
                                 url.appendingPathComponent("Laughter.2.wav")],
                    "Applause": [url.appendingPathComponent("Applause.1.mp3"),
                                 url.appendingPathComponent("Applause.2.caf")]]

do {
    let soundClassifier = try MLSoundClassifier(trainingData: trainingData)
} catch {
    // Handle creation error.
}

See Also

Creating and Training a Sound Classifier

init(trainingData: MLSoundClassifier.DataSource, parameters: MLSoundClassifier.ModelParameters)

Creates a sound classifier from a training data set represented by a data source.

enum MLSoundClassifier.DataSource

An enumeration that describes various ways to label audio-file URLs stored on disk.

struct MLSoundClassifier.ModelParameters

A structure that describes additional model parameters that you can set on the sound classifier.

let modelParameters: MLSoundClassifier.ModelParameters

The configuration parameters used to train the model during initialization.