golang
April 30, 2023

🧊 Cirno Neural Network

In this article, I want to show how we can create a small neural network using Golang that can detect anomalies in datasets. We will not set too strict tasks for it and agree to name it after Cirno, who was not known for her intelligence.

⑨, pronounced "Nineball," is a running joke among Touhou fans involving Cirno, originating from a description in the instruction manual of Phantasmagoria of Flower View, which labeled her, on a list of nine things, as "⑨: Idiot." Cirno is typically seen with various versions of the number 9 in a circle because of this. Her name may also be used as a replacement for "idiot" or "baka" in some cases. [Source]

We can observe how neural networks have been actively used in various projects for the analysis and clustering of large volumes of data lately. Is it possible to create a small neural network that can be embedded into our own project?

Configuration

First, we need to choose a toolkit. I will use the gobrain library to create a simple neural network. This library is well-documented and has a simple and intuitive functionality, which can be used to create truly amazing things.

I have also chosen the type of neural network that we will use in our project. Since we know the volume of incoming data in advance, we can use a type of network called FeedForward. The gobrain library can also create recursive neural networks, but I do not plan to use them, so you can try using them on your own.

I have also created a small file that describes the main parameters for generating the dataset, training the neural network, and the subsequent accuracy requirements when trying to test the performance of Cirno.

{
    "number of datasets": 5000,
    "number of points in dataset": 100,
    "number of hidden layers": 30,
    "number of learning epochs": 10000,
    "maximum number of anomalies": 10,
    "learning rate": 0.6,
    "scale factor": 0.4,
    "accuracy": 0.05
}

When generating a dataset that will be used for training and testing the quality of Cirno, all data groups will be normalized. Please note that normalization significantly improves the quality of the neural network's performance, so when using it in your project, remember to only provide normalized data.

Implementation

I will not list all the components of the repository below, but will only indicate those moments that, in my opinion, are the most important.

The main code is divided into three parts:

  • Reading and saving the configuration
configuration, err := configuration.New()
if err != nil {
	...
}
  • Creating a dataset that we can use when training the neural network
dataset, err := dataset.New(configuration)
if err != nil {
	...
}
  • Creating Cirno with subsequent training and quality checking
cirno, err := cirno.New(configuration)
if err != nil {
	...
}

accuracy, err := cirno.Train(dataset)
if err != nil {
	...
}
fmt.Println("Accuracy:", accuracy)

accuracyOnNewDataset, err := cirno.Verify()
if err != nil {
	...
}

fmt.Println("Accuracy on the new dataset:", accuracyOnNewDataset)

Thus, when launching the neural network, we can get the following response

Accuracy: 0.08
Accuracy on the new dataset: 1.16

using the following command

make run

However, remember that training a neural network is a random process, so each time you run it, you may see some differences in the numbers. Nevertheless, by varying the parameters listed in the configuration file, we can improve the quality of the network's performance.

In addition, we can save the obtained neural network to a file.

cirno.Save("./cirno.nya")

Repository

Below is a repository containing code with an example of using the Cirno that you can study.

🍃 GO-TO-REPO! 🍃

If you have downloaded the repository and want to see how the template works, simply enter the following command in the root directory of the repository.

make run

Conclusion

And in conclusion, I would like to note that this small package can be perfectly used to detect anomalies in fixed datasets, which I successfully do for my own needs.