nnGo

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2020 License: MIT Imports: 4 Imported by: 0

README

Toy Neural Network

Neural Network library with no dependencies, has it's own Matrix implementation and a 3 layered (input, hidden, output) Dense Network.


Pre - Requisites

Make sure you have GO Installed.


Testing The Library

  1. git clone https://github.com/sarthakpranesh/nnGo.git
  2. cd nnGo
  3. That's it hurray! You can checkout the examples subdirectory as a playground to test the library out!!!

About library

The nnGo folder in this repo holds two files named, Matrix.go and NN.go. Matrix.go contains all required components that should be their to generate and perform operations on Matrices, where as the NN.go file contains code that utilizes the Matrix implementation to provide a 3 layered fully connected Dense Neural Network.


Looking for help

Currently I only have two example ( XOR , MNIST ) implemented using this library and only two activation function supported. I would love if anyone of you can help me by implementing more examples or define different activation functions in this Toy library. Also looking for improving the existing code quality.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatrixAve

func MatrixAve(m Matrix) float64

MatrixAve takes in a Matrix and returns the average of type float64 of all the elements in that matrix

func MatrixSum

func MatrixSum(m Matrix) float64

MatrixSum takes in a Matrix and return the sum of all elements of that Matrix

Types

type Activation

type Activation struct {
	F  func(x float64) float64
	DF func(y float64) float64
}

Activation is a simple struct that holds Activation functions for forward progations (F) and back propagation (DF)

func NewSigmoid

func NewSigmoid() *Activation

NewSigmoid returns a pointer to an Activation, set to use Sigmoid

func NewTanh

func NewTanh() *Activation

NewTanh returns a pointer to an Activation, set to use Tanh

type Matrix

type Matrix struct {
	Rows int
	Cols int
	Val  [][]float64
}

Matrix is a struct that represents a mathematical Matrix

func MatrixAdd

func MatrixAdd(m Matrix, m2 Matrix) (Matrix, error)

MatrixAdd takes in two Matrix and returns their element wise addition

func MatrixMap

func MatrixMap(m Matrix, f func(x float64) float64) Matrix

MatrixMap takes in a Matrix and a function, returns a Matrix created by applying the function to all elements of the Matrix passed in as argument

func MatrixMul

func MatrixMul(m Matrix, m2 Matrix) (Matrix, error)

MatrixMul takes in two Matrix and returns their element wise Multiplication

func MatrixProduct

func MatrixProduct(m Matrix, m2 Matrix) (Matrix, error)

MatrixProduct takes in two Matrix and returns their Matrix product (Cross product)

func MatrixSub

func MatrixSub(m Matrix, m2 Matrix) (Matrix, error)

MatrixSub takes in two Matrix and returns their element wise subtraction

func MatrixTranspose

func MatrixTranspose(m Matrix) Matrix

MatrixTranspose takes in a Matrix and returns it's transpose Matrix

func NewColMatrix

func NewColMatrix(arr []float64) Matrix

NewColMatrix takes in a 1D array of float64 numbers and returns a Matrix (mathematically a vector)

func NewMatrix

func NewMatrix(arr [][]float64) Matrix

NewMatrix takes in an 2D array of float64 numbers and returns a Matrix

func NewRandom

func NewRandom(Rows, Cols int) Matrix

NewRandom takes in number of rows and column of type int and returns a Matrix of randomly filled values

func NewZeros

func NewZeros(Rows, Cols int) Matrix

NewZeros takes in number of rows and columns of type int and returns a Matrix of Val zero

func (*Matrix) Add

func (m *Matrix) Add(a float64)

Add takes in a float64 number and adds it to all elements of the Matrix

func (*Matrix) AddMat

func (m *Matrix) AddMat(m2 Matrix) error

AddMat takes in another Matrix as argument and performs element wise addition operation

func (*Matrix) Map

func (m *Matrix) Map(mapFunc func(x float64) float64)

Map takes in a function of type func(x float64) float64 and applies this function to all elements of the matrix

func (*Matrix) Mul

func (m *Matrix) Mul(a float64)

Mul takes in a float64 number and Multiplies it to all elements of the Matrix

func (*Matrix) MulMat

func (m *Matrix) MulMat(m2 Matrix) error

MulMat takes in another Matrix as argument and performs element wise multiplication operation

func (*Matrix) RandomFill

func (m *Matrix) RandomFill()

RandomFill fills the Matrix with random values

func (*Matrix) Show

func (m *Matrix) Show()

Show is used to visualize Matrix

func (*Matrix) Sub

func (m *Matrix) Sub(a float64)

Sub takes in a float64 number and substracts it to all elements of the Matrix

func (*Matrix) SubMat

func (m *Matrix) SubMat(m2 Matrix) error

SubMat takes in another Matrix as argument and performs element wise substraction operation

func (*Matrix) Transpose

func (m *Matrix) Transpose()

Transpose performs the mathamatical transpose operation on the Matrix

type Model

type Model interface {
	Train(input [][]float64, t [][]float64)
	Predict(input []float64)
}

Model is a simple interface having two methods Train and Predict

type NeuralNetwork

type NeuralNetwork struct {
	NumInputNodes  int // Num of input nodes on the Neural Network
	NumOutputNodes int // Num of output nodes on the Neural Network
	NumHiddenNodes int // Num of hidden nodes on the Neural Network

	WeightsIH Matrix // weights from input to hidden layer
	WeightsHO Matrix // weights from hidden to output layer

	BiasIH Matrix
	BiasHO Matrix

	LearningRate   float64     // Rate at which the Network would learn/fit data
	ActivationFunc *Activation // Pointer to an Activation function

	Epochs int // num of Epochs to loop
}

NeuralNetwork is a struct that contains all the values necessary to train a Neural Network model

func Mutate

func Mutate(n NeuralNetwork, mapping func(x float64) float64) NeuralNetwork

Mutate takes in a NeuralNetwork and a function of type func (x float64) float64, and returns the mutation of the NeuralNetwork by applying the function to all Matrix in the NeuralNetwork

func NewNN

func NewNN(inputNodes, hiddenNodes, outputNodes int, LearningRate float64, ActivationFunc string, e int) *NeuralNetwork

NewNN creates and returns a pointers to a NeuralNetwork

Example
nn = NewNN(2, 10, 1, 1, "sgd", 1000)
fmt.Println(nn.LearningRate)
Output:

1

func (*NeuralNetwork) Predict

func (n *NeuralNetwork) Predict(input []float64) [][]float64

Predict takes 1D array of input data and returns a One-Hot encoded 2D array

Example

Predicting Output for XOR problem

nn.Predict([]float64{1, 0})
Output:

func (*NeuralNetwork) Train

func (n *NeuralNetwork) Train(input [][]float64, t [][]float64)

Train takes in two 2D arrays of type float64 and trains the NeuralNetwork to fit the data. One arguments should be the input data to the Neural Network and another one should be One Hot encoding of the actual category of the correspoding input data.

Example

Training model to learn XOR problem

var input [][]float64 = [][]float64{{1, 1}, {0, 1}, {0, 0}, {1, 0}}
var target [][]float64 = [][]float64{{0}, {1}, {0}, {1}}
nn.Train(input, target)
Output:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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