goceptron

package module
v0.0.0-...-18192e9 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2017 License: GPL-3.0 Imports: 6 Imported by: 1

README

Perceptron library in Golang

Build Status Go Report Card GoDoc

A library implementing a multi-layer perceptron in Golang

Quick How-to-Use

Creation of the network

3 parameters are necessary:

  • size of the input layer (int)
  • sizes of the hidden layers (slice of int)
  • size of the output layer (int)
var (
    p                 gct.Percetron
    inputLayerSize    int
    hiddenLayersSizes []int
    outputLayerSize   int
)
inputLayerSize = 784
hiddenLayersSizes = []int{100}
outputLayerSize = 10
p.Init(inputLayersize, hiddenLayersSizes, outputLayersize)
Learning

For the forward propagation, you can either implement your own neuron activation function, using p.ComputeFromInputCustom, or use p.ComputeFromInput to use a sigmoid function. To make the backpropagation, you give as parameters, the expected values of each output neuron, as well as the learning rate eta.

var (
    expected   []float64
    eta        float64
    activation func(input float64) float64
    mse        float64
)
activation = func(input float64) float64 {
    return 1 / (1 + math.Exp(-input))
}
expected = make([]float64, 10)
eta = 0.3

// Init input layer here
// Modify expected values

p.ComputeFromInputCustom(activation)
mse = p.Backpropagation(expected, eta)
Testing the neural network

To test the neural network and get the recognition rate, use TryRecognitionCustom (to be able to use your own activation function) or TryRecognition (to use a sigmoid). The return value is the rate of recognition (between 0 and 1)

var (
    rate float64
)
rate = p.TryRecognitionCustom(activation)

Full example, using the MNIST dataset, is available here.

More documentation here.

TODO

  • More GoDoc

Documentation

Overview

Package goceptron is a more-or-less complete package to manage a perceptron

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Layer

type Layer struct {
	Position int
	Size     int
	Neurons  []Neuron
	Biases   []float64
}

Layer struct, contains the Position of the layer in the perceptron, the number of Neurons in it, and the list of the Neurons in the layer

func (Layer) Println

func (l Layer) Println()

Println prints neuron Values in layer

type Neuron

type Neuron struct {
	Value   float64
	Weights []float64
}

Neuron struct, contains the neuron Value, and the Weights coming out frmo this neuron

type Perceptron

type Perceptron struct {
	LayerNb int
	Layers  []Layer
}

Perceptron struct, contains the number of Layers, and the list of Layers in the perceptron

func (*Perceptron) AddLayer

func (p *Perceptron) AddLayer(Size int)

AddLayer adds a layer containing <Size> Neurons to the Perceptron

func (*Perceptron) Backpropagation

func (p *Perceptron) Backpropagation(expected []float64, eta float64) (outputError float64)

Backpropagation makes the perceptron learn by modifying the Weights on all Neurons

func (*Perceptron) BackpropagationCustom

func (p *Perceptron) BackpropagationCustom(expected []float64, eta float64, fn func(float64) float64) (outputError float64)

BackpropagationCustom makes the perceptron learn by modifying the Weights on all Neurons The derivative of the activation function is given as parameter

func (*Perceptron) CalculateLayer

func (p *Perceptron) CalculateLayer(layerPos int)

CalculateLayer calculates the new neuron Values of the layer which have the Position <layerPos> in the perceptron The activation function is a sigmoid

func (*Perceptron) CalculateLayerCustom

func (p *Perceptron) CalculateLayerCustom(layerPos int, fn func(float64) float64)

CalculateLayerCustom calculates the new neuron Values of the layer which have the Position <layerPos> in the perceptron The activation function is given as a parameter

func (*Perceptron) ComputeFromInput

func (p *Perceptron) ComputeFromInput()

ComputeFromInput computes new neuron Values, except for the first layer The activation function is a sigmoid

func (*Perceptron) ComputeFromInputCustom

func (p *Perceptron) ComputeFromInputCustom(fn func(float64) float64)

ComputeFromInputCustom computes new neuron Values, except for the first layer The activation function is given as a parameter

func (*Perceptron) Init

func (p *Perceptron) Init(inputLayersize int, hiddenLayersSizes []int, outputLayersize int)

Init initializes the perceptron

func (*Perceptron) LoadFromFile

func (p *Perceptron) LoadFromFile(path string) (err error)

LoadFromFile loads the perceptron from a given file

func (Perceptron) SaveToFile

func (p Perceptron) SaveToFile(path string) (err error)

SaveToFile saves the perceptron to a given file

func (Perceptron) TryRecognition

func (p Perceptron) TryRecognition(expected int) (float64, bool)

TryRecognition tests the rate of recognition of the values in the input neurons The activation function is a sigmoid

func (Perceptron) TryRecognitionCustom

func (p Perceptron) TryRecognitionCustom(expected int, fn func(float64) float64) (float64, bool)

TryRecognitionCustom tests the rate of recognition of the values in the input neurons The activation function is given as a parameter

Directories

Path Synopsis
cmd
gct

Jump to

Keyboard shortcuts

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