Inherits from NSObject
Conforms to NSCoding
Declared in SNNeuralNet.h

Overview

Represents a neural network. After creating a network with a specified number of inputs, outputs, and hidden layers, use the train:numRecords: method to train the network with an array of SNTrainingRecord structures. Then use the runInput: method to predict output for unknown inputs.

This example approximates the XOR function using a neural network:

#import "SNNeuralNet.h"

SNTrainingRecord records[] = {
    {SNInput(0,0), SNOutput(0)},
    {SNInput(0,1), SNOutput(1)},
    {SNInput(1,0), SNOutput(1)},
    {SNInput(1,1), SNOutput(0)}
};

SNNeuralNet *net = [[SNNeuralNet alloc] initWithTrainingData:records numRecords:4 numInputs:2 numOutputs:1];

double *output = [net runInput:SNInput(1, 0)];
printf("%f\n", output[0]); // 0.987

Tasks

Initializing a neural network

Neural network tasks

Configuration

Properties from initialization

Other properties

Properties

hiddenLayers

Array of hidden layer sizes the network was created with. Read only.

@property (readonly) NSArray *hiddenLayers

Discussion

Array of hidden layer sizes the network was created with. Read only.

Declared In

SNNeuralNet.h

isTrained

Whether the neural network has been trained. It can only be trained once.

@property (readonly) BOOL isTrained

Discussion

Whether the neural network has been trained. It can only be trained once.

Declared In

SNNeuralNet.h

learningRate

The learning rate of the network

@property (nonatomic) double learningRate

Discussion

The learning rate of the network

Declared In

SNNeuralNet.h

maxIterations

The maxium number of iterations to perform while training

@property (nonatomic) int maxIterations

Discussion

The maxium number of iterations to perform while training

Declared In

SNNeuralNet.h

minError

The error threshold to reach while training, unless maxIterations is reached first

@property (nonatomic) double minError

Discussion

The error threshold to reach while training, unless maxIterations is reached first

Declared In

SNNeuralNet.h

momentum

Momentum of learning from previous inputs

@property (nonatomic) double momentum

Discussion

Momentum of learning from previous inputs

Declared In

SNNeuralNet.h

numInputs

Number of inputs the network was created with. Read only.

@property (readonly) int numInputs

Discussion

Number of inputs the network was created with. Read only.

Declared In

SNNeuralNet.h

numOutputs

Number of outputs the network was created with. Read only.

@property (readonly) int numOutputs

Discussion

Number of outputs the network was created with. Read only.

Declared In

SNNeuralNet.h

Instance Methods

initWithInputs:hiddenLayers:outputs:

Initializes an SNNeuralNet with a number of inputs, an array of hidden layer sizes, and a number of outputs

- (instancetype)initWithInputs:(int)numInputs hiddenLayers:(NSArray *)hiddenLayers outputs:(int)numOutputs

Parameters

numInputs

Number of inputs to network

hiddenLayers

Array of hidden layer sizes.

numOutputs

Number of outputs from network

Return Value

An initialized SNNeuralNet

Discussion

Initializes an SNNeuralNet with a number of inputs, an array of hidden layer sizes, and a number of outputs

Declared In

SNNeuralNet.h

initWithInputs:outputs:

Initializes an SNNeuralNet with a number of inputs and outputs, and one default hidden layer.

- (instancetype)initWithInputs:(int)numInputs outputs:(int)numOutputs

Parameters

numInputs

Number of inputs to network

numOutputs

Number of outputs from network

Return Value

An initialized SNNeuralNet

Discussion

Initializes an SNNeuralNet with a number of inputs and outputs, and one default hidden layer.

Declared In

SNNeuralNet.h

initWithTrainingData:numRecords:numInputs:numOutputs:

Initializes and trains an SNNeuralNet in one step

- (instancetype)initWithTrainingData:(SNTrainingRecord *)trainingData numRecords:(int)records numInputs:(int)numInputs numOutputs:(int)numOutputs

Parameters

trainingData

Array of training records

records

Number of records in trainingData

numInputs

Number of inputs to network

numOutputs

Number of outputs from network

Return Value

An initialized and trained SNNeuralNet

Discussion

Initializes and trains an SNNeuralNet in one step

Declared In

SNNeuralNet.h

runInput:

Runs the neural network on an array of inputs of length numInputs

- (double *)runInput:(double *)input

Parameters

input

Array of doubles to use as input to the neural network. Could be created with the SNInput macro

Return Value

Array of doubles with output of neural network

Discussion

Runs the neural network on an array of inputs of length numInputs

Declared In

SNNeuralNet.h

train:numRecords:

Trains the neural network with a set of training data.

- (double)train:(SNTrainingRecord *)trainingData numRecords:(int)records

Parameters

trainingData

Array of training records

records

Number of records in trainingData

Return Value

Returns the amount of training error that occurred. The neural network can only be trained once, and will return -1 if you attempt to train it multiple times.

Discussion

Trains the neural network with a set of training data.

Declared In

SNNeuralNet.h