# NNHelper is a Go package for creating and using a neural network

## Summary

This article describes how the package works helperdesigned to create and use neural networks in Go programs.

If you are already familiar with machine learning and use it in your work, then this article and the examples described in it may seem too simple for you. If you are at the beginning of your journey and want to get acquainted with this topic or would like to learn how to use the neuromatrix in your Go programs, then you have come to the right place.

Go package helper designed to quickly create a neural network and use it in applications written in the Go language. For use helper you don’t need anything else other than Go. Package helper is a package add-on gonn. And this is the only external dependency.

## Neural network (neural matrix)

Let me try to explain in my own words what a neural network or neuromatrix is, so as not to copy here complex and, perhaps, incomprehensible explanations from the first reading.

A neuromatrix is ​​an array of floating point values ​​that allows you to give the desired output values ​​depending on the input values. The values ​​known to us are fed to the input of the neuromatrix, and the results we expect are given at the output.

Let’s use the neuromatrix to solve the problem “Minus for Minus gives Plus.” To do this, we will make two tables: one with input data, the other with results.

We have two input parameters and two output parameters. The input is two numbers with a plus or minus sign. At the output we get two numbers with values ​​from 0 to 1. If the first value is close to 1, then the result is Plus, if the second value is close to 1, then the result is Minus. Those. we give real values ​​​​as input, and we get an array of results as output. We select the largest value from the array and consider this the answer.

``````Входные данные:			   		Результаты (1ый  – это Плюс, 2ой – это минус):
1, 1  – плюс * плюс   		1, 0 – плюс
1,-1  – плюс * минус  		0, 1 – минус
-1, 1  – минус* плюс   		0, 1 – минус
-1,-1  – минус* минус  		1, 0 – плюс``````

From the prepared tables with input and output data, a neuromatrix is ​​created with the help of its training. The input data is fed to the inputs of the matrix. The matrix produces an output that is compared with the result from our results table. Based on these answers, the coefficients in the matrix change. The input data is looped through hundreds, thousands and even hundreds of thousands of times until the matrix answers are accurate enough.

## Let’s move on to programming

Enough words and theory. Let’s see how it looks in practice.

Let’s create a folder for the project. We will create three files in it:

``````main.go
sam03_inp.csv
sam03_tar.csv``````

Let’s place our input and output data in *.csv files:

sam03_inp.csv

``````1,1
1,-1
-1,1
-1,-1``````

sam03_tar.csv

``````1,0
0,1
0,1
1,0``````

In the main.go file, create the main function and write the code:

``````// Константы с именами файлов наших данных и самой матрицы
const (
SAM03_NN  = "sam03.nn"
SAM03_INP = "sam03_inp.csv"
SAM03_TAR = "sam03_tar.csv"
)

// Массив строк с понятными ответами для вывода на консоль

// Создаем матрицу с 2-я входами и 2-я выходами, из csv файлов,
// если марица отсутствует на диске
if _, err := os.Stat(SAM03_NN); errors.Is(err, os.ErrNotExist) {
log.Println("Create", SAM03_NN, "neural network")
nnhelper.Create(2, 4, 2, false, SAM03_INP, SAM03_TAR, SAM03_NN, true)
}

// Загружаем матрицу из файла

// Предыдущий оператор создал матрицу и теперь мы ее используем/тестируем:
// создаем массив с данными для которых хотим получить результат и для этого,
const (
PLUS  = 1.0
MINUS = -1.0
)

// Intput array for testing
in := [][]float64{
{PLUS, PLUS},   // Plus * Plus = Plus
{PLUS, MINUS},  // Plus * Minus = Minus
{MINUS, PLUS},  // Minus * Plus = Minus
{MINUS, MINUS}, // Minus * Minus = Plus
{3000, -0.001}, // Minus * Plus = Minus
}
for i := range in {
}``````

The full text of this example and the data files are located in the folder examples/sam03

Let’s run the example:

``go run .``

And we get the results:

``````[1 1] Plus [0.9944239772210877 0.005449692189449571]
[1 -1] Minus [0.006860785779850435 0.9935960167863507]
[-1 1] Minus [0.005651009980489101 0.994384581174021]
[-1 -1] Plus [0.9944591181959666 0.005221796400203198]
[3000 -0.001] Minus [0.005445102841471242 0.9960123783099599]``````

In the results we see (see the first line):

• our initial data: [1,1]

• result translated into understandable form: Plus

• the result obtained from the outputs of the matrix: [0.9944239772210877 0.005449692189449571]

Pay attention to the last line of results. Our matrix was able to give the correct answer to the “unknown” inputs. This is the whole taste of neural networks. The matrix gives answers not only to the input data that it was trained on, but also to other input parameters unknown to it.

To see the learning process of the neural network, you need to delete the sam03.nn file and run the example.

Well, I guess I’ll end here. For a long time I dreamed of writing a clear and simple explanation of how neural networks can be used in programming. I hope I succeeded.

There are two more examples in the package:

• sam02: Input the time in 24-hour format and get the answer: Morning, Evening, Day or Night;

• sam01: An example of a matrix for getting the reaction of a game bot. The input is the amount of health, the presence of weapons, the number of enemies, and at the output we get the answer to the question “what to do”: attack, sneak, run away or do nothing.

The package is posted on Github:
https://github.com/kirill-scherba/nnhelper

Sincerely,
Kirill Scherba