Skip to content

Latest commit

 

History

History
678 lines (551 loc) · 38.1 KB

docs.md

File metadata and controls

678 lines (551 loc) · 38.1 KB

neural

A neural network toolkit for Node.js and the browser.

neural~Neuron

Kind: inner class of neural

new Neuron(params)

Creates a new Neuron

Param Type Default Description
params Object
[params.transfer] function 'logSigmoid' The neuron's transfer function.
[params.inputs] Array.<number> An array of inputs to the neuron, which can be numbers, functions returning a number or Neurons
[params.weights] Array.<number> The input weights with which to initialise the Neuron. Defaults to zeros.

neuron.setInputs(newInputs)

Resets the inputs to a neuron. Any existing inputs are removed.

Kind: instance method of Neuron

Param Type Description
newInputs Array.<(number|function()|Neuron)> Array of inputs to the Neuron, which can be numbers, functions returning a number or Neurons.

neuron.getInputs() ⇒ Array

Returns the Neurons inputs.

Kind: instance method of Neuron
Returns: Array - The Neurons inputs, which could be numbers, functions returning a number or Neurons.

neuron.getInputCount() ⇒ number

Returns the number of inputs feeding this neuron.

Kind: instance method of Neuron
Returns: number - count of inputs, which could be numbers, functions returning a number of Neurons.

neuron.setWeights(newWeights)

Updates the Neuron's weights.

Kind: instance method of Neuron

Param Type Description
newWeights Array.<number> An array of numbers, which must be of the same length as the Neuron's array of inputs.

neuron.getWeights() ⇒ Array

Returns the Neuron's weights.

Kind: instance method of Neuron
Returns: Array - The neuron's weights (numbers).

neuron.getId() ⇒ number

Returns the Neuron's id.

Kind: instance method of Neuron
Returns: number - The Neuron's id, which is unique at the module level.

neuron.getTransfer() ⇒ fn

Get the Neuron's transfer function

Kind: instance method of Neuron
Returns: fn - The transfer function

neuron.setTransfer(The)

Sets the Neuron's transfer function

Kind: instance method of Neuron

Param Type Description
The function replacement transfer function (see section on transfer functions for details of acceptable formats).

neuron.getActivation() ⇒ number

Gets the Neuron's current activation value. Note that this does NOT recalculate the value if input values or weights have changed.

Kind: instance method of Neuron
Returns: number - The most recently-calculated activation value.

neuron.getInputSum() ⇒ number

Gets the Neuron's current input sum. This is the weighted sum of inputs, prior to having been passed through the transfer function. Note that this does NOT recalculate the value if input values or weights have changed.

Kind: instance method of Neuron
Returns: number - The most recently-calculated input sum.

neuron.getDelta() ⇒ number

Gets the Neuron's current delta.

Kind: instance method of Neuron
Returns: number - The Neuron's delta.

neuron.isOutput() ⇒ boolean

Indicates whether the Neuron is in the output layer of a Network

Kind: instance method of Neuron

neuron.setExpected(Expected)

Sets the expected activation value (from training data) of the Neuron

Kind: instance method of Neuron

Param Type Description
Expected number output value

neuron.addInput(The)

Plugs in a new input to the Neuron, which is initialised with a respective weight of 0.

Kind: instance method of Neuron

Param Type Description
The number | function | Neuron new input.

neuron.calc() ⇒ number

Recalculates the activation value, recalculating all the input nodes in turn as required.

Kind: instance method of Neuron
Returns: number - The recalculated activation value.

neuron.error() ⇒ number

The difference between the most recently calculated activation value and the last expected value to be supplied.

Kind: instance method of Neuron
Returns: number - Difference between activation and expected values.

neuron.invalidate()

Marks the Neuron as in need of recalculation by clearing the activation value cache.

Kind: instance method of Neuron

neuron.calcDelta() ⇒ number

Recalculates the Neuron's delta.

