goraph

package module
v0.0.0-...-50f73b2 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 7 Imported by: 0

README

Goraph

Goraph: A Go-Language-Based AI Library Built on Computational Graphs

Goraph is a newly developed artificial intelligence library entirely written in Go language, utilizing advanced computational graph technology to build and train machine learning models. Designed to provide high-performance, user-friendly, and scalable deep learning tools, Goraph takes full advantage of the concurrency and simplicity of the Go language.

Please note that it is currently an experimental project under development and not suitable for production use.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewKaimingNormalInit

func NewKaimingNormalInit(fanIn int) func() float64

func NewXavierNormalInit

func NewXavierNormalInit(fanIn, fanOut int) func() float64

func NewXavierUniformInit

func NewXavierUniformInit(fanIn, fanOut int) func() float64

Types

type AdamOptimizer

type AdamOptimizer struct {
	LearningRate float64
	Beta1        float64
	Beta2        float64
	M            []*Matrix
	V            []*Matrix
	T            int
	Eps          float64
	Parameters   []*VariableNode
}

func NewAdamOptimizer

func NewAdamOptimizer(parameters []*VariableNode, learningRate, beta1, beta2, eps float64) *AdamOptimizer

func (*AdamOptimizer) Reset

func (opt *AdamOptimizer) Reset()

func (*AdamOptimizer) Step

func (opt *AdamOptimizer) Step(batchSize int)

type AddNode

type AddNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

AddNode defines a node that performs matrix addition operations.

func Add

func Add(x Node, y Node) *AddNode

func (*AddNode) Backward

func (m *AddNode) Backward(grad *Matrix)

func (*AddNode) Forward

func (m *AddNode) Forward() *Matrix

func (*AddNode) Reset

func (m *AddNode) Reset()

func (*AddNode) Tag

func (m *AddNode) Tag(name string) Node

type ColSliceNode

type ColSliceNode struct {
	X          Node
	Start, End int
	Value      *Matrix
	Name       string
	// contains filtered or unexported fields
}

ColSliceNode defines a node that performs matrix slicing along the column direction.

func ColSlice

func ColSlice(x Node, start, end int) *ColSliceNode

func (*ColSliceNode) Backward

func (m *ColSliceNode) Backward(grad *Matrix)

func (*ColSliceNode) Forward

func (m *ColSliceNode) Forward() *Matrix

func (*ColSliceNode) Reset

func (m *ColSliceNode) Reset()

func (*ColSliceNode) Tag

func (m *ColSliceNode) Tag(name string) Node

type ColSumNode

type ColSumNode struct {
	X     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

func ColSum

func ColSum(x Node) *ColSumNode

func (*ColSumNode) Backward

func (m *ColSumNode) Backward(grad *Matrix)

func (*ColSumNode) Forward

func (m *ColSumNode) Forward() *Matrix

func (*ColSumNode) Reset

func (m *ColSumNode) Reset()

func (*ColSumNode) Tag

func (m *ColSumNode) Tag(name string) Node

type ConvNode

type ConvNode struct {
	X        Node
	Kernel   Node
	Stride   int
	Value    *Matrix
	XPadding int
	YPadding int
	Name     string
	// contains filtered or unexported fields
}

func Conv

func Conv(x Node, kernel Node, stride int) *ConvNode

func (*ConvNode) Backward

func (m *ConvNode) Backward(grad *Matrix)

func (*ConvNode) Forward

func (m *ConvNode) Forward() *Matrix

func (*ConvNode) Reset

func (m *ConvNode) Reset()

func (*ConvNode) Tag

func (m *ConvNode) Tag(name string) Node

type CrossEntropyLossNode

type CrossEntropyLossNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

CrossEntropyLossNode defines a node dedicated to calculating cross entropy loss. It should be used in conjunction with the SoftmaxNode, meaning that the preceding node of this one should be a SoftmaxNode.

func CrossEntropyLoss

func CrossEntropyLoss(x Node, y Node) *CrossEntropyLossNode

func (*CrossEntropyLossNode) Backward

func (m *CrossEntropyLossNode) Backward(grad *Matrix)

func (*CrossEntropyLossNode) Forward

func (m *CrossEntropyLossNode) Forward() *Matrix

func (*CrossEntropyLossNode) Reset

func (m *CrossEntropyLossNode) Reset()

func (*CrossEntropyLossNode) Tag

func (m *CrossEntropyLossNode) Tag(name string) Node

type DivElementNode

type DivElementNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

func Div

func Div(x Node, y Node) *DivElementNode

func (*DivElementNode) Backward

func (m *DivElementNode) Backward(grad *Matrix)

func (*DivElementNode) Forward

func (m *DivElementNode) Forward() *Matrix

func (*DivElementNode) Reset

func (m *DivElementNode) Reset()

func (*DivElementNode) Tag

func (m *DivElementNode) Tag(name string) Node

type DropoutNode

type DropoutNode struct {
	X     Node
	P     float64 //Keep probability
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

DropoutNode defines a node that performs Dropout operations.

func Dropout

func Dropout(x Node, p float64) *DropoutNode

func (*DropoutNode) Backward

func (m *DropoutNode) Backward(grad *Matrix)

func (*DropoutNode) Forward

func (m *DropoutNode) Forward() *Matrix

func (*DropoutNode) Reset

func (m *DropoutNode) Reset()

func (*DropoutNode) Tag

func (m *DropoutNode) Tag(name string) Node

type GradThresholdNode

type GradThresholdNode struct {
	X         Node
	Value     *Matrix
	Threshold float64
	Name      string
	// contains filtered or unexported fields
}

GradThresholdNode defines a processing node that, during forward propagation, does not perform any processing and directly passes the input to the next step. In backpropagation, it controls whether to continue propagation based on the set Threshold. When the module of the gradient is less than the Threshold, backpropagation will stop.

func GradThreshold

func GradThreshold(x Node, threshold float64) *GradThresholdNode

func (*GradThresholdNode) Backward

func (m *GradThresholdNode) Backward(grad *Matrix)

func (*GradThresholdNode) Forward

func (m *GradThresholdNode) Forward() *Matrix

func (*GradThresholdNode) Reset

func (m *GradThresholdNode) Reset()

func (*GradThresholdNode) Tag

func (m *GradThresholdNode) Tag(name string) Node

type HConcatNode

type HConcatNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

HConcatNode defines a node for matrix horizontal concatenation.

func HConcat

func HConcat(x Node, y Node) *HConcatNode

func (*HConcatNode) Backward

func (m *HConcatNode) Backward(grad *Matrix)

func (*HConcatNode) Forward

func (m *HConcatNode) Forward() *Matrix

func (*HConcatNode) Reset

func (m *HConcatNode) Reset()

func (*HConcatNode) Tag

func (m *HConcatNode) Tag(name string) Node

type L2Normalizer

type L2Normalizer struct {
	Dim    int     `json:"dim"`
	Groups [][]int `json:"groups"`
}

func NewL2Normalizer

func NewL2Normalizer(dim int, groups [][]int) *L2Normalizer

func (*L2Normalizer) Normalize

func (n *L2Normalizer) Normalize(data [][]float64) [][]float64

type LogNode

type LogNode struct {
	X     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

func Log

func Log(x Node) *LogNode

func (*LogNode) Backward

func (m *LogNode) Backward(grad *Matrix)

func (*LogNode) Forward

func (m *LogNode) Forward() *Matrix

func (*LogNode) Reset

func (m *LogNode) Reset()

func (*LogNode) Tag

func (m *LogNode) Tag(name string) Node

type MSELossNode

type MSELossNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

MSELossNode defines a node for calculating mean square error loss.

func MSELoss

func MSELoss(x Node, y Node) *MSELossNode

func (*MSELossNode) Backward

func (m *MSELossNode) Backward(grad *Matrix)

func (*MSELossNode) Forward

func (m *MSELossNode) Forward() *Matrix

func (*MSELossNode) Reset

func (m *MSELossNode) Reset()

func (*MSELossNode) Tag

func (m *MSELossNode) Tag(name string) Node

type Matrix

type Matrix struct {
	Data []float64 `json:"data"`
	Rows int       `json:"rows"`
	Cols int       `json:"cols"`
}

func NewConstMatrix

func NewConstMatrix(rows, cols int, value float64) *Matrix

func NewMatrix

func NewMatrix(rows, cols int, data []float64) *Matrix

func NewRandomMatrix

func NewRandomMatrix(rows, cols int, f func() float64) *Matrix

func (*Matrix) Add

func (m *Matrix) Add(other *Matrix) (result *Matrix)

func (*Matrix) ColSlice

func (m *Matrix) ColSlice(start, end int) *Matrix

func (*Matrix) ColSum

func (m *Matrix) ColSum() *Matrix

func (*Matrix) DivElement

func (m *Matrix) DivElement(other *Matrix) *Matrix

func (*Matrix) HConcat

func (m *Matrix) HConcat(other *Matrix) (result *Matrix)

func (*Matrix) Multi

func (m *Matrix) Multi(other *Matrix) (result *Matrix)

func (*Matrix) MultiElement

func (m *Matrix) MultiElement(other *Matrix) (result *Matrix)

func (*Matrix) Negate

func (m *Matrix) Negate() (result *Matrix)

func (*Matrix) Reshape

func (m *Matrix) Reshape(rows, cols int) *Matrix

func (*Matrix) RowSlice

func (m *Matrix) RowSlice(start, end int) *Matrix

func (*Matrix) RowSum

func (m *Matrix) RowSum() *Matrix

func (*Matrix) Scale

func (m *Matrix) Scale(rate float64) *Matrix

func (*Matrix) String

func (m *Matrix) String() string

func (*Matrix) Sub

func (m *Matrix) Sub(other *Matrix) (result *Matrix)

func (*Matrix) Trans

func (m *Matrix) Trans() (result *Matrix)

func (*Matrix) VConcat

func (m *Matrix) VConcat(other *Matrix) (result *Matrix)

type MinMaxScaler

type MinMaxScaler struct {
	Min    []float64 `json:"min"`
	Max    []float64 `json:"max"`
	Groups [][]int   `json:"groups"`
	Dim    int       `json:"dim"`
}

func NewMinMaxScaler

func NewMinMaxScaler(dim int, groups [][]int) *MinMaxScaler

func (*MinMaxScaler) Fit

func (m *MinMaxScaler) Fit(data [][]float64)

func (*MinMaxScaler) Transform

func (m *MinMaxScaler) Transform(data [][]float64) [][]float64

type Model

type Model struct {
	Parameters    []*VariableNode `json:"parameters"`
	InputScalers  []Scaler        `json:"input_scalers"`
	TargetScalers []Scaler        `json:"target_scalers"`
}

func NewModel

func NewModel(parameters []*VariableNode, inputScalers, targetScalers []Scaler) *Model

func (*Model) Load

func (m *Model) Load(filePath string) error

func (*Model) Save

func (m *Model) Save(filePath string) error

func (*Model) String

func (m *Model) String() string

type MultiElementNode

type MultiElementNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

MultiElementNode defines a node that performs matrix multiplication based on the corresponding elements.

func MultiElement

func MultiElement(x Node, y Node) *MultiElementNode

func (*MultiElementNode) Backward

func (m *MultiElementNode) Backward(grad *Matrix)

func (*MultiElementNode) Forward

func (m *MultiElementNode) Forward() *Matrix

func (*MultiElementNode) Reset

func (m *MultiElementNode) Reset()

func (*MultiElementNode) Tag

func (m *MultiElementNode) Tag(name string) Node

type MultiNode

type MultiNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

MultiNode defines a node that performs matrix multiplication operations.

func Multi

func Multi(x Node, y Node) *MultiNode

func (*MultiNode) Backward

func (m *MultiNode) Backward(grad *Matrix)

func (*MultiNode) Forward

func (m *MultiNode) Forward() *Matrix

func (*MultiNode) Reset

func (m *MultiNode) Reset()

func (*MultiNode) Tag

func (m *MultiNode) Tag(name string) Node

type NeuralNetwork

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

func NewNeuralNetwork

func NewNeuralNetwork(
	buildFunc func() (input, target *VariableNode, output, loss Node),
	optimizer Optimizer) *NeuralNetwork

func (*NeuralNetwork) Evaluate

func (nn *NeuralNetwork) Evaluate(inputData, targetData [][]float64) (lossValue float64, outputData [][]float64)

func (*NeuralNetwork) Predict

func (nn *NeuralNetwork) Predict(inputData []float64) (outputData []float64)

func (*NeuralNetwork) Train

func (nn *NeuralNetwork) Train(inputData, targetData [][]float64, batchSize int) (lossValue float64)

type Node

type Node interface {
	Backward(grad *Matrix)
	Forward() *Matrix
	Reset()
	Tag(name string) Node
}

Node defines the interface for computing graph nodes.

type Normalizer

type Normalizer interface {
	Normalize([][]float64) [][]float64
}

type Optimizer

type Optimizer interface {
	Step(batchSize int)
	Reset()
}

type PoolNode

type PoolNode struct {
	X      Node
	Width  int
	Height int
	Stride int
	Value  *Matrix
	Flags  []int
	Name   string
	// contains filtered or unexported fields
}

func Pool

func Pool(x Node, width, height, stride int) *PoolNode

func (*PoolNode) Backward

func (m *PoolNode) Backward(grad *Matrix)

func (*PoolNode) Forward

func (m *PoolNode) Forward() *Matrix

func (*PoolNode) Reset

func (m *PoolNode) Reset()

func (*PoolNode) Tag

func (m *PoolNode) Tag(name string) Node

type ReLuNode

type ReLuNode struct {
	X     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

ReLuNode defines a node that executes ReLu activation function.

func ReLu

func ReLu(x Node) *ReLuNode

func (*ReLuNode) Backward

func (m *ReLuNode) Backward(grad *Matrix)

func (*ReLuNode) Forward

func (m *ReLuNode) Forward() *Matrix

func (*ReLuNode) Reset

func (m *ReLuNode) Reset()

func (*ReLuNode) Tag

func (m *ReLuNode) Tag(name string) Node

type ReshapeNode

type ReshapeNode struct {
	X     Node
	Rows  int
	Cols  int
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

func Reshape

func Reshape(x Node, rows, cols int) *ReshapeNode

func (*ReshapeNode) Backward

func (m *ReshapeNode) Backward(grad *Matrix)

func (*ReshapeNode) Forward

func (m *ReshapeNode) Forward() *Matrix

func (*ReshapeNode) Reset

func (m *ReshapeNode) Reset()

func (*ReshapeNode) Tag

func (m *ReshapeNode) Tag(name string) Node

type RobustScaler

type RobustScaler struct {
	Median []float64 `json:"median"`
	IQR    []float64 `json:"IQR"`
	Groups [][]int   `json:"groups"`
	Dim    int       `json:"dim"`
}

func NewRobustScaler

func NewRobustScaler(dim int, groups [][]int) *RobustScaler

func (*RobustScaler) Fit

func (m *RobustScaler) Fit(data [][]float64)

func (*RobustScaler) Transform

func (m *RobustScaler) Transform(data [][]float64) [][]float64

type RowSliceNode

type RowSliceNode struct {
	X          Node
	Start, End int
	Value      *Matrix
	Name       string
	// contains filtered or unexported fields
}

RowSliceNode defines a node that performs matrix slicing along row direction.

func RowSlice

func RowSlice(x Node, start, end int) *RowSliceNode

func (*RowSliceNode) Backward

func (m *RowSliceNode) Backward(grad *Matrix)

func (*RowSliceNode) Forward

func (m *RowSliceNode) Forward() *Matrix

func (*RowSliceNode) Reset

func (m *RowSliceNode) Reset()

func (*RowSliceNode) Tag

func (m *RowSliceNode) Tag(name string) Node

type RowSumNode

type RowSumNode struct {
	X     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

func RowSum

func RowSum(x Node) *RowSumNode

func (*RowSumNode) Backward

func (m *RowSumNode) Backward(grad *Matrix)

func (*RowSumNode) Forward

func (m *RowSumNode) Forward() *Matrix

func (*RowSumNode) Reset

func (m *RowSumNode) Reset()

func (*RowSumNode) Tag

func (m *RowSumNode) Tag(name string) Node

type SGDOptimizer

type SGDOptimizer struct {
	LearningRate float64
	Momentum     float64
	Velocity     []*Matrix
	Parameters   []*VariableNode
}

func NewSGDOptimizer

func NewSGDOptimizer(parameters []*VariableNode, learningRate, momentum float64) *SGDOptimizer

func (*SGDOptimizer) Reset

func (opt *SGDOptimizer) Reset()

func (*SGDOptimizer) Step

func (opt *SGDOptimizer) Step(batchSize int)

type ScaleNode

type ScaleNode struct {
	X     Node
	Rate  float64
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

func Scale

func Scale(x Node, rate float64) *ScaleNode

func (*ScaleNode) Backward

func (m *ScaleNode) Backward(grad *Matrix)

func (*ScaleNode) Forward

func (m *ScaleNode) Forward() *Matrix

func (*ScaleNode) Reset

func (m *ScaleNode) Reset()

func (*ScaleNode) Tag

func (m *ScaleNode) Tag(name string) Node

type Scaler

type Scaler interface {
	Fit(data [][]float64)
	Transform(data [][]float64) [][]float64
}

type SigmoidNode

type SigmoidNode struct {
	X     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

SigmoidNode defines a node that executes Sigmoid activation function.

func Sigmoid

func Sigmoid(x Node) *SigmoidNode

func (*SigmoidNode) Backward

func (m *SigmoidNode) Backward(grad *Matrix)

func (*SigmoidNode) Forward

func (m *SigmoidNode) Forward() *Matrix

func (*SigmoidNode) Reset

func (m *SigmoidNode) Reset()

func (*SigmoidNode) Tag

func (m *SigmoidNode) Tag(name string) Node

type SoftmaxNode

type SoftmaxNode struct {
	X     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

SoftmaxNode defines a node that executes the Softmax activation function

func Softmax

func Softmax(x Node) *SoftmaxNode

func (*SoftmaxNode) Backward

func (m *SoftmaxNode) Backward(grad *Matrix)

func (*SoftmaxNode) Forward

func (m *SoftmaxNode) Forward() *Matrix

func (*SoftmaxNode) Reset

func (m *SoftmaxNode) Reset()

func (*SoftmaxNode) Tag

func (m *SoftmaxNode) Tag(name string) Node

type SubNode

type SubNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

SubNode defines a node that performs matrix subtraction operations.

func Sub

func Sub(x Node, y Node) *SubNode

func (*SubNode) Backward

func (m *SubNode) Backward(grad *Matrix)

func (*SubNode) Forward

func (m *SubNode) Forward() *Matrix

func (*SubNode) Reset

func (m *SubNode) Reset()

func (*SubNode) Tag

func (m *SubNode) Tag(name string) Node

type TanhNode

type TanhNode struct {
	X     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

TanhNode defines a node that executes Tanh activation function.

func Tanh

func Tanh(x Node) *TanhNode

func (*TanhNode) Backward

func (m *TanhNode) Backward(grad *Matrix)

func (*TanhNode) Forward

func (m *TanhNode) Forward() *Matrix

func (*TanhNode) Reset

func (m *TanhNode) Reset()

func (*TanhNode) Tag

func (m *TanhNode) Tag(name string) Node

type TransNode

type TransNode struct {
	X     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

func Trans

func Trans(x Node) *TransNode

func (*TransNode) Backward

func (m *TransNode) Backward(grad *Matrix)

func (*TransNode) Forward

func (m *TransNode) Forward() *Matrix

func (*TransNode) Reset

func (m *TransNode) Reset()

func (*TransNode) Tag

func (m *TransNode) Tag(name string) Node

type VConcatNode

type VConcatNode struct {
	X     Node
	Y     Node
	Value *Matrix
	Name  string
	// contains filtered or unexported fields
}

VConcatNode defines a node for matrix vertical concatenation.

func VConcat

func VConcat(x Node, y Node) *VConcatNode

func (*VConcatNode) Backward

func (m *VConcatNode) Backward(grad *Matrix)

func (*VConcatNode) Forward

func (m *VConcatNode) Forward() *Matrix

func (*VConcatNode) Reset

func (m *VConcatNode) Reset()

func (*VConcatNode) Tag

func (m *VConcatNode) Tag(name string) Node

type ValueThresholdNode

type ValueThresholdNode struct {
	X        Node
	Value    *Matrix
	MinValue float64
	MaxValue float64
	Name     string
	// contains filtered or unexported fields
}

func ValueThreshold

func ValueThreshold(x Node, minVal, maxVal float64) *ValueThresholdNode

func (*ValueThresholdNode) Backward

func (m *ValueThresholdNode) Backward(grad *Matrix)

func (*ValueThresholdNode) Forward

func (m *ValueThresholdNode) Forward() *Matrix

func (*ValueThresholdNode) Reset

func (m *ValueThresholdNode) Reset()

func (*ValueThresholdNode) Tag

func (m *ValueThresholdNode) Tag(name string) Node

type VariableNode

type VariableNode struct {
	Name     string  `json:"name"`
	Value    *Matrix `json:"value"`
	Gradient *Matrix `json:"-"`
	// contains filtered or unexported fields
}

VariableNode defines a variable node.

func NewConstVariable

func NewConstVariable(rows, cols int, value float64) *VariableNode

func NewRandomVariable

func NewRandomVariable(rows, cols int, f func() float64) *VariableNode

func NewVariable

func NewVariable(rows, cols int, data []float64) *VariableNode

func (*VariableNode) Backward

func (v *VariableNode) Backward(grad *Matrix)

func (*VariableNode) Forward

func (v *VariableNode) Forward() *Matrix

func (*VariableNode) Reset

func (v *VariableNode) Reset()

func (*VariableNode) Tag

func (v *VariableNode) Tag(name string) Node

type ZScoreScaler

type ZScoreScaler struct {
	Mean         []float64 `json:"mean"`
	StdDeviation []float64 `json:"stdDeviation"`
	Groups       [][]int   `json:"groups"`
	Dim          int       `json:"dim"`
}

func NewZScoreScaler

func NewZScoreScaler(dim int, groups [][]int) *ZScoreScaler

func (*ZScoreScaler) Fit

func (m *ZScoreScaler) Fit(data [][]float64)

func (*ZScoreScaler) Transform

func (m *ZScoreScaler) Transform(data [][]float64) [][]float64

Jump to

Keyboard shortcuts

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