Building a neural network from scratch in Go — Data Dan – AI Classroom

I’m super pumped that my new book Machine Learning with Go is now available! Writing the book allowed me to get a complete view of the current state of machine learning in Go, and let’s just say that I’m pretty excited to see how the community growing!

In the book (and for my own edification), I decided that I would build a neural network from scratch in Go. Turns out, this is fairly easy, and I thought it would be great to share my little neural net here.

All the code and data shown below is available on GitHub.

(If you are interested in leveraging pre-existing Go packaging for machine learning, check out all the great existing packages, and be sure to watch Chris Benson’s recent talk at GolangUK about Deep Learning in Go)

Goals

There are a whole variety of ways to accomplish this task of building a neural net in Go, but I wanted to adhere to the following guidelines:

  • No cgo – I want my little neural net to compile nicely to a statically linked binary, and I also want to highlight the numerical functionality that is available natively in Go.

  • gonum matrix input – I want supply matrices to my neural network for training, similar to how you would supply numpy arrays to most Python machine learning functions.

  • Variable numbers of nodes – Although I will only illustrate one architecture here, I wanted my code to be flexible, such that I could tweak the numbers of nodes in each layer for other scenarios.

Network Architecture

The basic network architecture that we will utilize in this example includes an input layer, a single hidden layer, and an output layer: