Introducing GoMind: A Simplistic Neural Network Library in Go

3 min read

Neural Network

Go

What is GoMind?

GoMind is a simplistic Neural Network library written entirely in Go (GoLang).

GoMind is built upon learnings from my previous article where I had explained Neural Networks and back propagation algorithms in detail.

Soon after its release, GoMind became a proud member of Avelino's curated list of awesome go projects.

Why bother?

I started learning Neural Network as an intellectual exercise and like almost every learning exercise it is best to set an end goal. In my case I decided on a two-dimensional self driving car simulation project, which I, in my magnanimosity christened, Vaahan.

Over the course of the project I was forced to write my own computational geometry library, which I started calling GoGeo, for the of lack of any reliable library in Go like Python's SymPy. Hopefully some day GoGeo will also get its own repositiory but it has a long journey ahead of it before that happens.

Similarly, after a while a need arose for a neural network library which gelled seemlesly with Vaahan and was also simplistic enough for quick training exercises. But I would be lying if I said that I only wrote GoMind because of a lack of good open source libraries in Go. No, the reason for starting GoMind is again educational. Afterall, what better way to understand neural networks than creating them yourselves?

Why Go?

I do realise that I can bypass many of my challenges if I moved away from Go but recently Go has been my language of choice for most of my personal projects because of its gentle learning curve, educated opinionatedness, built-in testing framework alongwith its beautiful set of tools like gofmt, fix etc. There is no dearth of material on internet listing Go's many advantages.

Combining Go with docker also creates a pretty smooth development pipeline.

Salient Features

Even though still in its infancy, GoMind attempts to provide a few good-to-have features for a neural network library like it:

Code specs

Every new push to the library is tested for Go versions 1.7 to 1.10 using Travis CI.

As of release v0.2-alpha GoMind has over 83% test coverage (find latest coverage report here).

GoMind also scores an impressive A+ in code quality on goreportcard.com (checkout latest score here).

Usage

Installation

GoMind can be installed using Go's go get command:

go get github.com/surenderthakran/gomind

Example

The following example teaches the XOR gate to GoMind's neural network.

package main

import (
	"github.com/singhsurender/gomind"
)

func main() {
	// XOR gate training set.
	trainingSet := [][][]float64{
		[][]float64{[]float64{0, 0}, []float64{0}},
		[][]float64{[]float64{0, 1}, []float64{1}},
		[][]float64{[]float64{1, 0}, []float64{1}},
		[][]float64{[]float64{1, 1}, []float64{0}},
	}

	// create neural network model.
	mind, err := gomind.New(&gomind.ModelConfiguration{
		NumberOfInputs:                    2,
		NumberOfOutputs:                   1,
		NumberOfHiddenLayerNeurons:        16,
		HiddenLayerActivationFunctionName: "relu",
		OutputLayerActivationFunctionName: "sigmoid",
	})
	if err != nil {
		return nil, fmt.Errorf("unable to create neural network. %v", err)
	}

	// iterate 500 times on random XOR gate transaction samples.
	for i := 0; i < 500; i++ {
		rand := rand.Intn(4)
		input := trainingSet[rand][0]
		output := trainingSet[rand][1]

		// learn from random sample.
		if err := mind.LearnSample(input, output); err != nil {
			mind.Describe(true)
			return nil, fmt.Errorf("error while learning from sample input: %v, target: %v. %v", input, output, err)
		}

		// output guessed by the neural network.
		actual := mind.LastOutput()

		// error in network's guess.
		outputError, err := mind.CalculateError(output)
		if err != nil {
			mind.Describe(true)
			return nil, fmt.Errorf("error while calculating error for input: %v, target: %v and actual: %v. %v", input, output, actual, err)
		}

		// accuracy in network's guess.
		outputAccuracy, err := mind.CalculateAccuracy(output)
		if err != nil {
			mind.Describe(true)
			return nil, fmt.Errorf("error while calculating error for input: %v, target: %v and actual: %v. %v", input, output, actual, err)
		}

		fmt.Println("Index: %v, Input: %v, Target: %v, Actual: %v, Error: %v, Accuracy: %v\n", i, input, output, actual, outputError, outputAccuracy)
	}
}

Roadmap

Following features are planned for future GoMind releases:

Confession

While GoMind is nowhere close to machine learning giants like Tensorflow or scikit-learn, it serves its purpose as a simplistic library for quick training in Go without worrying about C bindings or pre-creating models in Python.