neuralnetwork

package
v0.0.0-...-4303b71 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package neuralnetwork implements the machine learning library similar to keras architecture.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CrossEntropy

func CrossEntropy(prediction, truth []float64) float64

CrossEntropy returns the cross entropy loss

func Elu

func Elu(x float64) float64

Elu is an activation function. 0.7 is a parameter that should be above 0

func F1Score

func F1Score(predicted, actual []float64) int

F1Score returns the F1 Score

func FalseNegatives

func FalseNegatives(predicted, actual []float64) int

FalseNegatives returns the number of false negative predicted values.

func FalsePositives

func FalsePositives(predicted, actual []float64) int

FalsePositives returns the number of false positive predicted values.

func HeUniform

func HeUniform(x float64) float64

HeUniform stands for He Initialization or the glorot_unifom for kernel_initialization.

func JaccardIndex

func JaccardIndex(predicted, actual []float64) int

JaccardIndex returns the Jaccard index.

func LassoRegression

func LassoRegression(actual, pred []float64, lambda float64) float64

LassoRegression returns the LassoRegression or the l1 regularization to the loss function.

func Mse

func Mse(prediction, truth []float64) float64

Mse returns the mean squared error between prediction and truth arrays.

func OnesInitializer

func OnesInitializer(x float64) float64

OnesInitializer returns the ones initializer for the bias initialization

func Precision

func Precision(predicted, actual []float64) int

Precision returns the precision.

func Recall

func Recall(predicted, actual []float64) int

Recall returns the recall.

func Relu

func Relu(x float64) float64

Relu implements the rectified linear unit.

func RidgeRegression

func RidgeRegression(actual, pred []float64, lambda float64) float64

RidgeRegression returns the RidgeRegression or the l2 regularization to the loss function.

func Rmse

func Rmse(prediction, truth []float64) float64

Rmse returns the root mean squared error between prediction and truth arrays.

func Sensitivity

func Sensitivity(predicted, actual []float64) int

Sensitivity returns the sensitivity

func Sigmoid

func Sigmoid(x float64) float64

Sigmoid activation function

func SigmoidPrime

func SigmoidPrime(x float64) float64

SigmoidPrime is the derivative of the Sigmoid

func SoftDiceLoss

func SoftDiceLoss(values []float64, truth []float64) float64

SoftDiceLoss implements the soft dice loss.

func Specificity

func Specificity(predicted, actual []float64) int

Specificity returns the specificity

func Swish

func Swish(x float64) float64

Swish activation function. Beta is a parameter that should be above 0

func Tanh

func Tanh(x float64) float64

Tanh returns the tanh activation function.

func TrueNegatives

func TrueNegatives(predicted, actual []float64) int

TrueNegatives returns the number of true negative predicted values.

func TruePositivies

func TruePositivies(predicted, actual []float64) int

TruePositivies returns the number of true positive predicted values.

func Variance

func Variance(fls []float64) float64

Variance returns the variance

func ZeroInitializer

func ZeroInitializer(x float64) float64

ZeroInitializer returns the zeros initializer for the bias initialization

Types

type BatchNormLayer

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

BatchNormLayer layer

func BatchNorm

func BatchNorm(inputs []float64) BatchNormLayer

BatchNorm init

func (*BatchNormLayer) Call

func (bn *BatchNormLayer) Call() []float64

Call for the batch normalization layer

type Biases

type Biases struct {
	BiasInit func(float64) float64
	// contains filtered or unexported fields
}

Biases struct with the actual biases and the bias initializer function.

func BiasInit

func BiasInit(a int, biasInit func(float64) float64) Biases

BiasInit used for bias initialization. Already defined at the initialization of the dense layer.

type CSVLoader

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

func (*CSVLoader) Read

func (csv *CSVLoader) Read(filepath string) ([]float64, error)

type Callback

type Callback interface {
	Do(a ...interface{})
}

Callback interface

type CallbackHistory

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

CallbackHistory struct

func (*CallbackHistory) ReadToStrings

func (ch *CallbackHistory) ReadToStrings() ([]string, error)

ReadToStrings returns history in strings.

type DenseLayer

type DenseLayer struct {
	Activation func(float64) float64
	KernelInit func(float64) float64
	BiasInit   func(float64) float64
	// contains filtered or unexported fields
}

DenseLayer defines a fully connected layer.

func Dense

func Dense(units int, inputs []float64, activation func(float64) float64) DenseLayer

Dense fully connected layer initializer

func (DenseLayer) Call

func (d DenseLayer) Call() []float64

Call of the dense layer.Outputs the next tensors.

func (DenseLayer) GetBiases

func (d DenseLayer) GetBiases() matrix.Vector

GetBiases returns the layer's biases.

func (DenseLayer) GetWeights

func (d DenseLayer) GetWeights() matrix.Matrix

GetWeights returns the layer's weights.

func (DenseLayer) Name

func (d DenseLayer) Name() string

Name of the dense layer

func (*DenseLayer) SetBiases

func (d *DenseLayer) SetBiases(bs matrix.Vector)

SetBiases is used for manually defining the bias vector.

func (*DenseLayer) SetWeights

func (d *DenseLayer) SetWeights(kernels matrix.Matrix)

SetWeights is used for manually defining the weight matrix.

func (DenseLayer) TrainableParameters

func (d DenseLayer) TrainableParameters() int

TrainableParameters returns the count of trainable parameters.

type DropoutLayer

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

DropoutLayer layer

func Dropout

func Dropout(inputs []float64, rate float64) DropoutLayer

Dropout init

func (*DropoutLayer) Call

func (dr *DropoutLayer) Call() []float64

Call for the dropout layer

type EarlyStopper

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

EarlyStopper callback

type FlattenLayer

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

FlattenLayer layer

func Flatten

func Flatten(m matrix.Matrix) FlattenLayer

Flatten init.

func (*FlattenLayer) Call

func (f *FlattenLayer) Call() []float64

Call of the FlattenLayer

type InputLayer

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

InputLayer layer, much like the keras one.

func Input

func Input(inputs []float64) InputLayer

Input layer

func (*InputLayer) Call

func (i *InputLayer) Call() []float64

Call of the input layer

type JSONLoader

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

func (*JSONLoader) Read

func (js *JSONLoader) Read(filepath string) ([]float64, error)

type Layer

type Layer interface {
	Call() []float64
	GetWeights() matrix.Matrix
	GetBiases() matrix.Vector
	Name() string
	TrainableParameters() int
}

Layer interface given these 5 functions which every layer must have.

type Loader

type Loader interface {
	Read() []float64
	LoadBatch(batchSize int) []float64
}

type Metrics

type Metrics interface {
	Measure([]float64, []float64) float64
	Name() string
}

Metrics is an interface that requires two functions, Measure and Name and is passed to the model.compile method.

type Model

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

Model implements the model architecure.

func Sequential

func Sequential(layers []Layer, name string) *Model

Sequential returns a model given layers and a name.

func (*Model) Add

func (m *Model) Add(layer Layer) *Model

Add method adds a layer to the end of the model architecture

func (*Model) CallbackList

func (m *Model) CallbackList() []Callback

CallbackList returns model's callback list

func (*Model) Compile

func (m *Model) Compile(optimizer Optimizer, loss func([]float64, []float64) float64, ms []Metrics)

Compile compiles the model given the optimizer, loss and metrics

func (*Model) CsvLogger

func (m *Model) CsvLogger(filename string) error

CsvLogger logger

func (*Model) EarlyStopping

func (m *Model) EarlyStopping(index int, patience int, at int, minDdelta float64, mode string)

EarlyStopping is much like the keras EarlyStopping callback. Index is the number in the model metrics attribute. Look it up under model.get_metrics()

func (*Model) GetLayerByIndex

func (m *Model) GetLayerByIndex(index int) Layer

GetLayerByIndex returns the ith layer.

func (*Model) GetLayerByName

func (m *Model) GetLayerByName(name string) Layer

GetLayerByName returns the layer given its name.

func (*Model) GetMetricsByIndex

func (m *Model) GetMetricsByIndex(index int) Metrics

GetMetricsByIndex returns the index's model metric

func (*Model) History

func (m *Model) History(filepath string) (*CallbackHistory, error)

History for model checkpointing

func (*Model) LearningRateScheduler

func (m *Model) LearningRateScheduler(fn func(x float64) float64)

LearningRateScheduler defined by fn

func (*Model) ModelCheckpoint

func (m *Model) ModelCheckpoint(filepath string, metrics Metrics, saveWeightsOnly bool) error

ModelCheckpoint callback

func (*Model) Predict

func (m *Model) Predict(values []float64) []float64

Predict does the feed forward magic when fed the inputs.

func (*Model) Summary

func (m *Model) Summary()

Summary prints the layer by layer summaary along with trainable parameters.

func (*Model) Train

func (m *Model) Train(trainX, trainY []float64, epochs int) map[string]float64

Train trains the model given trainX and trainY data and the number of epochs. It keeps track of the defined metrics and prints it every epoch. It also prints the training duration. It returns a map from strings to floats, where strings represent the metrics name and float the metrics value.

type Network

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

Network defines the neural network.

func InitNetwork

func InitNetwork(inputNodes, hiddenNodes, outputNodes int, lr float64) Network

InitNetwork initializes the network with the number of nodes and the learning rate.

func (*Network) Predict

func (n *Network) Predict(inputArray []float64) []float64

Predict returns the model's prediction based on inputArray

func (*Network) Train

func (n *Network) Train(inputArray, targetArray []float64)

Train performs the training.

type Optimizer

type Optimizer interface {
	ApplyGradients()
}

Optimizer interface requires an ApplyGradients function. Pass it to the model compilation.

type ReduceLearningRateOnPlateau

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

ReduceLearningRateOnPlateau callback

func (*ReduceLearningRateOnPlateau) ReduceLr

func (rlr *ReduceLearningRateOnPlateau) ReduceLr(m *Model, at int)

ReduceLr callback

type SoftmaxLayer

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

SoftmaxLayer layer

func Softmax

func Softmax(inputs []float64, classes int) SoftmaxLayer

Softmax returns the softmax layer based on values.

func (*SoftmaxLayer) Call

func (s *SoftmaxLayer) Call() []float64

Call of the softmax

type TrainingLog

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

TrainingLog returns model's log

type Weights

type Weights struct {
	KernelInit func(float64) float64
	// contains filtered or unexported fields
}

Weights struct with the actual kernels and the kernel initializer function.

func WeightInit

func WeightInit(a, b int, kernelInit func(float64) float64) Weights

WeightInit used for weight initialization. Already defined at the initialization of the dense layer.

Jump to

Keyboard shortcuts

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