keras

package
v0.0.0-...-549aca6 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package keras implements many functionalities from the popular Keras API which is a deep learning API written in Python, running on top of the machine learning platform TensorFlow. This package was started mainly for fun is still very much under development

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColNum

func ColNum(m Matrix) int

ColNum returns the number of columns in a matrix.

func CrossEntropy

func CrossEntropy(prediction, truth []float64) float64

CrossEntropy returns the cross entropy loss

func Dimensions

func Dimensions(m Matrix) (int, int)

Dimensions returns the number of rows and columns of m.

func ELU

func ELU(x float64) float64

ELUExponential Linear Unit or its widely known name ELU is a function that tend to converge cost to zero faster and produce more accurate results

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 estimates the false positive predicted values

func HeUniform

func HeUniform(x float64) float64

HeUniform stands for He Initialization or the glorot_unifom for kernel_initialization.

func Max

func Max(x, y int) int

func MeanSqRoot

func MeanSqRoot(prediction, truth []float64) float64

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

func NumberOfElements

func NumberOfElements(m Matrix) int

NumberOfElements returns the number of elements.

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 PrintByRows

func PrintByRows(m Matrix)

PrintByRow prints the matrix by row.

func PrintfMatrix

func PrintfMatrix(m Matrix)

PrintfMatrix is a helper function that will print a matrix to stdout.

func ReLU

func ReLU(x float64) float64

ReLU or rectified linear activation function is a piecewise linear function that will output the input directly if it is positive, otherwise, it will output zero

func Recall

func Recall(predicted, actual []float64) int

Recall returns the recall.

func RootMeanSq

func RootMeanSq(prediction, truth []float64) float64

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

func RowNum

func RowNum(m Matrix) int

RowNum returns the number of rows in a matrix.

func Sensitivity

func Sensitivity(predicted, actual []float64) int

Sensitivity returns the sensitivity

func Sigmoid

func Sigmoid(x float64) float64

Sigmoid activation function is commonly known as the logistic function

func SigmoidPrime

func SigmoidPrime(x float64) float64

SigmoidPrime calculates the derivative of the Sigmoid

func SimpleRandomVector

func SimpleRandomVector(size int) []float64

RandomVector returns a random valued vector.

func Specificity

func Specificity(predicted, actual []float64) int

Specificity returns the specificity

func Swish

func Swish(x float64) float64

Swish is an activation function proposed byGoogle Brain Team, which is simply f(x) = x · sigmoid(x)

func Tanh

func Tanh(x float64) float64

Tanh calculates tanh(x) of a number

func ToArray

func ToArray(m Matrix) []float64

ToArray returns the matrix in array form.

func TrueNegatives

func TrueNegatives(predicted, actual []float64) int

TrueNegative calculates the 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 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 Conv2DLayer

type Conv2DLayer struct {
	Filters         int
	Inputs, Outputs []float64
	Weights         Weights
	Biases          Biases

	Activation func(float64) float64
	KernelInit func(float64) float64
	BiasInit   func(float64) float64
	// contains filtered or unexported fields
}

DenseLayer defines a fully connected layer.

func Conv2D

func Conv2D(numFilter int, x int, y int, inputs []float64, padding func(x, y int) Weights) Conv2DLayer

Conv2D initializes a Conv2DLayer

func (Conv2DLayer) Call

func (cd Conv2DLayer) Call() []float64

func (Conv2DLayer) GetBiases

func (cd Conv2DLayer) GetBiases() Vector

GetBiases returns the layer's biases.

func (Conv2DLayer) GetWeights

func (cd Conv2DLayer) GetWeights() Matrix

GetWeights returns the layer's weights.

func (Conv2DLayer) Name

func (cd Conv2DLayer) Name() string

Name of the dense layer

func (Conv2DLayer) TrainableParameters

func (cd Conv2DLayer) TrainableParameters() int

TrainableParameters returns the count of trainable parameters.

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() Vector

GetBiases returns the layer's biases.

func (DenseLayer) GetWeights

func (d DenseLayer) GetWeights() 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 Vector)

SetBiases is used for manually defining the bias vector.

func (*DenseLayer) SetWeights

func (d *DenseLayer) SetWeights(Kernels Matrix)

SetWeights is used for manually defining the weight

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) FlattenLayer

Flatten init.

func (*FlattenLayer) Call

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

Call of the FlattenLayer

type InputLayer

type InputLayer struct {
	Inputs    []float64
	Weights   Weights
	Biases    Biases
	Trainable bool
	Name      string
	// contains filtered or unexported fields
}

InputLayer layer, much like the keras one.

func Input

func Input(inputs []float64) InputLayer

Input

func (*InputLayer) Call

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

Call of the input layer

type Layer

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

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

type Matrix

type Matrix struct {
	Matrix [][]float64
}

Matrix type is a 2D slice of float64 Values.

func FromArray

func FromArray(arr []float64) Matrix

FromArray returns a matrix from array

func NewMatrix

func NewMatrix(m, n int) Matrix

NewMatrix allocates the appropriate memory for an m x n matrix.

func RandomMatrix

func RandomMatrix(m, n int) Matrix

RandomMatrix will create a new matrix and randomize float64 values.

func Transpose

func Transpose(m Matrix) Matrix

Transpose will tranpose a matrix and modify a given matrix.

func Zeros

func Zeros(row, column int) Matrix

Zeros returns a matrix of zeros.

func (Matrix) Add

func (m Matrix) Add(mat Matrix) Matrix

Add performs elementary matrix addition

func (Matrix) MapFunc

func (m Matrix) MapFunc(f func(x float64) float64) Matrix

MapFunc applies f to every element

func (Matrix) Multiply

func (m Matrix) Multiply(mat Matrix) Matrix

Multiply performs elementary matrix multiplication

func (Matrix) ScalarAdition

func (m Matrix) ScalarAdition(scalar float64) Matrix

ScalarAdition adds a scalar to every elements

func (Matrix) ScalarMultiplication

func (m Matrix) ScalarMultiplication(scalar float64) Matrix

ScalarMultiplication multiplies every element with a scalar

func (Matrix) Subtract

func (m Matrix) Subtract(mat Matrix) Matrix

Subtract performs elementary matrix subtraction

type Metrics

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

type Model

type Model struct {
	ConvLayers   []Layer
	Name         string
	Optimizer    Optimizer
	LossFunc     func([]float64, []float64) float64
	LossValues   []float64
	Duration     time.Duration
	Settings     []Metrics
	TrainDataX   []float64
	TrainDataY   []float64
	Callbacks    []Callback
	Training     bool
	LearningRate float64
	TrainingLog  TrainingLog
}

Model architecure to implement neural network.

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 {
	InputNodes, HiddenNodes, OutputNodes int
	WeightsIh, WeightsHo, BiasO, BiasH   Matrix
	LearningRate                         float64
}

Network defines a simple neural network architecture.

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 Shape

type Shape []float64

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 []string

TrainingLog returns model's log

type Vector

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

Vector type

func NewVector

func NewVector(slice []float64) Vector

NewVector returns a vector type

func RandomVector

func RandomVector(size int) Vector

RandomVector returns a random valued vector.

func (Vector) Add

func (v Vector) Add(v2 Vector) Vector

Add returns an elementary operation on two vectors.

func (Vector) ApplyMatrix

func (v Vector) ApplyMatrix(matrix Matrix) Vector

ApplyMatrix returns the vector through a matrix transformation.

func (Vector) Map

func (v Vector) Map(f func(float64) float64) Vector

Map maps the vector by with the function

func (Vector) NumberOfElements

func (v Vector) NumberOfElements() int

NumberOfElements returns the number of elements.

func (Vector) Slice

func (v Vector) Slice() []float64

Slice returns vector.slice. You can perform indexing with this method.

type Weights

type Weights struct {
	Kernels    Matrix
	KernelInit func(float64) float64
}

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

func DefaultPadding

func DefaultPadding(x, y int) Weights

func Valid

func Valid(x, y int) Weights

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