Documentation
¶
Overview ¶
Package graph provides a computational graph abstraction. Package graph provides a computational graph abstraction.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidInputCount = errors.New("invalid number of input tensors")
ErrInvalidInputCount is returned when the number of input tensors is invalid.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
Builder provides a fluent API for constructing a computation graph.
func NewBuilder ¶
NewBuilder creates a new graph builder.
func (*Builder[T]) Build ¶
func (b *Builder[T]) Build(outputNode Node[T]) (func(inputs ...*tensor.Tensor[T]) (*tensor.Tensor[T], error), func(initialGradient *tensor.Tensor[T]) error, error)
Build constructs the final graph and returns forward and backward functions.
func (*Builder[T]) Parameters ¶
Parameters returns all the trainable parameters in the graph.
type Graph ¶
Graph represents a computation graph with a defined execution order.
func (*Graph[T]) Parameters ¶
Parameters returns all the trainable parameters in the graph.
type NoParameters ¶
NoParameters is a helper struct for layers that have no parameters. It provides a default implementation of the Parameters() method.
func (*NoParameters[T]) Parameters ¶
func (np *NoParameters[T]) Parameters() []*Parameter[T]
Parameters returns an empty slice of parameters.
type Node ¶
type Node[T tensor.Numeric] interface { // OutputShape returns the shape of the output tensor. OutputShape() []int // Forward computes the output of the node given the inputs. Forward(inputs ...*tensor.Tensor[T]) (*tensor.Tensor[T], error) // Backward computes the gradients of the loss with respect to the inputs and parameters. Backward(outputGradient *tensor.Tensor[T]) ([]*tensor.Tensor[T], error) // Parameters returns the parameters of the node. Parameters() []*Parameter[T] }
Node represents a node in the computation graph.
type Parameter ¶
type Parameter[T tensor.Numeric] struct { Name string Value *tensor.Tensor[T] Gradient *tensor.Tensor[T] }
Parameter is a container for a trainable tensor (e.g., weights or biases). It holds both the tensor for its value and a tensor for its gradient.
func NewParameter ¶
func NewParameter[T tensor.Numeric](name string, value *tensor.Tensor[T], newTensorFn func(shape []int, data []T) (*tensor.Tensor[T], error)) (*Parameter[T], error)
NewParameter creates a new parameter, initializing its gradient tensor with the same shape. It takes a tensor creation function to allow for mocking in tests.
func (*Parameter[T]) AddGradient ¶
AddGradient accumulates the given gradient to the parameter's gradient.
func (*Parameter[T]) ClearGradient ¶
func (p *Parameter[T]) ClearGradient()
ClearGradient sets the parameter's gradient to zero.