nn

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package nn provides neural network building blocks, as layers and the general struct for ANN.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpNeuralNetwork

func DumpNeuralNetwork(nn NeuralNetwork, path string) error

DumpNeuralNetwork dumps the JSON represantation to a file given by a path.

The creating of the file is managed by os.Create function.

Types

type NeuralNetwork

type NeuralNetwork interface {
	json.Marshaler
	json.Unmarshaler

	InputSize() [2]int
	OutputSize() [2]int

	// Training functions
	ComputeCost(yHat, Y Matrix[float64]) float64
	ForwardPropagate(X Matrix[float64]) (inputCache [][2]Matrix[float64])
	BackPropagate(Y Matrix[float64], inputCache [][2]Matrix[float64], parameters utils.NeuralNetworkParameters)

	// Prediction functions
	Predict(X Matrix[float64]) (Y Matrix[float64])
	Train(X, Y []Matrix[float64], parameters utils.NeuralNetworkParameters) error
}

NeuralNetwork is an interface for a general ANN.

InputSize returns the dimensions of the input to the first layer. Returned as [2]{rowCount, 1}, but in practice columnCount can also differ.

OutputSize returns the dimensions of the output of the last layer. Returned as [2]{rowCount, 1}, but in practice columnCount can also differ.

ComputeCost uses the LossFunction of the last layer to compute cost, i.e. the error of the prediction. Separate columns of y and yHat represent different samples, and the cost is averaged between them.

ForwardPropagate produces a slices of outputs for each layer. First element in array for each slice item is the linear combination using weights and biases, the second - linear combination passed through the activation function. The length of the returned slice is numberOfLayers+1, as the 0th element is the initial input to the ANN.

item[0] = W*X + b
item[1] = activation(item[0])

BackPropagate uses the inputCache, produced by ForwardPropagate, to update the layers. Utilizes parameters for the neural network.

Predict produces ANN prediction for the given data. Equivalent to ForwardPropagate()[-1][1].

Train uses training functions (ComputeCost, ForwardPropagate, BackPropagate) to update the weights of the layers to decrease the cost and loss.

func LoadNeuralNetwork

func LoadNeuralNetwork(path string) (NeuralNetwork, error)

LoadNeuralNetwork loads ANN from a JSON file given by path.

Errors are returned due to any errors in the unmarshalling of any of the layers.

func NewNeuralNetwork

func NewNeuralNetwork(layers []layers.Layer, lossFunction loss.LossFunction[float64]) NeuralNetwork

NewNeuralNetwork produces an ANN based on layer slice and loss function applied to the \ last layer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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