Documentation ¶
Overview ¶
Package gobrain provides basic neural networks algorithms.
Index ¶
- type FeedForward
- func (nn *FeedForward) BackPropagate(targets []float64, lRate, mFactor float64) float64
- func (nn *FeedForward) Init(inputs, hiddens, outputs int)
- func (nn *FeedForward) ResetContexts()
- func (nn *FeedForward) SetContexts(nContexts int, initValues [][]float64)
- func (nn *FeedForward) Test(patterns [][][]float64)
- func (nn *FeedForward) Train(patterns [][][]float64, iterations int, lRate, mFactor float64, debug bool) []float64
- func (nn *FeedForward) Update(inputs []float64) []float64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FeedForward ¶
type FeedForward struct {
// Number of input, hidden, output nodes and contexts
NInputs, NHiddens, NOutputs, NContexts int
// Whether it is regression or not
Regression bool
// Activations for nodes
InputActivations, HiddenActivations, OutputActivations []float64
// ElmanRNN contexts
Contexts [][]float64
// Weights
InputWeights, OutputWeights [][]float64
ContextWeights [][][]float64
// Last change in weights for momentum
InputChanges, OutputChanges [][]float64
ContextChanges [][][]float64
}
FeedForwad struct is used to represent a simple neural network
Example ¶
// set the random seed to 0 rand.Seed(0) // create the XOR representation patter to train the network patterns := [][][]float64{ {{0, 0}, {0}}, {{0, 1}, {1}}, {{1, 0}, {1}}, {{1, 1}, {0}}, } // instantiate the Feed Forward ff := &FeedForward{} // initialize the Neural Network; // the networks structure will contain: // 2 inputs, 2 hidden nodes and 1 output. ff.Init(2, 2, 1) // train the network using the XOR patterns // the training will run for 1000 epochs // the learning rate is set to 0.6 and the momentum factor to 0.4 // use true in the last parameter to receive reports about the learning error ff.Train(patterns, 1000, 0.6, 0.4, false) // testing the network ff.Test(patterns) // predicting a value inputs := []float64{1, 1} ff.Update(inputs)
Output: [0 0] -> [0.057503945708445206] : [0] [0 1] -> [0.9301006350712101] : [1] [1 0] -> [0.9278099662272838] : [1] [1 1] -> [0.09740879532462123] : [0]
func (*FeedForward) BackPropagate ¶
func (nn *FeedForward) BackPropagate(targets []float64, lRate, mFactor float64) float64
The BackPropagate method is used, when training the Neural Network, to back propagate the errors from network activation.
func (*FeedForward) Init ¶
func (nn *FeedForward) Init(inputs, hiddens, outputs int)
Initialize the neural network;
the 'inputs' value is the number of inputs the network will have, the 'hiddens' value is the number of hidden nodes and the 'outputs' value is the number of the outputs of the network.
func (*FeedForward) ResetContexts ¶
func (nn *FeedForward) ResetContexts()
Reset the context values.
Useful to remove noise from previous context when the network is given the start of a new sequence. This does not affect the context weights.
func (*FeedForward) SetContexts ¶
func (nn *FeedForward) SetContexts(nContexts int, initValues [][]float64)
Set the number of contexts to add to the network.
By default the network do not have any context so it is a simple Feed Forward network, when contexts are added the network behaves like an Elman's SRN (Simple Recurrent Network).
The first parameter (nContexts) is used to indicate the number of contexts to be used, the second parameter (initValues) can be used to create custom initialized contexts.
If 'initValues' is set, the first parameter 'nContexts' is ignored and the contexts provided in 'initValues' are used.
When using 'initValues' note that contexts must have the same size of hidden nodes + 1 (bias node).
func (*FeedForward) Test ¶
func (nn *FeedForward) Test(patterns [][][]float64)
func (*FeedForward) Train ¶
func (nn *FeedForward) Train(patterns [][][]float64, iterations int, lRate, mFactor float64, debug bool) []float64
This method is used to train the Network, it will run the training operation for 'iterations' times and return the computed errors when training.
func (*FeedForward) Update ¶
func (nn *FeedForward) Update(inputs []float64) []float64
The Update method is used to activate the Neural Network.
Given an array of inputs, it returns an array, of length equivalent of number of outputs, with values ranging from 0 to 1.