Build Your Own Neural Network in Go

栏目: IT技术 · 发布时间: 5年前

内容简介:A beginner’s guide to building a simple neural network completely from scratch in Go languageIn this tutorial, we’ll build a simple neural network (single-layerPerceptrons — invented by

A beginner’s guide to building a simple neural network completely from scratch in Go language

Apr 13 ·5min read

Introduction

In this tutorial, we’ll build a simple neural network (single-layer perceptron ) in Golang, completely from scratch. We’ll also train it on sample data and perform predictions. Creating your own neural network from scratch will help you better understand what’s happening inside a neural network and the working of learning algorithms.

What’s a Perceptron?

Perceptrons — invented by Frank Rosenblatt in 1958, are the simplest neural network that consists of n number of inputs, only one neuron and one output , where n is the number of features of our dataset.

Hence, our single-layer perceptron consists of the following components

  1. An input layer (x)
  2. An output layer (ŷ)
  3. A set of weights (w) and a bias (b) between these two layers
  4. An activation function (σ) for the output layer. In this tutorial, we’ll be using the sigmoid activation function.

Our neural network is called a single-layer perceptron (SLP) as the neural network has only one layer of neurons. Neural networks with more than one layer of neurons are called multi-layer perceptron (MLP) .

(note epoch refers to one cycle through the full training dataset)

Before we start

We’ll build our own functions for the following math operations — vector addition , vector dot product & scalar matrix multiplication.

Initially, the weights of the neural network are set to random float values between 0 and 1 while the bias is set to zero.

Forward Propagation

The process of passing the data through the neural network is known as forward-propagation or forward pass. The output the perceptron is

In a nutshell, the dot product of the weight vector (w) and the input vector (x) is added with the bias ( b) and the sum is passed through an activation function. The output of the sigmoid activation function will be from 0 and 1.

The Learning Algorithm

The learning algorithm consists of two parts — Backpropagation and Optimization.

Backpropagation, short for backward propagation of errors , refers to the algorithm for computing the gradient of the loss function with respect to the weights. However, the term is often used to refer to the entire learning algorithm.

A loss function is used to get an estimation of how far are we from our desired solution. Generally, mean squared error is chosen as the loss function for regression problems and cross-entropy for classification problems. To keep it simple, we’ll use mean squared error as our loss function. Also, we will not be calculating the MSE but directly calculate its gradient.

Build Your Own Neural Network in Go

The gradient of the loss function is calculated using the chain rule . The gradients of the loss function with respect to the weights and bias are calculated as follows.

Build Your Own Neural Network in Go

(for the derivation of these expressions, check my article in which I have briefly explained the math concepts behind neural networks )

Build Your Own Neural Network in Go

Image source

Optimizationis the selection of best weights and bias of the perceptron to get the desired results. Let’s choose gradient descent as our optimization algorithm. The weights and the bias are updated as follows till convergence .

Build Your Own Neural Network in Go

Learning rate ( α ) is a hyperparameter which is used to control how much the weights and bias are changed. However, we will not be using the l earning rate in this tutorial.

Assembling the Pieces

Now let’s train and make predictions out of our neural network on the following data. The data has three inputs and only one output belonging to two classes(0 and 1). Hence the data can be trained on our single-layer perceptron.

Build Your Own Neural Network in Go

As you can see, the output Y is only dependent on the input X1 . Now we will train our neural network on the above data and check how it performs after 1000 epochs . To make predictions, we have to just do a forward propagation with the test inputs.

Build Your Own Neural Network in Go

As we compare the predicted values with the actual values, we can see that our trained single-layer perceptron has performed well. We’ve successfully created a neural network and trained it to produce desirable results.

What’s Next?

Now you’ve created your own neural network completely from scratch. Here are a few things you shall try next.

  • Test on your own data
  • Try other activation function besides the sigmoid function
  • Calculate MSE after each epoch
  • Try other error function besides the MSE
  • Try creating a multi-layer perceptron

Final Thoughts

I hope that you’ve learned a lot from creating your own neural network in Golang. I’ll be writing more on topics related to machine learning & deep learning and I’ll be hopefully covering multi-layer perceptron in my next article.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++程序设计原理与实践

C++程序设计原理与实践

(美)Bjarne Stroustrup / 王刚 等 / 机械工业出版社 / 2010.7 / 108.00元

本书是经典程序设计思想与C++开发实践的完美结合,是C++之父回归校园后对C++编程原理和技巧的全新阐述。书中全面地介绍了程序设计基本原理,包括基本概念、设计和编程技术、语言特性以及标准库等,教你学会如何编写具有输入、输出、计算以及简单图形显示等功能的程序。此外,本书通过对C++思想和历史的讨论、对经典实例(如矩阵运算、文本处理、测试以及嵌入式系统程序设计)的展示,以及对C语言的简单描述,为你呈现......一起来看看 《C++程序设计原理与实践》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试