Swift Artificial Intelligence (SAI) framework aims to be an easy to use library for creating, training and evaluating artificial neural networks.
SAI aims to support multitude of platforms including MacOs, iOS and TvOS.
Read more on saiframework.com
App expects the MNIST image dataset for training and classification of handwritten characters to be present under Data/mnist_png
(with training and testing subdirectories). You can download the dataset here.
If you don't download and expand the dataset, the Xcode build process will try it to do automatically so the first build of the app might take longer.
let hiddenLayer = PerceptronHiddenLayer(numberOfInputs: 14*14,
neuronCount: 66,
neuronType: BasicDeepNeuron.self)
let outputLayer = PerceptronOutputLayer(numberOfInputs: hiddenLayer.numberOfOutputs,
neuronCount: 10,
neuronType: BasicOutputNeuron.self)
guard let trainingInputLayer = UIImageTrainingInputLayer(samples: samples) else {return}
guard let network = BasicTrainableFeedForwardNetwork(trainingLayer: trainingInputLayer,
outputLayer: outputLayer,
hiddenLayers: [hiddenLayer]) else {return}
network.train(count: 1000,
stop: nil,
trainingSetSampleCompletion: nil,
trainingSetCompletion: nil,
completion: { finished, avgError in
//training finished
})
let inputLayer = UIImageInputLayer(image: image)
let array = net.evaluate(inputLayer: inputLayer).outputs
//as the result we take the index of the highest probability
let result = array.index(of: array.max() ?? -10.0) ?? -1
- Multilayer perceptron
This example includes two frameworks, first is the SAI.framework which is the actual SAI framework as advertised. The second framework is called ExamplesSAIIrrelevant.framework as is necessary only for this example app. It contains all the irrelevant stuff not related to the work with the artificial networks like enabling/disabling buttons on the UI or providing data to a UITableView. We did choose this approach so that the example app only contains code relevant to creating, training, evaluating, loading and storing of artificial networks so you don't have a hard time to find relevant code.