core

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Overview

Package core provides core neural network layer implementations.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core neural network layer implementations.

Package core provides core neural network layer implementations.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides the Shape layer for the Zerfoo ML framework.

Package core provides core neural network layer implementations.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core layer implementations for the Zerfoo ML framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildCast added in v0.3.0

func BuildCast[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildCast constructs a Cast node.

func BuildConcat added in v0.3.0

func BuildConcat[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildConcat constructs a Concat node, extracting the axis from attributes.

func BuildMatMul added in v0.3.0

func BuildMatMul[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildMatMul constructs a new MatMul node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildMul added in v0.3.0

func BuildMul[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildMul constructs a new Mul node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildReshape added in v0.3.0

func BuildReshape[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildReshape constructs a Reshape node, parsing target shape from attributes.

func BuildRotaryEmbedding added in v0.3.0

func BuildRotaryEmbedding[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	name string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildRotaryEmbedding constructs a new RotaryPositionalEmbedding node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildShape added in v0.3.0

func BuildShape[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildShape constructs a new Shape node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildSpectralFingerprint added in v0.3.0

func BuildSpectralFingerprint[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildSpectralFingerprint constructs a SpectralFingerprint node from attributes. Required attributes: - "window" (int): window length to analyze (must be > 1) - "top_k" (int): number of non-DC bins to output (must be > 0).

func BuildSub added in v0.3.0

func BuildSub[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildSub constructs a new Sub node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildUnsqueeze added in v0.3.0

func BuildUnsqueeze[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildUnsqueeze constructs a new Unsqueeze node for the given compute engine. It parses optional "axes" attributes from the attributes map.

Types

type Add added in v0.3.0

type Add[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Add represents an element-wise addition node.

func NewAdd added in v0.3.0

func NewAdd[T tensor.Numeric](engine compute.Engine[T]) *Add[T]

NewAdd creates a new Add node.

func (*Add[T]) Attributes added in v0.3.0

func (a *Add[T]) Attributes() map[string]any

Attributes returns the attributes of the Add node.

func (*Add[T]) Backward added in v0.3.0

func (a *Add[T]) Backward(_ context.Context, mode types.BackwardMode, dOut *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the backward pass of the Add node.

func (*Add[T]) Forward added in v0.3.0

func (a *Add[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass of the Add node.

func (*Add[T]) OpType added in v0.3.0

func (a *Add[T]) OpType() string

OpType returns the operator type of the Add node.

func (*Add[T]) OutputShape added in v0.3.0

func (a *Add[T]) OutputShape() []int

OutputShape returns the output shape of the Add node.

func (*Add[T]) Parameters added in v0.3.0

func (a *Add[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the Add node.

type Bias

type Bias[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Bias adds a bias vector to its input.

func NewBias

func NewBias[T tensor.Numeric](name string, engine compute.Engine[T], ops numeric.Arithmetic[T], size int, opts ...BiasOption[T]) (*Bias[T], error)

NewBias creates a new Bias layer with default tensor and parameter creation.

func NewBiasFromParam added in v0.3.0

func NewBiasFromParam[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], biases *graph.Parameter[T]) *Bias[T]

NewBiasFromParam creates a new Bias layer from an existing biases parameter.

func NewBiasWithFactories

func NewBiasWithFactories[T tensor.Numeric](
	name string, engine compute.Engine[T], ops numeric.Arithmetic[T], size int,
	newTensor func([]int, []T) (*tensor.TensorNumeric[T], error),
	newParameter func(string, *tensor.TensorNumeric[T], func([]int, []T) (*tensor.TensorNumeric[T], error)) (*graph.Parameter[T], error),
	initializer func(size int) []T,
) (*Bias[T], error)

NewBiasWithFactories creates a new Bias layer with custom tensor and parameter creation functions.

func (*Bias[T]) Attributes added in v0.3.0

func (b *Bias[T]) Attributes() map[string]interface{}

Attributes returns nil for the Bias layer.

func (*Bias[T]) Backward

func (b *Bias[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients.

func (*Bias[T]) Forward

func (b *Bias[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the forward pass: output = input + biases.

func (*Bias[T]) OpType added in v0.3.0

func (b *Bias[T]) OpType() string

OpType returns the operation type of the Bias layer.

func (*Bias[T]) OutputShape

func (b *Bias[T]) OutputShape() []int

OutputShape returns the output shape of the Bias layer.

func (*Bias[T]) Parameters

func (b *Bias[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the Bias layer.

func (*Bias[T]) SetName

func (b *Bias[T]) SetName(name string)

SetName sets the name of the Bias layer.

type BiasOption added in v0.3.0

type BiasOption[T tensor.Numeric] func(*BiasOptions[T])

BiasOption is a function that applies an option to BiasOptions.

func WithBiasInitializer added in v0.3.0

func WithBiasInitializer[T tensor.Numeric](initializer func(size int) []T) BiasOption[T]

WithBiasInitializer sets a custom initializer for the bias vector.

type BiasOptions added in v0.3.0

type BiasOptions[T tensor.Numeric] struct {
	Initializer func(size int) []T
}

BiasOptions holds configuration options for the Bias layer.

type Cast added in v0.3.0

type Cast[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Cast is a layer that converts tensor elements to a different data type.

func NewCast added in v0.3.0

func NewCast[T tensor.Numeric](engine compute.Engine[T]) *Cast[T]

NewCast creates a new Cast layer.

func (*Cast[T]) Attributes added in v0.3.0

func (c *Cast[T]) Attributes() map[string]interface{}

Attributes returns nil for the Cast layer.

func (*Cast[T]) Backward added in v0.3.0

func (c *Cast[T]) Backward(_ context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Cast layer.

func (*Cast[T]) Forward added in v0.3.0

func (c *Cast[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the cast operation (for same type, this is essentially a copy).

func (*Cast[T]) OpType added in v0.3.0

func (c *Cast[T]) OpType() string

OpType returns the operation type of the Cast layer.

func (*Cast[T]) OutputShape added in v0.3.0

func (c *Cast[T]) OutputShape() []int

OutputShape returns the output shape of the Cast layer.

func (*Cast[T]) Parameters added in v0.3.0

func (c *Cast[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Cast layer.

type Concat added in v0.3.0

type Concat[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Concat is a layer that concatenates multiple tensors along a specified axis.

func NewConcat added in v0.3.0

func NewConcat[T tensor.Numeric](engine compute.Engine[T], axis int) *Concat[T]

NewConcat creates a new Concat layer.

func (*Concat[T]) Attributes added in v0.3.0

func (c *Concat[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the Concat layer.

func (*Concat[T]) Backward added in v0.3.0

func (c *Concat[T]) Backward(_ context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Concat layer.

func (*Concat[T]) Forward added in v0.3.0

func (c *Concat[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the concatenation of input tensors along the specified axis.

func (*Concat[T]) OpType added in v0.3.0

func (c *Concat[T]) OpType() string

OpType returns the operation type of the Concat layer.

func (*Concat[T]) OutputShape added in v0.3.0

func (c *Concat[T]) OutputShape() []int

OutputShape returns the output shape of the Concat layer.

func (*Concat[T]) Parameters added in v0.3.0

func (c *Concat[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Concat layer.

type Dense

type Dense[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Dense is a fully connected layer that combines a linear transformation and a bias.

func NewDense

func NewDense[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	inputSize, outputSize int,
	opts ...DenseOption[T],
) (*Dense[T], error)

NewDense creates a new Dense layer.

func NewDenseFromParams added in v0.3.0

func NewDenseFromParams[T tensor.Numeric](linear *Linear[T], bias *Bias[T]) *Dense[T]

NewDenseFromParams creates a new Dense layer from existing parameters.

func (*Dense[T]) Attributes added in v0.3.0

func (d *Dense[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the Dense layer.

func (*Dense[T]) Backward

func (d *Dense[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients.

func (*Dense[T]) Forward

func (d *Dense[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the forward pass: output = input*weights + biases.

func (*Dense[T]) OpType added in v0.3.0

func (d *Dense[T]) OpType() string

OpType returns the operation type of the Dense layer.

func (*Dense[T]) OutputShape

func (d *Dense[T]) OutputShape() []int

OutputShape returns the output shape of the Dense layer.

func (*Dense[T]) Parameters

func (d *Dense[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the Dense layer.

func (*Dense[T]) SetName

func (d *Dense[T]) SetName(name string)

SetName sets the name of the Dense layer.

type DenseOption added in v0.3.0

type DenseOption[T tensor.Numeric] func(*DenseOptions[T])

DenseOption is a function that applies an option to DenseOptions.

func WithBias added in v0.3.0

func WithBias[T tensor.Numeric](withBias bool) DenseOption[T]

WithBias sets whether the Dense layer should include a bias.

type DenseOptions added in v0.3.0

type DenseOptions[T tensor.Numeric] struct {
	WithBias bool
}

DenseOptions holds configuration options for the Dense layer.

type FFN added in v0.3.0

type FFN[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

FFN (Feed-Forward Network) implements a two-layer MLP with SwiGLU activation.

func NewFFN added in v0.3.0

func NewFFN[T tensor.Numeric](name string, engine compute.Engine[T], ops numeric.Arithmetic[T], inputDim, hiddenDim, outputDim int, opts ...FFNOption[T]) (*FFN[T], error)

NewFFN creates a new Feed-Forward Network layer. name: The name of the FFN layer. engine: The compute engine to use for tensor operations. ops: Numeric operations for the given type T. inputDim: The input dimension of the FFN. hiddenDim: The dimension of the hidden layer. outputDim: The output dimension of the FFN.

func (*FFN[T]) Attributes added in v0.3.0

func (f *FFN[T]) Attributes() map[string]interface{}

Attributes returns the attributes.

func (*FFN[T]) Backward added in v0.3.0

func (f *FFN[T]) Backward(ctx context.Context, mode types.BackwardMode, dOut *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the backward pass of the FFN.

func (*FFN[T]) Engine added in v0.3.0

func (f *FFN[T]) Engine() compute.Engine[T]

Engine returns the compute engine of the FFN layer.

func (*FFN[T]) Forward added in v0.3.0

func (f *FFN[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass of the FFN.

func (*FFN[T]) OpType added in v0.3.0

func (f *FFN[T]) OpType() string

OpType returns the operation type.

func (*FFN[T]) OutputShape added in v0.3.0

func (f *FFN[T]) OutputShape() []int

OutputShape returns the output shape of the FFN layer.

func (*FFN[T]) Parameters added in v0.3.0

func (f *FFN[T]) Parameters() []*graph.Parameter[T]

Parameters returns the trainable parameters of the FFN layer.

type FFNOption added in v0.3.0

type FFNOption[T tensor.Numeric] func(*FFNOptions[T])

FFNOption is a function that applies an option to FFNOptions.

func WithFFNBias added in v0.3.0

func WithFFNBias[T tensor.Numeric](withBias bool) FFNOption[T]

WithFFNBias sets whether the FFN layer should include bias terms.

func WithFFNInitializer added in v0.3.0

func WithFFNInitializer[T tensor.Numeric](initializer components.WeightInitializer[T]) FFNOption[T]

WithFFNInitializer sets a custom weight initializer for the FFN layer.

type FFNOptions added in v0.3.0

type FFNOptions[T tensor.Numeric] struct {
	Initializer components.WeightInitializer[T]
	WithBias    bool
}

FFNOptions holds configuration options for the FFN layer.

type FiLM added in v0.3.0

type FiLM[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

FiLM (Feature-wise Linear Modulation) layer. It takes a feature tensor and a context tensor as input. It generates scale and bias vectors from the context tensor and applies them to the feature tensor. Output = (feature * scale) + bias

func NewFiLM added in v0.3.0

func NewFiLM[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	contextDim, featureDim int,
) (*FiLM[T], error)

NewFiLM creates a new FiLM layer.

func (*FiLM[T]) Attributes added in v0.3.0

func (f *FiLM[T]) Attributes() map[string]interface{}

Attributes returns nil for the FiLM layer.

func (*FiLM[T]) Backward added in v0.3.0

func (f *FiLM[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the FiLM layer.

func (*FiLM[T]) Forward added in v0.3.0

func (f *FiLM[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the forward pass for the FiLM layer. It expects two inputs: inputs[0] is the feature tensor, inputs[1] is the context tensor.

func (*FiLM[T]) OpType added in v0.3.0

func (f *FiLM[T]) OpType() string

OpType returns the operation type of the FiLM layer.

func (*FiLM[T]) OutputShape added in v0.3.0

func (f *FiLM[T]) OutputShape() []int

OutputShape returns the output shape of the FiLM layer.

func (*FiLM[T]) Parameters added in v0.3.0

func (f *FiLM[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the FiLM layer.

type LMHead added in v0.3.0

type LMHead[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

LMHead is a linear layer that maps hidden states to vocabulary logits.

func NewLMHead added in v0.3.0

func NewLMHead[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], hiddenDim, vocabSize int) (*LMHead[T], error)

NewLMHead creates a new LMHead.

func (*LMHead[T]) Backward added in v0.3.0

func (h *LMHead[T]) Backward(ctx context.Context, mode types.BackwardMode, dOut *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the backward pass of the LMHead.

func (*LMHead[T]) Forward added in v0.3.0

func (h *LMHead[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass of the LMHead.

func (*LMHead[T]) OutputShape added in v0.3.0

func (h *LMHead[T]) OutputShape() []int

OutputShape returns the output shape of the LMHead.

func (*LMHead[T]) Parameters added in v0.3.0

func (h *LMHead[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the LMHead.

func (*LMHead[T]) SetWeights added in v0.3.0

func (h *LMHead[T]) SetWeights(weights *tensor.TensorNumeric[T])

SetWeights sets the weights of the LMHead. This is useful for sharing weights with a token embedding layer.

type Linear

type Linear[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Linear performs a linear transformation: output = input * weights. Uses component-based architecture for better modularity and testability.

func NewLinear

func NewLinear[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	inputSize, outputSize int,
	opts ...LinearOption[T],
) (*Linear[T], error)

NewLinear creates a new Linear layer.

func NewLinearFromParam added in v0.3.0

func NewLinearFromParam[T tensor.Numeric](engine compute.Engine[T], weights *graph.Parameter[T]) *Linear[T]

NewLinearFromParam creates a new Linear layer from an existing weights parameter.

func NewLinearWithFactories

func NewLinearWithFactories[T tensor.Numeric](
	name string, engine compute.Engine[T], _ numeric.Arithmetic[T], inputSize, outputSize int,
	initializer components.WeightInitializer[T],
	newTensor func([]int, []T) (*tensor.TensorNumeric[T], error),
	newParameter func(string, *tensor.TensorNumeric[T], func([]int, []T) (*tensor.TensorNumeric[T], error)) (*graph.Parameter[T], error),
) (*Linear[T], error)

NewLinearWithFactories creates a new Linear layer with custom tensor and parameter creation functions.

func (*Linear[T]) Attributes added in v0.3.0

func (l *Linear[T]) Attributes() map[string]interface{}

Attributes returns nil for the Linear layer.

func (*Linear[T]) Backward

func (l *Linear[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients using the gradient computer component.

func (*Linear[T]) Forward

func (l *Linear[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the forward pass: output = input * weights.

func (*Linear[T]) OpType added in v0.3.0

func (l *Linear[T]) OpType() string

OpType returns the operation type of the Linear layer.

func (*Linear[T]) OutputShape

func (l *Linear[T]) OutputShape() []int

OutputShape returns the output shape of the Linear layer.

func (*Linear[T]) Parameters

func (l *Linear[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the Linear layer.

func (*Linear[T]) SetName

func (l *Linear[T]) SetName(name string)

SetName sets the name of the Linear layer.

type LinearOption added in v0.3.0

type LinearOption[T tensor.Numeric] func(*LinearOptions[T])

LinearOption is a function that applies an option to LinearOptions.

func WithHe added in v0.3.0

func WithHe[T tensor.Numeric](ops numeric.Arithmetic[T]) LinearOption[T]

WithHe is an option to use He weight initialization.

func WithInitializer added in v0.3.0

func WithInitializer[T tensor.Numeric](initializer components.WeightInitializer[T]) LinearOption[T]

WithInitializer sets a custom weight initializer for the Linear layer.

func WithUniform added in v0.3.0

func WithUniform[T tensor.Numeric](ops numeric.Arithmetic[T], scale float64) LinearOption[T]

WithUniform is an option to use Uniform weight initialization.

func WithXavier added in v0.3.0

func WithXavier[T tensor.Numeric](ops numeric.Arithmetic[T]) LinearOption[T]

WithXavier is an option to use Xavier weight initialization.

type LinearOptions added in v0.3.0

type LinearOptions[T tensor.Numeric] struct {
	Initializer components.WeightInitializer[T]
}

LinearOptions holds configuration options for the Linear layer.

type MatMul added in v0.3.0

type MatMul[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

MatMul is a layer that performs matrix multiplication of two tensors.

func NewMatMul added in v0.3.0

func NewMatMul[T tensor.Numeric](engine compute.Engine[T]) *MatMul[T]

NewMatMul creates a new MatMul layer.

func (*MatMul[T]) Attributes added in v0.3.0

func (m *MatMul[T]) Attributes() map[string]interface{}

Attributes returns nil for the MatMul layer.

func (*MatMul[T]) Backward added in v0.3.0

func (m *MatMul[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the MatMul layer.

func (*MatMul[T]) Forward added in v0.3.0

func (m *MatMul[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the matrix multiplication.

func (*MatMul[T]) OpType added in v0.3.0

func (m *MatMul[T]) OpType() string

OpType returns the operation type of the MatMul layer.

func (*MatMul[T]) OutputShape added in v0.3.0

func (m *MatMul[T]) OutputShape() []int

OutputShape returns the output shape of the MatMul layer.

func (*MatMul[T]) Parameters added in v0.3.0

func (m *MatMul[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the MatMul layer.

type Mul added in v0.3.0

type Mul[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Mul is a layer that performs element-wise multiplication of two tensors.

func NewMul added in v0.3.0

func NewMul[T tensor.Numeric](engine compute.Engine[T]) *Mul[T]

NewMul creates a new Mul layer.

func (*Mul[T]) Attributes added in v0.3.0

func (m *Mul[T]) Attributes() map[string]interface{}

Attributes returns nil for the Mul layer.

func (*Mul[T]) Backward added in v0.3.0

func (m *Mul[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Mul layer.

func (*Mul[T]) Forward added in v0.3.0

func (m *Mul[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the element-wise multiplication of two input tensors.

func (*Mul[T]) OpType added in v0.3.0

func (m *Mul[T]) OpType() string

OpType returns the operation type of the Mul layer.

func (*Mul[T]) OutputShape added in v0.3.0

func (m *Mul[T]) OutputShape() []int

OutputShape returns the output shape of the Mul layer.

func (*Mul[T]) Parameters added in v0.3.0

func (m *Mul[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Mul layer.

type PolynomialExpansion

type PolynomialExpansion[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

PolynomialExpansion layer transforms input features into polynomial combinations up to a specified degree. This is useful for capturing non-linear relationships in data through feature engineering.

For input [x1, x2] with degree 2, it generates: [1, x1, x2, x1^2, x1*x2, x2^2]

The layer supports: - Configurable polynomial degree - Optional bias term (constant 1) - Interaction terms between features - Efficient computation using tensor operations.

func NewPolynomialExpansion

func NewPolynomialExpansion[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	inputSize int,
	options ...PolynomialExpansionOption[T],
) (*PolynomialExpansion[T], error)

NewPolynomialExpansion creates a new polynomial expansion layer.

Parameters: - name: layer name (currently not used but kept for consistency) - engine: compute engine for tensor operations - ops: arithmetic operations for the numeric type - inputSize: number of input features - options: functional options for configuration

Default values: - degree: 2 - includeBias: true

Returns the polynomial expansion layer or an error if parameters are invalid.

func (*PolynomialExpansion[T]) Attributes added in v0.3.0

func (p *PolynomialExpansion[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the PolynomialExpansion layer.

func (*PolynomialExpansion[T]) Backward

func (p *PolynomialExpansion[T]) Backward(_ context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes gradients for the polynomial expansion layer. This computes the derivative of each polynomial term with respect to the input features.

func (*PolynomialExpansion[T]) Forward

func (p *PolynomialExpansion[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the polynomial expansion transformation. Input shape: [batch_size, input_size] Output shape: [batch_size, output_size].

func (*PolynomialExpansion[T]) GetDegree

func (p *PolynomialExpansion[T]) GetDegree() int

GetDegree returns the polynomial degree of the layer.

func (*PolynomialExpansion[T]) GetInputSize

func (p *PolynomialExpansion[T]) GetInputSize() int

GetInputSize returns the input size of the layer.

func (*PolynomialExpansion[T]) GetOutputSize

func (p *PolynomialExpansion[T]) GetOutputSize() int

GetOutputSize returns the output size of the layer.

func (*PolynomialExpansion[T]) GetTermIndices

func (p *PolynomialExpansion[T]) GetTermIndices() [][]int

GetTermIndices returns the polynomial term indices for inspection/debugging.

func (*PolynomialExpansion[T]) HasBias

func (p *PolynomialExpansion[T]) HasBias() bool

HasBias returns whether the layer includes a bias term.

func (*PolynomialExpansion[T]) OpType added in v0.3.0

func (p *PolynomialExpansion[T]) OpType() string

OpType returns the operation type of the PolynomialExpansion layer.

func (*PolynomialExpansion[T]) OutputShape

func (p *PolynomialExpansion[T]) OutputShape() []int

OutputShape returns the shape of the output tensor.

func (*PolynomialExpansion[T]) Parameters

func (p *PolynomialExpansion[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the layer. Polynomial expansion has no trainable parameters.

func (*PolynomialExpansion[T]) SetName

func (p *PolynomialExpansion[T]) SetName(_ string)

SetName sets the name of the layer (for consistency with other layers). SetName sets the name of the layer (for consistency with other layers).

type PolynomialExpansionOption added in v0.3.0

type PolynomialExpansionOption[T tensor.Numeric] func(*PolynomialExpansionOptions[T])

PolynomialExpansionOption is a function that configures PolynomialExpansionOptions.

func WithPolynomialBias added in v0.3.0

func WithPolynomialBias[T tensor.Numeric](includeBias bool) PolynomialExpansionOption[T]

WithPolynomialBias sets whether to include a bias term (constant 1).

func WithPolynomialDegree added in v0.3.0

func WithPolynomialDegree[T tensor.Numeric](degree int) PolynomialExpansionOption[T]

WithPolynomialDegree sets the maximum polynomial degree.

type PolynomialExpansionOptions added in v0.3.0

type PolynomialExpansionOptions[T tensor.Numeric] struct {
	Degree      int
	IncludeBias bool
}

PolynomialExpansionOptions holds configuration options for PolynomialExpansion layer.

type Reshape added in v0.3.0

type Reshape[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Reshape is a layer that changes the shape of a tensor without changing its data.

func NewReshape added in v0.3.0

func NewReshape[T tensor.Numeric](engine compute.Engine[T], targetShape []int) *Reshape[T]

NewReshape creates a new Reshape layer.

func (*Reshape[T]) Attributes added in v0.3.0

func (r *Reshape[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the Reshape layer.

func (*Reshape[T]) Backward added in v0.3.0

func (r *Reshape[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Reshape layer.

func (*Reshape[T]) Forward added in v0.3.0

func (r *Reshape[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the reshape operation.

func (*Reshape[T]) OpType added in v0.3.0

func (r *Reshape[T]) OpType() string

OpType returns the operation type of the Reshape layer.

func (*Reshape[T]) OutputShape added in v0.3.0

func (r *Reshape[T]) OutputShape() []int

OutputShape returns the output shape of the Reshape layer.

func (*Reshape[T]) Parameters added in v0.3.0

func (r *Reshape[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Reshape layer.

type RotaryEmbedding added in v0.3.0

type RotaryEmbedding[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

RotaryEmbedding applies rotary position embedding to input tensors.

func NewRotaryEmbedding added in v0.3.0

func NewRotaryEmbedding[T tensor.Numeric](engine compute.Engine[T]) *RotaryEmbedding[T]

NewRotaryEmbedding creates a new RotaryEmbedding layer.

func (*RotaryEmbedding[T]) Attributes added in v0.3.0

func (r *RotaryEmbedding[T]) Attributes() map[string]interface{}

Attributes returns nil for the RotaryEmbedding layer.

func (*RotaryEmbedding[T]) Backward added in v0.3.0

func (r *RotaryEmbedding[T]) Backward(_ context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the RotaryEmbedding layer.

func (*RotaryEmbedding[T]) Forward added in v0.3.0

func (r *RotaryEmbedding[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward applies rotary embedding to the input.

func (*RotaryEmbedding[T]) Name added in v0.3.0

func (r *RotaryEmbedding[T]) Name() string

Name returns the name of the layer.

func (*RotaryEmbedding[T]) OpType added in v0.3.0

func (r *RotaryEmbedding[T]) OpType() string

OpType returns the operation type of the RotaryEmbedding layer.

func (*RotaryEmbedding[T]) OutputShape added in v0.3.0

func (r *RotaryEmbedding[T]) OutputShape() []int

OutputShape returns the output shape of the layer.

func (*RotaryEmbedding[T]) Parameters added in v0.3.0

func (r *RotaryEmbedding[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the layer (none for RotaryEmbedding).

func (*RotaryEmbedding[T]) SetName added in v0.3.0

func (r *RotaryEmbedding[T]) SetName(name string)

SetName sets the name of the layer.

type Shape added in v0.3.0

type Shape[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Shape is a layer that outputs the shape of its input tensor.

func New added in v0.3.0

func New[T tensor.Numeric](engine compute.Engine[T]) *Shape[T]

New creates a new Shape layer.

func (*Shape[T]) Attributes added in v0.3.0

func (s *Shape[T]) Attributes() map[string]interface{}

Attributes returns nil for the Shape layer.

func (*Shape[T]) Backward added in v0.3.0

func (s *Shape[T]) Backward(_ context.Context, mode types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Shape layer.

func (*Shape[T]) Forward added in v0.3.0

func (s *Shape[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the shape of the input tensor.

func (*Shape[T]) OpType added in v0.3.0

func (s *Shape[T]) OpType() string

OpType returns the operation type of the Shape layer.

func (*Shape[T]) OutputShape added in v0.3.0

func (s *Shape[T]) OutputShape() []int

OutputShape returns the output shape of the Shape layer.

func (*Shape[T]) Parameters added in v0.3.0

func (s *Shape[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Shape layer.

type SpectralFingerprint added in v0.3.0

type SpectralFingerprint[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

SpectralFingerprint implements a layer that extracts the top-K frequency components from the input using FFT, creating a fixed-size spectral signature for variable-length inputs.

func NewSpectralFingerprint added in v0.3.0

func NewSpectralFingerprint[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], window, topK int) (*SpectralFingerprint[T], error)

NewSpectralFingerprint creates a new SpectralFingerprint layer.

func (*SpectralFingerprint[T]) Attributes added in v0.3.0

func (s *SpectralFingerprint[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the layer.

func (*SpectralFingerprint[T]) Backward added in v0.3.0

Backward returns no gradients (treated as non-differentiable feature transform).

func (*SpectralFingerprint[T]) Forward added in v0.3.0

func (s *SpectralFingerprint[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes spectral magnitudes for bins 1..topK for each row in the batch. Input must be [batch, window]. If input window dimension is larger than the configured window, only the last `window` elements are used. If smaller, an error is returned.

func (*SpectralFingerprint[T]) OpType added in v0.3.0

func (s *SpectralFingerprint[T]) OpType() string

OpType returns the operation type.

func (*SpectralFingerprint[T]) OutputShape added in v0.3.0

func (s *SpectralFingerprint[T]) OutputShape() []int

OutputShape returns the last computed output shape.

func (*SpectralFingerprint[T]) Parameters added in v0.3.0

func (s *SpectralFingerprint[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for SpectralFingerprint.

type Sub added in v0.3.0

type Sub[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Sub is a layer that performs element-wise subtraction of two tensors.

func NewSub added in v0.3.0

func NewSub[T tensor.Numeric](engine compute.Engine[T]) *Sub[T]

NewSub creates a new Sub layer.

func (*Sub[T]) Attributes added in v0.3.0

func (s *Sub[T]) Attributes() map[string]interface{}

Attributes returns nil for the Sub layer.

func (*Sub[T]) Backward added in v0.3.0

func (s *Sub[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Sub layer.

func (*Sub[T]) Forward added in v0.3.0

func (s *Sub[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the element-wise subtraction of two input tensors (a - b).

func (*Sub[T]) OpType added in v0.3.0

func (s *Sub[T]) OpType() string

OpType returns the operation type of the Sub layer.

func (*Sub[T]) OutputShape added in v0.3.0

func (s *Sub[T]) OutputShape() []int

OutputShape returns the output shape of the Sub layer.

func (*Sub[T]) Parameters added in v0.3.0

func (s *Sub[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Sub layer.

type Unsqueeze added in v0.3.0

type Unsqueeze[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Unsqueeze is a layer that adds dimensions of size 1 to a tensor at specified axes.

func NewUnsqueeze added in v0.3.0

func NewUnsqueeze[T tensor.Numeric](engine compute.Engine[T], axes []int) *Unsqueeze[T]

NewUnsqueeze creates a new Unsqueeze layer.

func (*Unsqueeze[T]) Attributes added in v0.3.0

func (u *Unsqueeze[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the Unsqueeze layer.

func (*Unsqueeze[T]) Backward added in v0.3.0

func (u *Unsqueeze[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Unsqueeze layer.

func (*Unsqueeze[T]) Forward added in v0.3.0

func (u *Unsqueeze[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the unsqueeze operation by reshaping the input tensor.

func (*Unsqueeze[T]) OpType added in v0.3.0

func (u *Unsqueeze[T]) OpType() string

OpType returns the operation type of the Unsqueeze layer.

func (*Unsqueeze[T]) OutputShape added in v0.3.0

func (u *Unsqueeze[T]) OutputShape() []int

OutputShape returns the output shape of the Unsqueeze layer.

func (*Unsqueeze[T]) Parameters added in v0.3.0

func (u *Unsqueeze[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Unsqueeze layer.

Jump to

Keyboard shortcuts

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