nn

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(o any, r io.Reader) error

func NormalInit

func NormalInit(t *Tensor, mean, std float32)

func Save

func Save(o any, w io.Writer) error

func SetInferenceMode

func SetInferenceMode(enabled bool)

SetInferenceMode enables or disables inference mode, which disables gradient computation to improve performance during model evaluation.

Types

type Adam

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

func (*Adam) SetJobs

func (o *Adam) SetJobs(jobs int)

func (*Adam) SetWeightDecay

func (o *Adam) SetWeightDecay(wd float32)

func (*Adam) Step

func (a *Adam) Step()

func (*Adam) ZeroGrad

func (o *Adam) ZeroGrad()

type EmbeddingLayer

type EmbeddingLayer struct {
	W *Tensor
}

func (*EmbeddingLayer) Forward

func (e *EmbeddingLayer) Forward(x *Tensor) *Tensor

func (*EmbeddingLayer) Parameters

func (e *EmbeddingLayer) Parameters() []*Tensor

func (*EmbeddingLayer) SetJobs

func (e *EmbeddingLayer) SetJobs(int)

type Layer

type Layer interface {
	Parameters() []*Tensor
	Forward(x *Tensor) *Tensor
	SetJobs(jobs int)
}

func NewEmbedding

func NewEmbedding(n int, shape ...int) Layer

func NewFlatten

func NewFlatten() Layer

func NewLinear

func NewLinear(in, out int) Layer

func NewReLU

func NewReLU() Layer

func NewSigmoid

func NewSigmoid() Layer

type LinearLayer

type LinearLayer struct {
	W *Tensor
	B *Tensor
	// contains filtered or unexported fields
}

func (*LinearLayer) Forward

func (l *LinearLayer) Forward(x *Tensor) *Tensor

func (*LinearLayer) Parameters

func (l *LinearLayer) Parameters() []*Tensor

func (*LinearLayer) SetJobs

func (l *LinearLayer) SetJobs(jobs int)

type Model

type Model Layer

func NewSequential

func NewSequential(layers ...Layer) Model

type Optimizer

type Optimizer interface {
	SetWeightDecay(rate float32)
	SetJobs(jobs int)
	ZeroGrad()
	Step()
}

func NewAdam

func NewAdam(params []*Tensor, alpha float32) Optimizer

func NewSGD

func NewSGD(params []*Tensor, lr float32) Optimizer

type SGD

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

func (*SGD) SetJobs

func (o *SGD) SetJobs(jobs int)

func (*SGD) SetWeightDecay

func (o *SGD) SetWeightDecay(wd float32)

func (*SGD) Step

func (s *SGD) Step()

func (*SGD) ZeroGrad

func (o *SGD) ZeroGrad()

type Sequential

type Sequential struct {
	Layers []Layer
}

func (*Sequential) Forward

func (s *Sequential) Forward(x *Tensor) *Tensor

func (*Sequential) Parameters

func (s *Sequential) Parameters() []*Tensor

func (*Sequential) SetJobs

func (s *Sequential) SetJobs(jobs int)

type Tensor

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

func Abs

func Abs(x *Tensor) *Tensor

func Add

func Add(x0 *Tensor, x ...*Tensor) *Tensor

Add returns the element-wise sum of two tensors. The shape of the second tensor must be a suffix sequence of the shape of the first tensor.

func BCEWithLogits

func BCEWithLogits(target, prediction, weights *Tensor) *Tensor

BCEWithLogits calculates the binary cross-entropy loss between target and prediction with logits. This implementation is numerically stable. It is equivalent to the formula:

max(prediction, 0) - prediction*y + log(1 + exp(-|prediction|))

where y = (target + 1) / 2, target is -1 or 1.

func BMM

func BMM(x, y *Tensor, transpose1, transpose2 bool, jobs int) *Tensor

func Broadcast

func Broadcast(x *Tensor, shape ...int) *Tensor

func Cos

func Cos(x *Tensor) *Tensor

func Div

func Div(x0, x1 *Tensor) *Tensor

Div returns the element-wise division of two tensors. The shape of the second tensor must be a suffix sequence of the shape of the first tensor.

func Embedding

func Embedding(w, x *Tensor) *Tensor

func Exp

func Exp(x *Tensor) *Tensor

Exp returns the element-wise exponential of a tensor.

func Flatten

func Flatten(x *Tensor) *Tensor

func LinSpace

func LinSpace(start, end float32, shape ...int) *Tensor

func Log

func Log(x *Tensor) *Tensor

Log returns the element-wise natural logarithm of a tensor.

func MatMul

func MatMul(x, y *Tensor, transpose1, transpose2 bool, jobs int) *Tensor

func Mean

func Mean(x *Tensor) *Tensor

Mean returns the mean of all elements in a tensor.

func MeanSquareError

func MeanSquareError(x, y *Tensor) *Tensor

func Mul

func Mul(x0, x1 *Tensor) *Tensor

Mul returns the element-wise product of two tensors. The shape of the second tensor must be a suffix sequence of the shape of the first tensor.

func Neg

func Neg(x *Tensor) *Tensor

func NewScalar

func NewScalar(data float32) *Tensor

func NewTensor

func NewTensor(data []float32, shape ...int) *Tensor

func Normal

func Normal(mean, std float32, shape ...int) *Tensor

func Ones

func Ones(shape ...int) *Tensor

Ones creates a tensor filled with ones.

func Pow

func Pow(x *Tensor, n *Tensor) *Tensor

Pow returns the element-wise power of a tensor. The shape of the second tensor must be a suffix sequence of the shape of the first tensor.

func Rand

func Rand(shape ...int) *Tensor

func ReLu

func ReLu(x *Tensor) *Tensor

func Reshape

func Reshape(x *Tensor, shape ...int) *Tensor

func Sigmoid

func Sigmoid(x *Tensor) *Tensor

func Sin

func Sin(x *Tensor) *Tensor

Sin returns the element-wise sine of a tensor.

func Softmax

func Softmax(x *Tensor, axis int) *Tensor

func SoftmaxCrossEntropy

func SoftmaxCrossEntropy(x, y *Tensor) *Tensor

func Square

func Square(x *Tensor) *Tensor

Square returns the element-wise square of a tensor.

func Sub

func Sub(x0, x1 *Tensor) *Tensor

Sub returns the element-wise difference of two tensors. The shape of the second tensor must be a suffix sequence of the shape of the first tensor.

func Sum

func Sum(x *Tensor, along ...int) *Tensor

Sum returns the sum of all elements in a tensor.

func Uniform

func Uniform(low, high float32, shape ...int) *Tensor

func Zeros

func Zeros(shape ...int) *Tensor

Zeros creates a tensor filled with zeros.

func (*Tensor) Backward

func (t *Tensor) Backward()

func (*Tensor) Data

func (t *Tensor) Data() []float32

func (*Tensor) Get

func (t *Tensor) Get(indices ...int) float32

Get returns the value of the tensor at the given indices.

func (*Tensor) Grad

func (t *Tensor) Grad() *Tensor

func (*Tensor) IsScalar

func (t *Tensor) IsScalar() bool

func (*Tensor) NoGrad

func (t *Tensor) NoGrad() *Tensor

NoGrad convert a node tensor to a leaf tensor.

func (*Tensor) Shape

func (t *Tensor) Shape() []int

func (*Tensor) Slice

func (t *Tensor) Slice(start, end int) *Tensor

Slice returns a slice of the tensor.

func (*Tensor) SliceIndices

func (t *Tensor) SliceIndices(indices ...int) *Tensor

func (*Tensor) String

func (t *Tensor) String() string

Jump to

Keyboard shortcuts

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