gobrain

package module
Version: v0.0.0-...-2e2d98c Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2020 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:

package main

import (
	"github.com/goml/gobrain"
	"math/rand"
)

func main() {
	// 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.

In the example folder there are runnable examples with persistence of the trained network on file.

In example/02 the network is saved on file and in example/03 the network is loaded from file.

To run the example cd in the folder and run

go run main.go

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 initialized with 0.5. It is also possible to create custom initialized 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

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL