Categorygithub.com/nathanleary/neural-net
modulepackage
0.0.0-20221106050509-797df6ec259b
Repository: https://github.com/nathanleary/neural-net.git
Documentation: pkg.go.dev

# README

This is an edited version of the go-deep library except it has been converted to 32-bit for better performance and some extra activation functions have been added (Elu, Mish and Swish, RootX, MulDiv and DoubleRoot)

Update: concurrency is now used more to increase performance and enabled multiple activation functions in the one network. (one activation function type per layer)

neural-net

Feed forward/backpropagation neural network implementation. Currently supports:

  • Activation functions: sigmoid, hyperbolic, ReLU, Elu, Mish, Swish, also activations I created (RootX, DivX, DoublePow, DoubleRoot and DoubleDiv).. RootX is particularly effective.
  • Double Root is looks like a combination of the sqrt function and tanh (double Div is similar except using division), they can be used to squash numbers inside your network to prevent the network from exploding... Boom!
  • I designed DoubleDiv, DoublePow & DoubleRoot to help the neural networks solve mathematical equations, usually used with the linear activation function
  • RootX (combining sqrt with relu) seems to solve problems facter than Mish and Swish... Still testing DivX (combining division with relu) but should produce similar results to RootX
  • Solvers: SGD, SGD with momentum/nesterov, Adam
  • Classification modes: regression, multi-class, multi-label, binary
  • Supports batch training in parallel
  • Bias nodes

Networks are modeled as a set of neurons connected through synapses. No GPU computations - don't use this for any large scale applications.

Install

go get -u github.com/nathanleary/neural-net

Usage

Import the go-deep package

import (
	"fmt"
	deep "github.com/nathanleary/neural-net"
	"github.com/nathanleary/neural-net/training"
)

Define some data...

var data = training.Examples{
	{[]float32{2.7810836, 2.550537003}, []float32{0}},
	{[]float32{1.465489372, 2.362125076}, []float32{0}},
	{[]float32{3.396561688, 4.400293529}, []float32{0}},
	{[]float32{1.38807019, 1.850220317}, []float32{0}},
	{[]float32{7.627531214, 2.759262235}, []float32{1}},
	{[]float32{5.332441248, 2.088626775}, []float32{1}},
	{[]float32{6.922596716, 1.77106367}, []float32{1}},
	{[]float32{8.675418651, -0.242068655}, []float32{1}},
}

Create a network with two hidden layers of size 2 and 2 respectively:

n := deep.NewNeural(&deep.Config{
	/* Input dimensionality */
	Inputs: 2,
	/* Three hidden layers consisting of two neurons each, and a single output */
	Layout: []int{2, 2, 2, 2, 1},
	/* Activation functions: Sigmoid, Tanh, ReLU, Linear, Elu, Mish, Swish, RootX, DoubleRoot */
	/*Defining the three hidden layer's Activation function*/
	Activation: []deep.ActivationType{
				deep.ActivationMulDiv,
				deep.ActivationRootX,
				deep.ActivationDoubleRoot,
				deep.ActivationMish,
			},
	/* Determines output layer activation & loss function: 
	ModeRegression: linear outputs with MSE loss
	ModeMultiClass: softmax output with Cross Entropy loss
	ModeMultiLabel: sigmoid output with Cross Entropy loss
	ModeBinary: sigmoid output with binary CE loss */
	Mode: deep.ModeBinary,
	/* Weight initializers: {deep.NewNormal(μ, σ), deep.NewUniform(μ, σ)} */
	Weight: deep.NewNormal(1.0, 0.0),
	/* Apply bias */
	Bias: true,
})

Train:

// params: learning rate, momentum, alpha decay, nesterov
optimizer := training.NewSGD(0.05, 0.1, 1e-6, true)
// params: optimizer, verbosity (print stats at every 50th iteration)
trainer := training.NewTrainer(optimizer, 50)

training, heldout := data.Split(0.5)
trainer.Train(n, training, heldout, 1000) // training, validation, iterations

resulting in:

Epochs        Elapsed       Error         
---           ---           ---           
5             12.938µs      0.36438       
10            125.691µs     0.02261       
15            177.194µs     0.00404       
...     
1000          10.703839ms   0.00000       

Finally, make some predictions:

fmt.Println(data[0].Input, "=>", n.Predict(data[0].Input))
fmt.Println(data[5].Input, "=>", n.Predict(data[5].Input))

Alternatively, batch training can be performed in parallell:

optimizer := NewAdam(0.001, 0.9, 0.999, 1e-8)
// params: optimizer, verbosity (print info at every n:th iteration), batch-size, number of workers
trainer := training.NewBatchTrainer(optimizer, 1, 200, 4)

training, heldout := data.Split(0.75)
trainer.Train(n, training, heldout, 1000) // training, validation, iterations

Examples

See training/trainer_test.go for a variety of toy examples of regression, multi-class classification, binary classification, etc.

See examples/ for more realistic examples:

# Packages

No description provided by the author
No description provided by the author

# Functions

ArgMax is the index of the largest element.
Dot product.
FromDump restores a Neural from a dump.
GetActivation returns the concrete activation given an ActivationType.
GetLoss returns a loss function given a LossType.
Logistic is the logistic function.
Max is the largest element.
Mean of xx.
Min is the smallest element.
NewLayer creates a new layer with n nodes.
NewNeural returns a new neural network.
NewNeuron returns a neuron with the given activation.
NewNormal returns a normal weight generator.
NewSynapse returns a synapse with the specified initialized weight.
NewUniform returns a uniform weight generator.
Normal samples a value from N(μ, σ).
Normalize scales to (0,1).
OutputActivation returns activation corresponding to prediction mode.
Round to nearest integer.
No description provided by the author
No description provided by the author
Sgn is signum.
Softmax is the softmax function.
No description provided by the author
StandardDeviation of xx.
Standardize (z-score) shifts distribution to μ=0 σ=1.
Sum is sum.
Uniform samples a value from u(mean-stdDev/2,mean+stdDev/2).
Unmarshal restores network from a JSON blob.
Variance of xx.

# Constants

ActivationCustom is a Custom activation.
ActivationMulDiv is a Custom activation.
ActivationMulDiv is a Custom activation.
ActivationMulDiv is a Custom activation.
ActivationCustom is a Custom activation.
ActivationELU is a Elu activation.
ActivationLinear is linear activation.
ActivationMish is a Mish activation.
ActivationNone is no activation.
ActivationReLU is rectified linear unit activation.
ActivationMulDiv is a Custom activation.
ActivationMulDiv is a Custom activation.
ActivationCustom is a Custom activation.
ActivationSigmoid is a sigmoid activation.
ActivationSoftmax is a softmax activation (per layer).
ActivationSwish is a Swish activation.
ActivationTanh is hyperbolic activation.
LossBinaryCrossEntropy is the special case of binary cross entropy loss.
LossCrossEntropy is cross entropy loss.
LossMeanSquared is MSE.
LossNone signifies unspecified loss.
ModeBinary is binary classification, applies sigmoid output layer.
ModeDefault is unspecified mode.
ModeMultiClass is for one-hot encoded classification, applies softmax output layer.
ModeMultiLabel is for multilabel classification, applies sigmoid output layer.
ModeRegression is regression, applies linear output layer.

# Structs

BinaryCrossEntropy is binary CE loss.
Config defines the network topology, activations, losses etc.
CrossEntropy is CE loss.
No description provided by the author
MulDiv is a logistic activator in the special case of a = 1.
MulDiv is a logistic activator in the special case of a = 1.
RootX is a logistic activator in the special case of a = 1.
DoubleRoot is a logistic activator in the special case of a = 1.
Dump is a neural network dump.
Layer is a set of neurons and corresponding activation.
Linear is a linear activator.
MeanSquared in MSE loss.
No description provided by the author
Neural is a neural network.
Neuron is a neural network node.
ReLU is a rectified linear unit activator.
RootX is a logistic activator in the special case of a = 1.
No description provided by the author
RootX is a logistic activator in the special case of a = 1.
Sigmoid is a logistic activator in the special case of a = 1.
No description provided by the author
Synapse is an edge between neurons.
Tanh is a hyperbolic activator.

# Interfaces

Differentiable is an activation function and its first order derivative, where the latter is expressed as a function of the former for efficiency.
Loss is satisfied by loss functions.

# Type aliases

ActivationType is represents a neuron activation function.
LossType represents a loss function.
Mode denotes inference mode.
A WeightInitializer returns a (random) weight.