Initializer

init(trainingData:parameters:)

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

Declaration

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

Parameters

trainingData

The labeled audio files that you use to train this model, contained in a data source.

parameters

Parameters that you use to configure model training.

Discussion

You create an instance of this class by passing it an MLSoundClassifier.DataSource of training data. The following example shows how build a data source from labeled directories of audio files and use it to create a model to classify sounds.

// 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 soundsURL = documentsURL.appendingPathComponent("Sounds")

// The Sounds directory should contain subdirectories that define the labels
// and each subdirectory should contain multiple audio files to use for training.
// For example:
// Sounds
// -- Laughter
//      Laughter1.wav
//      Laughter2.wav
// -- Applause
//      Applause1.wav
//      Applause2.wav
let trainingData = MLSoundClassifier.DataSource.labeledDirectories(at: soundsURL)

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

See Also

Creating and Training a Sound Classifier

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

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

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.