Swiftpack.co - Package - thevojacek/NeuralNetwork

NeuralNetwork

An implementation of a basic neural networks in and for Swift 4.

CREDIT

Based on work of Bilal Reffas. Credit for actual neural network logic algorithm goes to him. I used his open-sourced code, modified it and wrap it into this neat package.

CAUTION - no compilation for Linux machines at the moment

This package will not compile on Linux machine, due to using arc4random_uniform function from BSD based UNIX systems. This is going to be fixed once Apple releases Swift 4.2 with new possibilities of generating random numbers.

USAGE EXAMPLE

import NeuralNetwork

// Training data example.
let trainingData: [[Float]] = [[0, 0], [0, 1], [1, 0], [1, 1]]
let trainingResults: [Float] = [0, 1, 1, 1]

// Initialization.
let network: NeuralNetwork = NeuralNetwork(inputSize: 2, hiddenSize: 4, outputSize: 1)

// Training.
for _ in 0...90000 {
    for q in 0..<trainingResults.count {
        network.train(
            input: trainingData[q],
            targetOutput: [trainingResults[q]],
            learningRate: NeuralNetwork.defaultLearningRate,
            momentum: NeuralNetwork.defaultMomentum
        )
    }
}

// Running network.
let result: [Float] = network.run([0, 0])

do {
    // Saving neural network to JSON.
    let save = try network.saveToJSON()

    // Initializing trained neural network from JSON.
    let loadedNetwork = try NeuralNetwork(fromJSON: save)
} catch {}

Github

link
Stars: 0
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

1.1.1 - Jul 17, 2018