kafn

package module
v0.0.0-...-d22bb4b Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const Eps = 1e-16

Smallest number

View Source
const Leps = 1e-20
View Source
const MinIterations = 5

Minimal number of iterations

Variables

View Source
var Rand = NewRand()

Functions

func ArgMax

func ArgMax(xx []Deepfloat64) int

ArgMax is the index of the largest element

func Fibonacci

func Fibonacci() func() int

fibonacci returns a function that returns successive fibonacci numbers from each successive call

func Float64

func Float64(a Deepfloat64) float64

Types

type BinaryCrossEntropy

type BinaryCrossEntropy struct{}

BinaryCrossEntropy is binary CE loss

func (BinaryCrossEntropy) Cf

func (l BinaryCrossEntropy) Cf(estimate, ideal [][]Deepfloat64) Deepfloat64

Cf is CE(...)

func (BinaryCrossEntropy) Df

func (l BinaryCrossEntropy) Df(estimate, ideal Deepfloat64) Deepfloat64

Df is CE'(...)

func (BinaryCrossEntropy) F

func (l BinaryCrossEntropy) F(estimate, ideal Deepfloat64) Deepfloat64

F is MSE(...)

type Config

type Config struct {
	// Number of inputs
	Inputs int
	// Number of outputs
	Outputs int
	// Loss functions: {LossCrossEntropy, LossBinaryCrossEntropy, LossMeanSquared}
	Loss LossType
	// Error/Loss precision
	LossPrecision int
	// Specifies basis size
	Degree int
	// Specify Synap Tags for the input layer
	InputTags []string
	// Number of training iterations
	Epoch uint32
	// If Smooth() need to be executed before each training iteration
	Smooth bool
	// TrapolationLinear by default
	Trapolation tabulatedfunction.Trapolation
}

Config defines the network topology, activations, losses etc

type CrossEntropy

type CrossEntropy struct{}

CrossEntropy is CE loss

func (CrossEntropy) Cf

func (l CrossEntropy) Cf(estimate, ideal [][]Deepfloat64) Deepfloat64

Cf is CE(...)

func (CrossEntropy) Df

func (l CrossEntropy) Df(estimate, ideal Deepfloat64) Deepfloat64

Df is CE'(...)

func (CrossEntropy) F

func (l CrossEntropy) F(estimate, ideal Deepfloat64) Deepfloat64

F is MSE(...)

type Deepfloat64

type Deepfloat64 = float64

func Add

func Add(a, b Deepfloat64) Deepfloat64

func Copy

func Copy(a Deepfloat64) Deepfloat64

func DF

func DF(f float64) Deepfloat64

func Div

func Div(a, b Deepfloat64) Deepfloat64

func Mul

func Mul(a, b Deepfloat64) Deepfloat64

func Round

func Round(x Deepfloat64) Deepfloat64

Round to nearest integer

func Sub

func Sub(a, b Deepfloat64) Deepfloat64

func Sum

func Sum(xx []Deepfloat64) (sum Deepfloat64)

Sum is sum

func TotalError

func TotalError(E []Deepfloat64) Deepfloat64

type Dump

type Dump struct {
	Config      *Config
	Weights     [][][][]Deepfloat64
	Activations [][]Point
	Synapses    [][]Point
}

Dump is a neural network dump

type Example

type Example struct {
	Input    []Deepfloat64
	Response []Deepfloat64
}

Example is an input-target pair

type Examples

type Examples []Example

Examples is a set of input-output pairs

func (Examples) Shuffle

func (e Examples) Shuffle()

Shuffle shuffles slice in-place

func (Examples) Split

func (e Examples) Split(p float64) (first, second Examples)

Split assigns each element to two new slices according to probability p

func (Examples) SplitN

func (e Examples) SplitN(n int) []Examples

SplitN splits slice into n parts

func (Examples) SplitSize

func (e Examples) SplitSize(size int) []Examples

