完全Swift开发的高性能AI和机器学习库:Swift AI

jopen 8年前

Swift AI是一个完全采用Swift开发的高性能AI和机器学习库。这些工具完已经专门为iOS 和OS X应用优化。

Features

  • Feed-Forward Neural Network
  • Recurrent Neural Network
  • Convolutional Network
  • GPU-Accelerated Networks
  • Genetic Algorithms
  • Fast Matrix Library
  • Fourier Transform Functions

Installation

Grab the files you need, drag them into your project. That was easy!

Usage

Multi-Layer Feed-Forward Neural Network

FFNN.swift

TheFFNNclass contains a fully-connected, 3-layer feed-forward neural network. This neural net uses a standard backpropagation training algorithm (stochastic gradient descent), and is designed for flexibility and use in performance-critical applications.

It's fast, lightweight and perfect for use with both OS X and iOS!

Creating anFFNNinstance is easy...

let network = FFNN(inputs: 100, hidden: 64, outputs: 10,                  learningRate: 0.7, momentum: 0.4, weights: nil)

You must provide six parameters to the initializer:

  • inputs: The number of input nodes (aka, 'neurons'). This number corresponds to the dimensionality of the data that you plan to feed the network. If the above example were to be used for handwriting recognition,100might be the number of pixels in each image being processed.
  • hidden: The number of nodes in the hidden layer. The ideal number of hidden nodes depends heavily on the application, and should be determined by testing. If you're completely unsure, [(inputs * 2/3) + outputs] might be a good place to start.
  • outputs: The number of output nodes. For classification problems (like recognizing handwritten digits), the number of outputs usually corresponds to the number of possible classifications. In this example, each output might correspond to one digit (0-9). The number of outputs depends entirely on the problem being applied.
  • learningRate: The 'learning rate' to apply during the backpropagation phase of training. If you're unsure what this means,0.7is probably a good number.
  • momentum: Another constant applied during backpropagation. If you're not sure, try0.4.
  • weights: An optional array ofFloats used to initialize the weights of the neural network. This allows you to 'clone' a pre-trained network, and begin solving problems without training first. When you're creating a new network from scratch, leave this parameterniland random weights will calculated based on your input data.

You interact with your neural net using these five methods:

update - Accepts a single set of input data, and returns the resulting output as calculated by the neural net.

let output: [Float] = try network.update(inputs: imagePixels)

backpropagate - Used to train the network manually. Accepts the single set of expected outputs (aka 'answers') corresponding to the most recentupdatecall. Returns the total error, as calculated from the difference between the expected and actual outputs.

let error: Float = try network.backpropagate(answer: correctAnswer)

train - Initiates an automated training process on the neural network. Accepts all sets of inputs and corresponding answers to use during the training process and all sets of inputs and answers to be used for network validation, as well as an error threshold to determine when a sufficient solution has been found.

The validation data (testInputsandtestAnswers) will NOT be used to train the network, but will be used to test the network's progress periodically. Once the desired error threshold on the validation data has been reached, the training will stop. An appropriate error threshold should take into account the number of validation sets, as error is accumulated over all sets of data. Ideally, the validation data should be randomly selected and representative of the entire search space.

Note: this method will block the calling thread until it is finished, but may safely be dispatched to a background queue.

let weights = try network.train(inputs: allImages, answers: allAnswers,                testInputs: validationImages, testAnswers: validationAnswers,                errorThreshold: 0.2)

getWeights - Returns a serialized array of the network's current weights.

let weights = network.getWeights()

resetWithWeights - Allows the user to reset the network with specified weights. Accepts a serialized array of weights, as returned by thegetWeights()method.

try network.resetWithWeights(preTrainedWeights)

More information can be found in the documentation.

Additional Information

To achieve nonlinearity,FFNNuses the sigmoid activation function for hidden and output nodes. Because of this property, you will achieve better results if the following points are taken into consideration:

  • Input data should be normalized to have a mean of0and standard deviation of1.
  • Outputs will always reside in the range (0, 1). For regression problems, a wider range is often needed and thus the outputs must be scaled accordingly.
  • When providing 'answers' for backpropagation, this data must be scaled in reverse so that all outputs also reside in the range (0, 1).

Softmax activation will be added soon.

项目主页:http://www.open-open.com/lib/view/home/1448282136501