Kind: instance method of Neuron
Returns: number - The new delta.

neuron.getOutputWeightPartials() ⇒ Array

Returns the partial derivative of the Neuron's error with respect to its output weights.

Kind: instance method of Neuron
Returns: Array - The output weight partials.

neuron.getInputWeightPartials() ⇒ Array

Returns the partial derivatives of the input Neurons errors with respect to this Neuron's input weights.

Kind: instance method of Neuron
Returns: Array - The input weight partials.

neuron.randomizeWeights([max], [min])

Randomizes the Neuron's input weights.

Kind: instance method of Neuron

Param Type Default Description
[max] number 0.000001 Maximum possible weight.
[min] number 0 Minimum possible weights.

neural~Layer

Kind: inner class of neural

new Layer(params)

Creates a new Network Layer

Param Type Description
params Object
[params.neurons] number | Array.<Neuron> The Neurons in the layer. If an integer is passed, that number of Neurons will be constructed for this Layer.

layer.isOutput() ⇒ boolean

Returns whether this an output layer. Note that all layers are output layers by default, until they have their neurons plugged into another layer.

Kind: instance method of Layer

layer.setStatus(statuses)

Overrides the layer's status.

Kind: instance method of Layer

Param Type Description
statuses Object
statuses.input boolean Whether the layer should be marked as an input layer.
statuses.output boolean Whether the layer should be marked as an output layer.

layer.plug(outputLayer) ⇒ Layer

Plugs this Layer into another Layer such that the Neurons in this Layer become input Neurons for the supplied Layer.

Kind: instance method of Layer
Returns: Layer - The supplied output Layer for chaining purposes.

Param Type Description
outputLayer Layer The Layer which will have its Neurons take the Neurons in this Layer as inputs.

layer.getNeurons() ⇒ Array

Returns the Neurons which make up this layer.

Kind: instance method of Layer

layer.setInputs(inputs, isInputLayer)

Sets the inputs for all the Neurons in this Layer.

Kind: instance method of Layer

Param Type Description
inputs Array.<(number|function()|Neuron)> An array of inputs, which could be numbers, functions returning a number or Neurons.
isInputLayer boolean Indicates whether this is intended to be the input layer in a Network. If set to true, rather than each of the supplied inputs being wired into each of the Neurons in this Layer, they will be mapped one-to-one, with no bias (i.e. passed straight through to the next layer).

layer.setExpected(An)

Sets the expected values for the activation values of the Neurons in this Layer.

Kind: instance method of Layer

Param Type Description
An Array.<number> array of output values (numbers), equal in length to the number of Neurons in this Layer.

layer.setTransfer(fn)

Sets the transfer function for every Neuron in this Layer

Kind: instance method of Layer

Param Type Description
fn function The replacement transfer function (see section on transfer functions for details of acceptable formats).

layer.calc() ⇒ Array

Recalculates the activation values for all of the Neurons in this Layer, recalculating their input Neuron values in sequence as required.

Kind: instance method of Layer
Returns: Array - The recalculated activation values.

layer.invalidate()

Invalidates the activation cache for all the Neurons in this Layer.

Kind: instance method of Layer

layer.calcDeltas() ⇒ Array

Recalculates the deltas for all Neurons in this Layer.

Kind: instance method of Layer
Returns: Array - The deltas for the Neurons in this Layer.

layer.getWeights() ⇒ Array.<Array.<number>>

Gets the weights for each Neuron in this Layer.

Kind: instance method of Layer
Returns: Array.<Array.<number>> - The weights for each Neuron in this Layer.

layer.setWeights(weights)

Update the input weights for the Neurons in this Layer.

Kind: instance method of Layer

Param Type Description
weights Array.<Array.<number>> The new weights for each Neuron in this Layer.

layer.getActivations() ⇒ Array

Returns the activation values for the Neurons in this Layer.

Kind: instance method of Layer
Returns: Array - The activation values.

layer.getInputSums() ⇒ Array