SplitSize splits slice into parts of size size

type KafnRand

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

func NewRand

func NewRand() *KafnRand

func (*KafnRand) Float64

func (r *KafnRand) Float64() float64

func (*KafnRand) Intn

func (r *KafnRand) Intn(n int) int

func (*KafnRand) NormFloat64

func (r *KafnRand) NormFloat64() float64

func (*KafnRand) Perm

func (r *KafnRand) Perm(n int) []int

func (*KafnRand) Seed

func (r *KafnRand) Seed(Seed int64)

type Layer

type Layer struct {
	Number  int
	S       SynapseType
	Neurons []*Neuron
}

Layer is a set of neurons and corresponding activation

func NewLayer

func NewLayer(l, n int, synapse SynapseType) *Layer

NewLayer creates a new layer with n nodes

func (*Layer) Connect

func (l *Layer) Connect(next *Layer, c *Config)

Connect fully connects layer l to next, and initializes each synapse with the given weight function func (l *Layer) Connect(next *Layer, degree int, weight WeightType) {

func (*Layer) CreateInputSynapses

func (l *Layer) CreateInputSynapses(c *Config)

CreateInputSynapses create input synapses for the bottom layer

func (*Layer) Fire

func (l *Layer) Fire()

func (*Layer) FireT

func (l *Layer) FireT(trapolation tabulatedfunction.Trapolation)

func (Layer) NumIns

func (l Layer) NumIns() (num int)

func (Layer) String

func (l Layer) String() string

type Loss

type Loss interface {
	F(estimate, ideal Deepfloat64) Deepfloat64
	Cf(estimate, ideal [][]Deepfloat64) Deepfloat64
	Df(estimate, ideal Deepfloat64) Deepfloat64
}

Loss is satisfied by loss functions

func GetLoss

func GetLoss(loss LossType) Loss

GetLoss returns a loss function given a LossType

type LossType

type LossType int

LossType represents a loss function

const (
	// LossNone signifies unspecified loss
	LossNone LossType = 0
	// LossCrossEntropy is cross entropy loss
	LossCrossEntropy LossType = 1
	// LossBinaryCrossEntropy is the special case of binary cross entropy loss
	LossBinaryCrossEntropy LossType = 2
	// LossMeanSquared is MSE
	LossMeanSquared LossType = 3
)

func (LossType) String

func (l LossType) String() string

type MeanSquared

type MeanSquared struct{}

MeanSquared in MSE loss

func (MeanSquared) Cf

func (l MeanSquared) Cf(estimate, ideal [][]Deepfloat64) Deepfloat64

Cf is MSE(...)

func (MeanSquared) Df

func (l MeanSquared) Df(estimate, ideal Deepfloat64) Deepfloat64

Df is MSE'(...)

func (MeanSquared) F

func (l MeanSquared) F(estimate, ideal Deepfloat64) Deepfloat64

F is MSE(...)

type Neural

type Neural struct {
	Layers     []*Layer
	Config     *Config
	TotalError Deepfloat64
}

Neural is a neural network

func FromDump

func FromDump(dump *Dump) *Neural

FromDump restores a Neural from a dump

func Load

func Load(path string) (*Neural, error)

Load retrieves network from the file specified created using Save method

func NewNeural

func NewNeural(c *Config) *Neural

NewNeural returns a new neural network

func Unmarshal

func Unmarshal(bytes []byte) (*Neural, error)

Unmarshal restores network from a JSON blob

func (*Neural) ApplySynapses

func (n *Neural) ApplySynapses(points [][]Point)

func (*Neural) ApplyWeights

func (n *Neural) ApplyWeights(weights [][][][]Deepfloat64)

ApplyWeights sets the weights from a four-dimensional slice

func (*Neural) Check

func (n *Neural) Check() string

func (*Neural) Dot

func (n *Neural) Dot(path string) error

Save the network in DOT format for graphviz

func (*Neural) DrawPS

func (n *Neural) DrawPS(path_prefix string)

func (*Neural) Dump

func (n *Neural) Dump() *Dump

Dump generates a network dump

func (*Neural) Forward

func (n *Neural) Forward(input []Deepfloat64) error

Forward computes a forward pass

func (*Neural) ForwardT

func (n *Neural) ForwardT(input []Deepfloat64, trapolation tabulatedfunction.Trapolation) error

ForwardT computes a forward pass with trapolation

func (*Neural) Marshal

func (n *Neural) Marshal() ([]byte, error)

Marshal marshals to JSON from network

func (*Neural) Net

func (n *Neural) Net(path string) error

Save the network in NET format

func (*Neural) NumWeights

func (n *Neural) NumWeights() (num int)

NumWeights returns the number of weights in the network

func (*Neural) Polinate

func (n *Neural) Polinate()

func (*Neural) Predict

func (n *Neural) Predict(input []Deepfloat64) []Deepfloat64

Predict computes a forward pass and returns a prediction

func (*Neural) Save

func (n *Neural) Save(path string) error

Save saves network into the file specified to be loaded later

func (*Neural) SaveReadable

func (n *Neural) SaveReadable(path string) error

Save saves network in readable JSON into the file specified

func (*Neural) Smooth

func (n *Neural) Smooth()

func (*Neural) String

func (n *Neural) String() string

func (*Neural) Synapses

func (n *Neural) Synapses() [][]Point

func (*Neural) Trapolate

func (n *Neural) Trapolate(input []Deepfloat64, trapolation tabulatedfunction.Trapolation) []Deepfloat64

Trapolate computes a forward pass with trapolation and returns a prediction

func (*Neural) Weights

func (n *Neural) Weights() [][][][]Deepfloat64

Weights returns all weights in sequence

type Neuron

type Neuron struct {
	In             []Synapse
	Out            []Synapse
	Ideal          Deepfloat64
	Sum            Deepfloat64
	MinSum, MaxSum Deepfloat64
}

Neuron is a neural network node

func NewNeuron

func NewNeuron() *Neuron

NewNeuron returns a neuron with the given activation

type OnlineTrainer

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

OnlineTrainer is a basic, online network trainer

func NewTrainer

func NewTrainer(precision, verbosity, parallelism int) *OnlineTrainer

NewTrainer creates a new trainer

func (*OnlineTrainer) Save

func (t *OnlineTrainer) Save(path string) error

Save() saves internal of the trainer in readable JSON into file specified

func (*OnlineTrainer) SetPrefix

func (t *OnlineTrainer) SetPrefix(prefix string)

Set new output prtefix

func (*OnlineTrainer) Train

func (t *OnlineTrainer) Train(n *Neural, examples, validation Examples, iterations uint32)

Train trains n

type Point

type Point struct {
	X, Y Deepfloat64
}

Point is a point in Tabulated activation

type StatsPrinter

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

StatsPrinter prints training progress

func NewStatsPrinter

func NewStatsPrinter(precision int) *StatsPrinter

NewStatsPrinter creates a StatsPrinter

func (*StatsPrinter) Init

func (p *StatsPrinter) Init(n *Neural)

Init initializes printer

func (*StatsPrinter) PrintProgress

func (p *StatsPrinter) PrintProgress(n *Neural, validation Examples, elapsed time.Duration, iteration uint32)

PrintProgress prints the current state of training

func (*StatsPrinter) SetPrefix

func (p *StatsPrinter) SetPrefix(prefix string)

SetPrefix set new prefix

type Synapse

type Synapse interface {
	Refire()
	Fire(Deepfloat64)
	FireT(Deepfloat64, tabulatedfunction.Trapolation)
	FireDerivative() Deepfloat64
	SetTag(string)
	GetTag() string
	SetWeight(int, Deepfloat64)
	GetGradient(Deepfloat64, int) Deepfloat64
	GetWeight(int) Deepfloat64
	String() string
	WeightsString() string
	GetIn() Deepfloat64
	GetOut() Deepfloat64
	Len() int
	SetWeights([]Deepfloat64)
	GetWeights() []Deepfloat64
	GetUp() *Neuron
	Epoch(uint32)
	AddPoint(x, y Deepfloat64, it uint32)
	Trapolate(x Deepfloat64, trapolation tabulatedfunction.Trapolation) Deepfloat64
	GetPoint(i int) (Deepfloat64, Deepfloat64)
	DrawPS(path string)
	Smooth()
	Polinate(Synapse)
	Clear()
}

Synapse is an edge between neurons

type SynapseAnalytic

type SynapseAnalytic struct {
	Weights []Deepfloat64
	Up      *Neuron
	In, Out Deepfloat64
	Tag     string
}

func NewSynapseAnalytic

func NewSynapseAnalytic(up *Neuron, degree int, init_weights []Deepfloat64, tag string) *SynapseAnalytic

NewSynapseAnalytic returns a synapse with the weigths preset with specified initializer and marked with specified tag

func (*SynapseAnalytic) AddPoint

func (s *SynapseAnalytic) AddPoint(x, y Deepfloat64, it uint32)

func (*SynapseAnalytic) Clear

func (s *SynapseAnalytic) Clear()

func (*SynapseAnalytic) DrawPS

func (s *SynapseAnalytic) DrawPS(path string)

func (*SynapseAnalytic) Epoch

func (s *SynapseAnalytic) Epoch(uint32)

func (*SynapseAnalytic) Fire

func (s *SynapseAnalytic) Fire(value Deepfloat64)

func (*SynapseAnalytic) FireDerivative

func (s *SynapseAnalytic) FireDerivative() Deepfloat64

func (*SynapseAnalytic) FireT

func (s *SynapseAnalytic) FireT(value Deepfloat64, trapolation tabulatedfunction.Trapolation)

func (*SynapseAnalytic) GetGradient

func (s *SynapseAnalytic) GetGradient(D_E_x Deepfloat64, k int) Deepfloat64

func (*SynapseAnalytic) GetIn

func (s *SynapseAnalytic) GetIn() Deepfloat64

func (*SynapseAnalytic) GetOut

func (s *SynapseAnalytic) GetOut() Deepfloat64

func (*SynapseAnalytic) GetPoint

func (s *SynapseAnalytic) GetPoint(i int) (Deepfloat64, Deepfloat64)

GetPoint() returns (0,0,1)

func (*SynapseAnalytic) GetTag

func (s *SynapseAnalytic) GetTag() string

func (*SynapseAnalytic) GetUp

func (s *SynapseAnalytic) GetUp() *Neuron

func (*SynapseAnalytic) GetWeight

func (s *SynapseAnalytic) GetWeight(k int) Deepfloat64

func (*SynapseAnalytic) GetWeights

func (s *SynapseAnalytic) GetWeights() []Deepfloat64

func (*SynapseAnalytic) Len

func (s *SynapseAnalytic) Len() int

func (*SynapseAnalytic) Polinate

func (s *SynapseAnalytic) Polinate(m Synapse)

func (*SynapseAnalytic) Refire

func (s *SynapseAnalytic) Refire()

func (*SynapseAnalytic) SetTag

func (s *SynapseAnalytic) SetTag(tag string)

func (*SynapseAnalytic) SetWeight

func (s *SynapseAnalytic) SetWeight(k int, weight Deepfloat64)

func (*SynapseAnalytic) SetWeights

func (s *SynapseAnalytic) SetWeights(w []Deepfloat64)

func (*SynapseAnalytic) Smooth

func (s *SynapseAnalytic) Smooth()

func (*SynapseAnalytic) String

func (s *SynapseAnalytic) String() string

func (*SynapseAnalytic) Trapolate

func (*SynapseAnalytic) WeightsString

func (s *SynapseAnalytic) WeightsString() string

type SynapseTabulated

type SynapseTabulated struct {
	Up      *Neuron
	In, Out Deepfloat64
	Tag     string
	// contains filtered or unexported fields
}

func NewSynapseTabulated

func NewSynapseTabulated(c *Config, up *Neuron, tag string) *SynapseTabulated

NewSynapseTabulated returns a tabulated function synapse preset with specific tag

func (*SynapseTabulated) AddPoint

func (s *SynapseTabulated) AddPoint(x, y Deepfloat64, it uint32)

func (*SynapseTabulated) Clear

func (s *SynapseTabulated) Clear()

func (*SynapseTabulated) DrawPS

func (s *SynapseTabulated) DrawPS(path string)

func (*SynapseTabulated) Epoch

func (s *SynapseTabulated) Epoch(epoch uint32)

func (*SynapseTabulated) Fire

func (s *SynapseTabulated) Fire(value Deepfloat64)

func (*SynapseTabulated) FireDerivative

func (s *SynapseTabulated) FireDerivative() Deepfloat64

func (*SynapseTabulated) FireT

func (s *SynapseTabulated) FireT(value Deepfloat64, trapolation tabulatedfunction.Trapolation)

func (*SynapseTabulated) GetGradient

func (s *SynapseTabulated) GetGradient(D_E_x Deepfloat64, k int) Deepfloat64

func (*SynapseTabulated) GetIn

func (s *SynapseTabulated) GetIn() Deepfloat64

func (*SynapseTabulated) GetOut

func (s *SynapseTabulated) GetOut() Deepfloat64

func (*SynapseTabulated) GetPoint

func (s *SynapseTabulated) GetPoint(i int) (Deepfloat64, Deepfloat64)

GetPoint() returns n-th point in Tabulated activation

func (*SynapseTabulated) GetTag

func (s *SynapseTabulated) GetTag() string

func (*SynapseTabulated) GetUp

func (s *SynapseTabulated) GetUp() *Neuron

func (*SynapseTabulated) GetWeight

func (s *SynapseTabulated) GetWeight(k int) Deepfloat64

func (*SynapseTabulated) GetWeights

func (s *SynapseTabulated) GetWeights() []Deepfloat64

func (*SynapseTabulated) Len

func (s *SynapseTabulated) Len() int

func (*SynapseTabulated) Polinate

func (s *SynapseTabulated) Polinate(m Synapse)

func (*SynapseTabulated) Refire

func (s *SynapseTabulated) Refire()

func (*SynapseTabulated) RefireT

func (s *SynapseTabulated) RefireT(trapolation tabulatedfunction.Trapolation)

func (*SynapseTabulated) SetTag

func (s *SynapseTabulated) SetTag(tag string)

func (*SynapseTabulated) SetWeight

func (s *SynapseTabulated) SetWeight(k int, weight Deepfloat64)

func (*SynapseTabulated) SetWeights

func (s *SynapseTabulated) SetWeights(w []Deepfloat64)

func (*SynapseTabulated) Smooth

func (s *SynapseTabulated) Smooth()

func (*SynapseTabulated) String

func (s *SynapseTabulated) String() string

func (*SynapseTabulated) Trapolate

func (*SynapseTabulated) WeightsString

func (s *SynapseTabulated) WeightsString() string

type SynapseType

type SynapseType int
const (
	// SynapseAnalytic is an analytical function
	SynapseTypeAnalytic SynapseType = 0
	// SynapseTabulated is a tabulated function
	SynapseTypeTabulated SynapseType = 1
)

type Trainer

type Trainer interface {
	Train(n *Neural, examples, validation Examples, iterations int)
	SetPrefix(prefix string)
}

Trainer is a neural network trainer

Jump to

Keyboard shortcuts

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