nn

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 5 Imported by: 0

README

Go-ML nn

Documentation

Package nn is a machine learning neural network package for Go.

Example Usage

Below contains example usage of the nn package on XOR training data. This example can be found in ../tests/xor_test.go.

Neural Network for Predicting XOR Operations
    // Create training data
	var x_train = [][]float64{{0, 0}, {0, 1}, {1, 0}, {1, 1}}
	var y_train = [][]float64{{0}, {1}, {1}, {0}}

	// Initialize neural network with two hidden layers and MSE loss
	var net nn.Network
	net.AddLayer("INPUT", "", 2)
	net.AddLayer("DENSE", "TANH", 3)
	net.AddLayer("DENSE", "SIGMOID", 3)
	net.AddLayer("DENSE", "ARCTAN", 1)
	net.SetLoss("HUBER", []float64{1.35}) // delta = 1.35

	// Train the model and display results
	net.Fit(x_train, y_train, 1000, 0.1, false)
	result := net.Predict(x_train)
	for i := range result {
		fmt.Printf("index %d: got %f, want %f\n", i, result[i][0], y_train[i][0])
		if math.Round(result[i][0]) != y_train[i][0] {
			t.Fatalf(`net.Predict() gave "%f", want "%f"`, math.Round(result[i][0]), y_train[i][0])
		}
	}
Result
    epoch 1/1000  error=0.190287
    epoch 2/1000  error=0.178683
    epoch 3/1000  error=0.167760
    ...
    epoch 998/1000  error=0.000001
    epoch 999/1000  error=0.000001
    epoch 1000/1000  error=0.000001
    Training Time: 26.300815ms

    index 0: got 0.000250, want 0.000000
    index 1: got 0.997680, want 1.000000
    index 2: got 0.998551, want 1.000000
    index 3: got 0.000402, want 0.000000

Documentation

Overview

The nn package contains code for building a neural network and other related functions needed to run them.

As of now, the list of functionality supported is layers with flat and convolutional layers being a work in progress; activation layers with functions like Tanh, Sigmoid, ReLu, Arctan; and loss functions like MSE, RMSE, and Huber.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arctan

func Arctan(input *mat.Dense) *mat.Dense

Arctan Activation function

func ArctanDeriv

func ArctanDeriv(input *mat.Dense) *mat.Dense

Arctan Derivative function

func Gaussian

func Gaussian(input *mat.Dense) *mat.Dense

Gaussian Activation function

func GaussianDeriv

func GaussianDeriv(input *mat.Dense) *mat.Dense

Gaussian Derivative function

func Hmse

func Hmse(y_true, y_pred *mat.Dense, params []float64) float64

Hmse returns the half mean squared error given `y_true` and `y_pred` matrices.

func HmseDerivative

func HmseDerivative(y_true, y_pred *mat.Dense, params []float64) *mat.Dense

HmseDerivative returns the derivative matrix of the half mean squared error given `y_true` and `y_pred` matrices.

func Huber

func Huber(y_true, y_pred *mat.Dense, params []float64) float64

Huber returns the pseudo-huber loss given `y_true` and `y_pred` matrices. Params[0] represents the provided delta value.

func HuberDerivative

func HuberDerivative(y_true, y_pred *mat.Dense, params []float64) *mat.Dense

Huber returns the derivative matrix of the pseudo-huber loss given `y_true` and `y_pred` matrices. Params[0] represents the provided delta value.

func Linear

func Linear(input *mat.Dense) *mat.Dense

Linear Activation function

func LinearDeriv

func LinearDeriv(input *mat.Dense) *mat.Dense

Linear Derivative function

func Mae

func Mae(y_true, y_pred *mat.Dense, params []float64) float64

Mae returns the mean absolute error given `y_true` and `y_pred` matrices.

func MaeDerivative

func MaeDerivative(y_true, y_pred *mat.Dense, params []float64) *mat.Dense

MaeDerivative returns the derivative matrix of the mean absolute error given `y_true` and `y_pred` matrices. The derivative defaults to 1 when y_pred = y_true due to not being differentiable.

func Mse

func Mse(y_true, y_pred *mat.Dense, params []float64) float64

Mse returns the mean squared error given `y_true` and `y_pred` matrices.

func MseDerivative

func MseDerivative(y_true, y_pred *mat.Dense, params []float64) *mat.Dense

MseDerivative returns the derivative matrix of the mean squared error given `y_true` and `y_pred` matrices.

func ReLu

func ReLu(input *mat.Dense) *mat.Dense

ReLu Activation function: it defaults to 0 if x = 0

func ReLuDeriv

func ReLuDeriv(input *mat.Dense) *mat.Dense

ReLu Derivative function

func Rmse

func Rmse(y_true, y_pred *mat.Dense, params []float64) float64

Rmse returns the root mean squared error given `y_true` and `y_pred` matrices.

func RmseDerivative

func RmseDerivative(y_true, y_pred *mat.Dense, params []float64) *mat.Dense

RmseDerivative returns the derivative matrix of the root mean squared error given `y_true` and `y_pred` matrices.

func Sigmoid

func Sigmoid(input *mat.Dense) *mat.Dense

Sigmoid Activation function

func SigmoidDeriv

func SigmoidDeriv(input *mat.Dense) *mat.Dense

Sigmoid Derivative function

func Tanh

func Tanh(input *mat.Dense) *mat.Dense

Tanh Activation function

func TanhDeriv

func TanhDeriv(input *mat.Dense) *mat.Dense

Tanh Derivative function

Types

type Activation

type Activation func(input *mat.Dense) *mat.Dense

Activation defines a type for any activation function.

type ActivationLayer

