Giter Club home page Giter Club logo

swiftneuralnetwork's Introduction

Swift Neural Network

A neural network built in Swift and accelerated by SIMD math. This is a feed forward neural network that can be trained on device with good performance. No need to use a server!

This is my first foray into machine learning. It is well tested and being used in production, but please file an issue or create a pull request if you see anything wrong or have any questions.

How is this different than CoreML or CreateML?

Both CoreML and CreateML are excellent frameworks provided by Apple to assist developers in machine learning. SwiftNeuralNetwork is different than these frameworks however, because SwiftNeuralNetwork is meant to be taught on an iOS device.

Installation

You can add SwiftNeuralNetwork to your project through Carthage by adding the following to your Cartfile.

github "jasonsilberman/SwiftNeuralNetwork"

Usage

Below is an example of some basic usage, you can look at /tests to see some more advanced uses.

Creation & Training

To get started create an instance of NeuralNetwork and train some data to it.

// This creates a network with three layers of the following sizes(2 nodes, 8, 1).
// The input layer has no activation function, while the second two both use a sigmoid function.
let network = NeuralNetwork(layerStructure: [2, 8, 1], activationFunctions: [.none, .sigmoid, .sigmoid])

// We are going to train our network about the XOR operator.
let trainingData: [[Double]] = [[0, 1], [0, 0], [1, 1], [1, 0]]
let trainingResults: [[Double]] = [[1], [0], [0], [1]]

network.train(inputs: trainingData, targetOutputs: trainingResults, learningRate: 0.9)

Predictions

let result = network.infer(input: [0, 1])

Cost Calculation

You can calculate the error or cost of the network by using the following function:

let RSS = network.rss(inputs: trainingData, targetOutputs: trainingResults)

Saving a model to disk

Saving a model to disk is useful so you don't have to retrain the model every time you launch the app. With SwiftNeuralNetwork you can do this very easily:

let encodedData = try JSONEncoder().encode(network)
try storedData.write(to: fileURL)

And then you can restore the network later:

let storedData = try Data(contentsOf: fileURL)
let recreatedNetwork = try JSONDecoder().decode(NeuralNetwork.self, from: storedData)

Activation Functions

The following activation functions are available, although some do not work properly yet. You can see the math for them here.

None

This should be used on the input layer, it applies no math to the inputs.

Sigmoid

This is the most common activation function and it will be used most frequently. It will crush all values between 0 and 1. This function has been tested the most thoroughly.

NOTE: The inputs to this function should be normalized, otherwise the function will not be that helpful.

LeakyReLU

This function only activates if the values are positive. This could be useful if you only want to turn on nodes if a value is positive. This has been somewhat tested.

Softamx

This function is useful for categorizing data. It is currently a work in progress and not supported.

NOTE: Do not use this function in production yet.

License

This project is licensed under the MIT License.

Inspiration

All of the following projects helped contribute to my learning and creation of this project. Some of the initial code/concepts were based on them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.