alec

package module
v0.0.0-...-43aa20b Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2017 License: MIT Imports: 4 Imported by: 0

README

alec

Neural Network implementation in Go - Documentation: https://godoc.org/github.com/s4ayub/alec

Build Status

Motivation and Features

This neural network currently allows users to specify a learning rate, the number of iterations for training and the number of units in the hidden layer of the network. Training is faciliated using a backpropagation algorithm. It was made for the purpose of learning basic machine learning concepts under supervised learning. Using Go allowed me to gain more experience with the language before delving into more research intensive projects using it.

Train and Predict

import (
	"fmt"
	"github.com/s4ayub/alec"
}

func main() {
	// An alec instance with a learning rate of 0.2, 1,000,000 iterations and 10 units in hidden layer
	a := alec.Build(0.2, 1000000, 10) 

	a.Train([][][]float64{ // XOR truth table
		{{0, 0}, {0}},
		{{0, 1}, {1}},
		{{1, 0}, {1}},
		{{1, 1}, {0}},
	})

	input := [][]float64{{0, 0}}
	guess := a.Predict(input)
	fmt.Println("XOR TEST: ", guess.At(0, 0))
}

Correctness

A 65.2% soft correctness was determined for the network when approximating sin(x). This test can be found within the comments of "alec_test.go". Improvements could be made with the amount of training data provided. Also, only the sigmoid activation function is used which may not be the best for training for this purpose. For example, when determing correctness with XOR truth table predictions, the correctness would be 95%+ yet, this is not an accurate representation of the true correctness of the network.

Improvements

  • Allow for different activation functions
  • Refactor to support multiple hidden layers
  • Implement custom error threshold to facilitate more custom training

References

Reading Material:

The following repositories were referenced as well:

Documentation

Overview

Basic neural network

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Randoms

func Randoms(rows, columns int) *mat64.Dense

Helper to randomize weights of synapses

func Scrape

func Scrape(trainingData [][][]float64) (*mat64.Dense, *mat64.Dense)

Helper to grab data from the 3D array of training data

func Sigmoid

func Sigmoid(value float64) float64

Helper function that applies the sigmoid function to a value

func SigmoidActivate

func SigmoidActivate(m *mat64.Dense) *mat64.Dense

Activation function -> currently only uses sigmoid function

func SigmoidPrime

func SigmoidPrime(value float64) float64

Helper function that applies the derivative of the sigmiod function to a value

func SigmoidPrimeActivate

func SigmoidPrimeActivate(m *mat64.Dense) *mat64.Dense

Types

type Alec

type Alec struct {
	LearningRate        float64
	NumOfIterations     int
	HiddenNeurons       int
	InputSynapses       *mat64.Dense // Connections between neurons
	OutputSynapses      *mat64.Dense // Connections between neurons
	HiddenNeuronSum     *mat64.Dense // Sums and results coming in and out of each neuron
	HiddenNeuronResults *mat64.Dense
	NeuronOutputSum     *mat64.Dense
	NeuronOutputResults *mat64.Dense
}

func Build

func Build(learningRate float64, iterations int, hiddenNeurons int) *Alec

Initialize the network

func (*Alec) BackPropagate

func (a *Alec) BackPropagate(inputMatrix *mat64.Dense, outputMatrix *mat64.Dense)

The following process is based off a formula used to determine the changes in weights required at each layer. These changes in weights are then added to the original weights to calibrate the network. The formula deals with the derivative of the activation function and applying it to the changes.

func (*Alec) ForwardPropagate

func (a *Alec) ForwardPropagate(inputMatrix *mat64.Dense)

Helper function to use forward propagation on the network

func (*Alec) Predict

func (a *Alec) Predict(inputData [][]float64) *mat64.Dense

Ask a smart alec! This function allows you to make a prediction

func (*Alec) Train

func (a *Alec) Train(trainingData [][][]float64)

Jump to

Keyboard shortcuts

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