type ActivationLayer struct {
	ACTIVATION      Activation
	ACTIVATIONDERIV Activation
	// contains filtered or unexported fields
}

ActivationLayer implements the Layer interface for an Activation layer in a neural network.

func NewActivationLayer

func NewActivationLayer(activation Activation, activationDeriv Activation) *ActivationLayer

NewActivationLayer returns a new instance of an activation layer.

func (*ActivationLayer) BackPropagation

func (layer *ActivationLayer) BackPropagation(output_error *mat.Dense, learning_rate float64) *mat.Dense

BackPropagation implements the Layer interface and returns a matrix after performing the derivative activation function on the layer values.

func (*ActivationLayer) ForwardPropagation

func (layer *ActivationLayer) ForwardPropagation(input *mat.Dense) *mat.Dense

ForwardPropagation implements the Layer interface and returns a matrix after performing the activation function on the layer values.

type ConvolutionalLayer

type ConvolutionalLayer struct {
	INPUT  *mat.Dense
	OUTPUT *mat.Dense
}

ConvolutionalLayer implements the Layer interface for a convolutional layer (CURRENT WIP).

func NewConvolutionalLayer

func NewConvolutionalLayer(input_size int, output_size int) *ConvolutionalLayer

NewConvolutionalLayer returns a new instance of a convolutional layer (CURRENT WIP).

func (*ConvolutionalLayer) BackPropagation

func (layer *ConvolutionalLayer) BackPropagation(output_error *mat.Dense, learning_rate float64) *mat.Dense

BackPropagation implements the Layer interface and returns the error matrix after performing back propagation (CURRENT WIP).

func (*ConvolutionalLayer) ForwardPropagation

func (layer *ConvolutionalLayer) ForwardPropagation(input *mat.Dense) *mat.Dense

ForwardPropagation implements the Layer interface and returns a matrix after performing forward propagation (CURRENT WIP).

type DenseLayer

type DenseLayer struct {
	// contains filtered or unexported fields
}

DenseLayer implements the Layer interface for a Dense layer in a neural network.

func NewDenseLayer

func NewDenseLayer(input_size int, output_size int) *DenseLayer

NewDenseLayer returns a new instance of a dense layer.

func (*DenseLayer) BackPropagation

func (layer *DenseLayer) BackPropagation(output_error *mat.Dense, learning_rate float64) *mat.Dense

BackPropagation implements the Layer interface and returns the error matrix after performing back propagation.

func (*DenseLayer) ForwardPropagation

func (layer *DenseLayer) ForwardPropagation(input *mat.Dense) *mat.Dense

ForwardPropagation implements the Layer interface and returns a matrix after performing forward propagation.

type FlattenLayer

type FlattenLayer struct {
	INPUT        *mat.Dense
	OUTPUT       *mat.Dense
	INPUT_SHAPE  int
	OUTPUT_SHAPE int
}

ConvolutionalLayer implements the Layer interface for a flat layer (CURRENT WIP).

func NewFlattenLayer

func NewFlattenLayer(input_size int, output_size int) *FlattenLayer

NewFlattenLayer returns a new instance of a flat layer (CURRENT WIP).

func (*FlattenLayer) BackPropagation

func (layer *FlattenLayer) BackPropagation(output_error *mat.Dense, learning_rate float64) *mat.Dense

BackPropagation implements the Layer interface and returns the error matrix after performing back propagation (CURRENT WIP).

func (*FlattenLayer) ForwardPropagation

func (layer *FlattenLayer) ForwardPropagation(input *mat.Dense) *mat.Dense

ForwardPropagation implements the Layer interface and returns a matrix after performing forward propagation (CURRENT WIP).

func (*FlattenLayer) GetShape

func (layer *FlattenLayer) GetShape() (int, int)

GetShape returns the flat layers dimensions

type Layer

type Layer interface {
	ForwardPropagation(input *mat.Dense) *mat.Dense
	BackPropagation(output_error *mat.Dense, learning_rate float64) *mat.Dense
}

Layer is a representation for different neural network layers, which require the functions ForwardPropagation and BackPropagation.

type Loss added in v0.3.0

type Loss struct {
	LOSSFUNC   LossFwd
	LOSSDERIV  LossBwd
	LOSSPARAMS []float64
}

Loss defines the essential loss functions for the neural network.

type LossBwd added in v0.3.0

type LossBwd func(y_true, y_pred *mat.Dense, params []float64) *mat.Dense

LossBwd defines a type for any loss function for back propagation.

type LossFwd added in v0.3.0

type LossFwd func(y_true, y_pred *mat.Dense, params []float64) float64

LossFwd defines a type for any loss function for forward propagation.

type Network

type Network struct {
	LAYERS      []*Layer
	LOSS        Loss
	OUTPUT_SIZE int
	LAYER_COUNT int
}

Network represents the neural network model specifics and related helper functions for loss.

func (*Network) AddLayer

func (net *Network) AddLayer(layerType, activationType string, output_nodes int)

AddLayer appends to `LAYERS` in the network given `layerType`, `activationType`, and output neurons.

func (*Network) Fit

func (net *Network) Fit(x_train, y_train [][]float64, epochs int, learning_rate float64, debug bool)

Fit trains the neural network provided training data, the number of epochs, and learning rate. If `debug` is true, then the error at each epoch is printed in terminal.

func (*Network) Predict

func (net *Network) Predict(input [][]float64) [][]float64

Predict returns predicted values given `input` through running the values through the neural network.

func (*Network) SetLoss

func (net *Network) SetLoss(lossType string, params []float64)

SetLoss initializes the loss function for the network given `lossType`. If the string is empty, it defaults to MSE loss.

Jump to

Keyboard shortcuts

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