gobrain

package module
v0.0.0-...-52144c8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2017 License: MIT Imports: 4 Imported by: 7

README

gobrain

Neural Networks written in go

GoDoc Build Status

Getting Started

The version 1.0.0 includes just basic Neural Network functions such as Feed Forward and Elman Recurrent Neural Network. A simple Feed Forward Neural Network can be constructed and trained as follows:

// 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 := &gobrain.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, true)

After running this code the network will be trained and ready to be used.

The network can be tested running using the Test method, for instance:

ff.Test(patterns)

The test operation will print in the console something like:

[0 0] -> [0.057503945708445]  :  [0]
[0 1] -> [0.930100635071210]  :  [1]
[1 0] -> [0.927809966227284]  :  [1]
[1 1] -> [0.097408795324620]  :  [0]

Where the first values are the inputs, the values after the arrow -> are the output values from the network and the values after : are the expected outputs.

The method Update can be used to predict the output given an input, for example:

inputs := []float64{1, 1}
ff.Update(inputs)

the output will be a vector with values ranging from 0 to 1.

Recurrent Neural Network

This library implements Elman's Simple Recurrent Network.

To take advantage of this, one can use the SetContexts function.

ff.SetContexts(1, nil)

In the example above, a single context will be created initilized with 0.5. It is also possible to create custom initilized contexts, for instance:

contexts := [][]float64{
	{0.5, 0.8, 0.1}
}

Note that custom contexts must have the same size of hidden nodes + 1 (bias node), in the example above the size of hidden nodes is 2, thus the context has 3 values.

Changelog

  • 1.0.0 - Added Feed Forward Neural Network with contexts from Elman RNN

Documentation

Overview

Package gobrain provides basic neural networks algorithms.

Index

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 and output nodes
	NInputs, NHiddens, NOutputs int
	// Whether it is regression or not
	Regression bool
	// Activations for nodes
	InputActivations, HiddenActivations, OutputActivations []float64
	// ElmanRNN contexts
	Contexts [][]float64
	// Weights
	InputWeights, OutputWeights [][]float64
	// Last change in weights for momentum
	InputChanges, OutputChanges [][]float64
}

FeedForwad struct is used to represent a simple neural network

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) 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL