core

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package core provides core neural network layer implementations.

Package core provides core neural network layer implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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) (*Bias[T], error)

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

func NewBiasWithFactories

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

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

func (*Bias[T]) Backward

func (b *Bias[T]) Backward(outputGradient *tensor.Tensor[T]) ([]*tensor.Tensor[T], error)

Backward computes the gradients.

func (*Bias[T]) Forward

func (b *Bias[T]) Forward(inputs ...*tensor.Tensor[T]) (*tensor.Tensor[T], error)

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

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 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) (*Dense[T], error)

NewDense creates a new Dense layer.

func (*Dense[T]) Backward

func (d *Dense[T]) Backward(outputGradient *tensor.Tensor[T]) ([]*tensor.Tensor[T], error)

Backward computes the gradients.

func (*Dense[T]) Forward

func (d *Dense[T]) Forward(inputs ...*tensor.Tensor[T]) (*tensor.Tensor[T], error)

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

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 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) (*Linear[T], error)

NewLinear creates a new Linear layer with Xavier initialization (default).

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.Tensor[T], error), newParameter func(string, *tensor.Tensor[T], func([]int, []T) (*tensor.Tensor[T], error)) (*graph.Parameter[T], error)) (*Linear[T], error)

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

func NewLinearWithHe

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

NewLinearWithHe creates a Linear layer with He initialization.

func NewLinearWithInitializer

func NewLinearWithInitializer[T tensor.Numeric](name string, engine compute.Engine[T], ops numeric.Arithmetic[T], inputSize, outputSize int, initializer components.WeightInitializer[T]) (*Linear[T], error)

NewLinearWithInitializer creates a Linear layer with a custom weight initializer.

func NewLinearWithUniform

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

NewLinearWithUniform creates a Linear layer with uniform initialization.

func NewLinearWithXavier

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

NewLinearWithXavier creates a Linear layer with Xavier initialization.

func (*Linear[T]) Backward

func (l *Linear[T]) Backward(outputGradient *tensor.Tensor[T]) ([]*tensor.Tensor[T], error)

Backward computes the gradients using the gradient computer component.

func (*Linear[T]) Forward

func (l *Linear[T]) Forward(inputs ...*tensor.Tensor[T]) (*tensor.Tensor[T], error)

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

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 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,
	degree int,
	includeBias bool,
) (*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 - degree: maximum polynomial degree (must be >= 1) - includeBias: whether to include a bias term (constant 1)

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

func (*PolynomialExpansion[T]) Backward

func (p *PolynomialExpansion[T]) Backward(outputGradient *tensor.Tensor[T]) []*tensor.Tensor[T]

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(inputs ...*tensor.Tensor[T]) *tensor.Tensor[T]

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]) OutputShape

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

OutputShape returns the shape of the output tensor.

func (*PolynomialExpansion[T]) Parameters

func (p *PolynomialExpansion[T]) Parameters() []*tensor.Tensor[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).

Jump to

Keyboard shortcuts

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