Returns the input sums for the Neurons in this Layer (the weighted sums of inputs for each Neuron before they've been passed through the transfer function).

Kind: instance method of Layer
Returns: Array - The input sums.

layer.getIds() ⇒ Array

Returns the ids for the Neurons in this Layer.

Kind: instance method of Layer
Returns: Array - An array of ids.

layer.getOutputWeightPartials() ⇒ Array.<Array.<number>>

Returns the output weight partials for the Neurons in this Layer.

Kind: instance method of Layer
Returns: Array.<Array.<number>> - Array of output weight partials for each Neuron.
See: Neuron#getOutputWeightPartials

layer.getInputWeightPartials() ⇒ Array.<Array.<number>>

Returns the input weight partials for the Neurons in this Layer.

Kind: instance method of Layer
Returns: Array.<Array.<number>> - Array of input weight partials for each Neuron.
See: Neuron#getInputWeightPartials

layer.randomizeWeights(e)

Randomizes the weights for all of the Neurons in this Layer.

Kind: instance method of Layer

Param Type Description
e number The randomized weights will be in the range [-e, e].

neural~Network

Kind: inner class of neural

new Network(params)

Creates a new Network

Param Type Description
params Object
[params.layers] Array.<number> An array of layer sizes, indicating the number of Neurons in each Layer (and implicitly, the number of Layers).

network.layerCount() ⇒ number

Returns the number of layers in the network.

Kind: instance method of Network

network.getLayers() ⇒ Array

Returns the Layer instances which make up the network (from input to output).

Kind: instance method of Network
Returns: Array - Array of Layers.

network.inputLayer() ⇒ Layer

Returns the Network's input Layer.

Kind: instance method of Network

network.outputLayer() ⇒ Layer

Returns the Network's output Layer.

Kind: instance method of Network

network.setInputs(inputs)

Sets the inputs for the Network's input Layer.

Kind: instance method of Network
See: Layer#setInputs for more details.

Param Type Description
inputs Array.<(number|function()|Neuron)> An array of inputs, which must be of the same length as the number of Neurons in the input Layer. Note that whilst these could be Neurons, in an input Layer they would more normally be numbers or functions.

network.setExpected(outputs)

Sets the expected outputs for the Network's output Layer.

Kind: instance method of Network
See: Layer#setOutputs for more details.

Param Type Description
outputs Array.<number> An array of expected values for the output Layer. This must be the same length as the number of Neurons in the output Layer.

network.calc() ⇒ Array

Calculates the output values for the Network based on the current inputs using forward propagation. Note that if Neuron activation values have been calculated since the previous invalidation, these will be used rather than recalculation occurring.

Kind: instance method of Network
Returns: Array - The output layer activation values resulting from the current network inputs.

network.getActivations() ⇒ Array.<Array.<number>>

Gets all activation values for all Neurons in all Layers in the Network.

Kind: instance method of Network
Returns: Array.<Array.<number>> - Activation values.

network.getInputSums() ⇒ Array.<Array.<number>>

Gets all input sums for all Neurons in all Layers in the Network.

Kind: instance method of Network
Returns: Array.<Array.<number>> - Input sums.
See: Neuron#getInputSum for more details.

network.invalidate()

Marks the activation cache for every Neuron in the Network as invalid.

Kind: instance method of Network

network.randomizeWeights(e)

Randomizes the input weights for all the Neurons in the Network.

Kind: instance method of Network
See: Layer#randomizeWeights for more details.

Param Type Description
e number Resulting weights will be in the interval [-e, e].

network.forwardPropagate(trial) ⇒ Array.<number>

Peforms a full forward propagation of the Network using the supplied input values. Also optionally sets the expected output values for error calculation and training.

Kind: instance method of Network
Returns: Array.<number> - The actual output values resulting from the supplied inputs with the current network weights.

Param Type Description
trial Object
[trial.inputs] Array.<number> An array of input values to feed into the Network's input Layer. This must be the same length as the number of Neurons in the input Layer.
[trial.outputs=] Array.<number> An array of output values to mark as the output Layer's expected activation values. This must be the same length as the number of Neurons in the output Layer.

network.sumSqError() ⇒ number

Returns the sum-squared error resulting from comparing the calculated Network outputs with the expected outputs, using the current inputs and weights.

Kind: instance method of Network

network.backPropagate() ⇒ Array.<Array.<number>>

Applies the back-propagation algorithm to recalculate the deltas for each Neuron in the Network, working from the output Layer to the input Layer.

Kind: instance method of Network
Returns: Array.<Array.<number>> - The deltas for the Neurons in each of the output Layers.

network.getIds() ⇒ Array.<Array.<number>>

Returns the ids for all the Neurons in the Network.

Kind: instance method of Network

network.getOutputWeightPartials() ⇒ Array.<Array.<Array.<number>>>

Gets the output weight partials for each Neuron in the Network

Kind: instance method of Network
Returns: Array.<Array.<Array.<number>>> - Input weight partials for each Neuron.
See: Neuron#getInputWeightPartials

network.getInputWeightPartials() ⇒ Array.<Array.<Array.<number>>>

Gets the output weight partials for each Neuron in the Network

Kind: instance method of Network
Returns: Array.<Array.<Array.<number>>> - Output weight partials for each Neuron.
See: Neuron#getOutputWeightPartials

network.getWeights() ⇒ Array.<Array.<Array.<number>>>

Gets the input weights for each Neuron in the network.

Kind: instance method of Network
Returns: Array.<Array.<Array.<number>>> - Input weights by input, Neuron and Layer.

network.setWeights(The)

Sets the weights for the entire network in one go. Useful for rebuilding a trained network.

Kind: instance method of Network

Param Type Description
The Array.<Array.<Array.<number>>> inner-most Arrays refer to the input weights for each Neuron. These should be arranged in Arrays corresponding to each Neuron in a Layer. Finally, the weights for each Layer should make up the outer-most Array.

neural~TrainingData

Kind: inner class of neural

new TrainingData(data)

Creates a new TrainingData object, which can be used to make generators which iterate over the supplied set of examples. This is very useful for repeated training on a single set of data.

Param Type Description
data Array.<object> An array of trial objects pertaining to individual training examples.
[trial.inputs] Array.<number> An array of inputs for this trial. This must be the same length as the number of Neurons in the input layer of the Network that it will be used to train.
[trial.outputs] Array.<number> An array of expected outputs for this trial. This must be the same length as the number of Neurons in the output layer of the Network that it will be used to train.

trainingData.dataGenerator() ⇒ generator

Returns a generator which iterators over the dataset which was used to construct the TrainingData object.

Kind: instance method of TrainingData

trainingData.dataLength() ⇒ number

Gives the number of individual trials in the associated data set.

Kind: instance method of TrainingData

neural~transferFunctions : object

The store of transfer functions which can be used in Neurons to convert input sums to activation values. By default, the following are available:

Kind: inner namespace of neural
Properties

Name Type Description
logSigmoid function see https://en.wikipedia.org/wiki/Logistic_function
rectifier function see https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
linear function useful for output layers for training data which could take any value. Note that neural networks cannot rely solely on linear transfer functions otherwise hidden layers will be effectively redundant.

neural~addTransferFunction(key, fn, deriv)

Adds a function to the store of transferFunctions which can be applied to Neurons to convert the input sum into an activation value.

Kind: inner method of neural

Param Type Description
key string transfer Function name
fn one-to-one The transfer function itself, which should take a number and output a number. It should be differentiable if it's to be used in network training.
deriv one-to-one The derivative of the transfer function, which is required for back-propagation. It should take a number and output a number.

neural~one-to-one ⇒ number

Kind: inner typedef of neural
Returns: number - output Output value.

Param Type Description
input number Input value.