package module
Version: v0.0.0-...-3930778 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2018 License: MIT Imports: 6 Imported by: 0



Neural Networks with GO

Build Status Go Report Card API Reference codecov MIT License Release

About Package

Some time ago I decided to learn Go language and neural networks. So it's my variation of Neural Networks library. I tried to make library for programmers (not for mathematics).

For now Varis is 0.1 version.

I would be happy if someone can find errors and give advices. Thank you. Artem.

Main features

  • All neurons and synapses are goroutines.
  • Golang channels for connecting neurons.
  • No dependencies


go get


package main

import (

func main() {
	net := varis.CreatePerceptron(2, 3, 1)

	dataset := varis.Dataset{
		{varis.Vector{0.0, 0.0}, varis.Vector{1.0}},
		{varis.Vector{1.0, 0.0}, varis.Vector{0.0}},
		{varis.Vector{0.0, 1.0}, varis.Vector{0.0}},
		{varis.Vector{1.0, 1.0}, varis.Vector{1.0}},

	trainer := varis.PerceptronTrainer{
		Network: &net,
		Dataset: dataset,

	varis.PrintCalculation = true

	net.Calculate(varis.Vector{0.0, 0.0}) // Output: [0.9816677167418877]
	net.Calculate(varis.Vector{1.0, 0.0}) // Output: [0.02076530509106318]
	net.Calculate(varis.Vector{0.0, 1.0}) // Output: [0.018253250887023762]
	net.Calculate(varis.Vector{1.0, 1.0}) // Output: [0.9847884089930481]

Roadmap 0.2-0.5

  • Add locks
  • Add training channels
  • Improve speed
  • Add error return to functions.
  • Create more tests and benchmarks.
  • Create server and cli realization for use Varis as a application


gonn | go-mind | go-perceptron-go



PrintCalculation = false
n := CreatePerceptron(2, 3, 1)

dataset := Dataset{
	{Vector{0.0, 0.0}, Vector{1.0}},
	{Vector{1.0, 0.0}, Vector{0.0}},
	{Vector{0.0, 1.0}, Vector{0.0}},
	{Vector{1.0, 1.0}, Vector{1.0}},

trainer := PerceptronTrainer{&n, dataset}

PrintCalculation = true

n.Calculate(Vector{0.0, 0.0})
n.Calculate(Vector{1.0, 0.0})
n.Calculate(Vector{0.0, 1.0})
n.Calculate(Vector{1.0, 1.0})

Input: [0 0] Output: [0.9816677167418877]
Input: [1 0] Output: [0.020765305091063144]
Input: [0 1] Output: [0.01825325088702373]
Input: [1 1] Output: [0.9847884089930483]




This section is empty.


View Source
var ACTIVATION neuronFunction = func(x float64) float64 {
	return 1 / (1 + math.Exp(-x))

ACTIVATION store default activation function.

View Source
var DEACTIVATION neuronFunction = func(x float64) float64 {
	var fx = ACTIVATION(x)
	return fx * (1 - fx)

DEACTIVATION store default deactivation function.

View Source
var PrintCalculation = false

PrintCalculation logs all calculate calls (print input and output).


func ConnectNeurons

func ConnectNeurons(in Neuron, out Neuron, weight float64)

ConnectNeurons connect two neurons. It creates synapse and add connection to input and output Neuron.

func ToJSON

func ToJSON(network Perceptron) string

ToJSON dump and transform Perceptron to json string.


type CoreNeuron

type CoreNeuron struct {
	// contains filtered or unexported fields

CoreNeuron - entity with float64 weight (it is bias) and connection. Activation result store in cache for training.

type Dataset

type Dataset [][2]Vector

Dataset - simple type for store input and expected Vectors.

type Neuron

type Neuron interface {
	// contains filtered or unexported methods

Neuron - interface for all Neuron. Each Neuron must have: - coreNeuron is a basic neuron for all types - getCore() is a the function for getting pointer to CoreNeuron - live() - method for running neuron's goroutine. All kind of Neurons implement functionality live - changeWeight is the method for training

func HNeuron

func HNeuron(weight float64) Neuron

HNeuron - creates hiddenNeuron.

func INeuron

func INeuron(weight float64, connectTo chan float64) Neuron

INeuron - creates inputNeuron.

func ONeuron

func ONeuron(weight float64, connectTo chan float64) Neuron

ONeuron - creates outputNeuron.

type Perceptron

type Perceptron struct {
	// contains filtered or unexported fields

Perceptron implement Neural Network Perceptron by collect layers with Neurons and input/output channels.

func CreatePerceptron

func CreatePerceptron(layers Perceptron

CreatePerceptron make new Perceptron NN with count of neurons in each Layer.

func FromJSON

func FromJSON(jsonString string) Perceptron

FromJSON load json string and create Perceptron.

func (*Perceptron) Calculate

func (n *Perceptron) Calculate(input Vector) Vector

Calculate run Network calculations by broadcasting signals to input channels and wait signals from output array of chan.

func (*Perceptron) ConnectLayers

func (n *Perceptron) ConnectLayers()

ConnectLayers create all to all connection between layers.

func (*Perceptron) RunNeurons

func (n *Perceptron) RunNeurons()

RunNeurons create goroutines for all Neuron in Perceptron.

type PerceptronTrainer

type PerceptronTrainer struct {
	Network *Perceptron
	Dataset Dataset

PerceptronTrainer is a trainer for Perceptron networks

func (*PerceptronTrainer) BackPropagation

func (t *PerceptronTrainer) BackPropagation(times int)

BackPropagation train Network input Dataset for 'times' times.

type Vector

type Vector []float64

Vector implement array of float64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL