CocoaPods trunk is moving to be read-only. Read more on the blog, there are 17 months to go.

NeuralKit 0.1.11

NeuralKit 0.1.11

Maintained by Sylvan Martin.



NeuralKit 0.1.11

  • By
  • Sylvan Martin

NeuralKit

Implementation of the Neural Network structure in swift to make machine learning easier in iOS apps.

Installation

NeuralKit is available on CocoaPods. To download, add the following to your podfile:

pod 'NeuralKit'

Why did I make NeuralKit?

The current default Swift library for machine learning (CoreML) is not very intuitive and does not give the user much control over the actual training of the network. Also, I was bored and wanted to learn more about machine learning :)

Documentation

https://sylvanm.github.io/NeuralKit/

How to use

Creating a network

// Set the dimension for the network
// This is an array that has the sizes of each layer.
//
// The following will describe a network with an input layer size of 2, hidden layer sizes of 3, 3, and 4, and an output layer size of 2.
let dimension = [2, 3, 3, 4, 2]

// You can initialize a network with the dimension array, and optionally with random weights and biases.
let network = Network(networkDimension: dimension, withRandomWeights: true)

Input and Output

// Set input
let input = [3.0, 42.1]
network.set(input: input)

// Compute output
let output = network.compute()

Advanced Network Settings

// The network uses an activation function to decide how to 'squish' the weighted sum into a neuron activation.
// By default, this is the "Rectified Linear Units" functions.
//
// There are default functions that do this defined in the ActivationFunctions struct, and you can tell the network to use those:
network.activationFunction = ActivationFunctions.sigmoid // Sets the activation function to the sigmoid function
network.activationFunction = ActivationFunctions.relu    // Sets the activation function to the rectified linear units function

// You can also make your own function if you so wish.
// It be a closure that fits the typealias Activation Function (Defined in ActivationFunctions.swift), or (Double) -> Double.
let myActivationFunction: ActivationFunction = { x in
    return 1 / (1 + exp(-x)) // This is techincally the same as the ActivationFunctions.sigmoid function
}

network.activationFunction = myActivationFunction

Training

// To do any training operations, you must first have dataset, an NKTrainingData object, and an NKTrainer.
// You must first define your dataset, which is a dictionary, with inputs corresponding to desired outputs.
let dataset: [[Double] : [Double]] = [ // data was chosen arbitrarily
    [0.0, 0.0] : [0.4, 0.2],
    [0.1, 0.8] : [2.1, 9.3],
    [5.2, 9.0] : [13.3, 19.0]
]

// Now make an NKTrainingData object
let trainingData = NKTrainingData(dataset)

// Now make an NKTrainer, and set the training data
let trainer = NKTrainer()
trainer.loadTrainingData(trainingData)

// With your dataset, you can get the cost (or loss) of your network.
do {

    print(network)

    let cost = try trainer.cost(of: network)
    print("Cost:", cost)

} catch {
    print("Error:", error)
}

Exporting

When you train the network, you will want to save the structure of the trained network so you don't have to start all over. With NeuralKit, you can export the network as a Data object, with which you can do whatever you want; save it, encrypt it, whatever. You can then import the network back to create a network from the saved data.

do {

    // save the network
    let networkData: Data = try network.exportNetwork()

    // from here, you can do whatever with the data, maybe write it to a file to save, and continue training later.

    // now load a network from the data
    let loadedNetwork: Network = try Network.importNetwork(fromData: networkData)
    print(loadedNetwork)

} catch {
    print(error)
}