nn

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2023 License: BSD-2-Clause Imports: 11 Imported by: 34

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidIndex = errors.New("nn: invalid index")

Functions

func Apply

func Apply(m Model, fn func(model Model))

Apply fn recursively to every sub-models as well as self. Typical use includes initializing the parameters of a model.

func Dump

func Dump(obj any, w io.Writer) error

Dump saves a serialized object to a stream. This function uses Gob utility for serialization. Models, matrices, and all kinds of Gob serializable objects can be saved using this function.

func DumpToFile

func DumpToFile[T any](obj T, filename string) error

DumpToFile saves a serialized object to a file. See Dump for further details.

func ForEachParam

func ForEachParam(m Model, fn func(param *Param))

ForEachParam iterate all the parameters of a model also exploring the sub-parameters recursively.

func ForEachParamStrict

func ForEachParamStrict(m Model, fn func(param *Param))

ForEachParamStrict iterate all the parameters of a model without exploring the sub-models.

func Load

func Load[T any](r io.Reader) (T, error)

Load uses Gob to deserialize objects to memory.

func LoadFromFile

func LoadFromFile[T any](filename string) (T, error)

LoadFromFile uses Gob to deserialize objects files to memory. See Load for further details.

func ZeroGrad

func ZeroGrad(m Model)

ZeroGrad set the gradients of all model's parameters (including sub-params) to zeros.

Types

type Buffer

type Buffer struct {
	mat.Tensor
}

Buffer is a type of Node that do not require gradients but that can be serialized similarly to any other parameters. This is useful e.g. to store constants, to track the mean and std in batch norm layers etc.

func Buf

func Buf(value mat.Tensor) *Buffer

Buf creates a new Buffer Node.

type Model

type Model interface {
	// contains filtered or unexported methods
}

Model is implemented by all neural network architectures.

type Module

type Module struct{}

Module must be embedded into all neural models.

type ModuleList added in v1.1.0

type ModuleList[T StandardModel] []T

func (ModuleList[T]) Forward added in v1.1.0

func (ml ModuleList[T]) Forward(xs ...mat.Tensor) []mat.Tensor

Forward operates on a slice of StandardModel connecting outputs to inputs sequentially for each module following, finally returning its output.

type Param

type Param struct {
	mat.Matrix
	State interface{} // support structure for the optimization algorithm
}

func NewParam

func NewParam(value mat.Matrix) *Param

NewParam returns a new param.

func (*Param) ReplaceValue

func (p *Param) ReplaceValue(value mat.Matrix)

func (*Param) WithGrad added in v1.1.0

func (p *Param) WithGrad(value bool) *Param

WithGrad sets whether the param requires gradients (default true)

type ParamChannelFunc added in v1.1.0

type ParamChannelFunc func(ctx context.Context) <-chan *Param

func Parameters added in v1.1.0

func Parameters(m Model) ParamChannelFunc

func StreamParams added in v1.1.0

func StreamParams(params []*Param) ParamChannelFunc

type ParamsTraverser

type ParamsTraverser interface {
	// TraverseParams visit each Param.
	TraverseParams(callback func(param *Param))
}

ParamsTraverser allows you to define a custom procedure to traverse the parameters of a model. If a model implements this procedure, it will take precedence over the regular parameters visit.

type StandardModel

type StandardModel interface {
	Model

	// Forward executes the forward step of the model.
	Forward(...mat.Tensor) []mat.Tensor
}

StandardModel consists of a model that implements a Forward variadic function that accepts mat.Tensor and returns a slice of mat.Tensor. It is called StandardModel since this is the most frequent forward method among all implemented neural models.

Directories

Path Synopsis
approxlinear module
conv1x1
Package conv1x1 implements a 1-dimensional 1-kernel convolution model
Package conv1x1 implements a 1-dimensional 1-kernel convolution model
Package gmlp implements a model composed by basic MLP layers with gating mechanism.
Package gmlp implements a model composed by basic MLP layers with gating mechanism.
gnn
slstm
Package slstm implements a Sentence-State LSTM graph neural network.
Package slstm implements a Sentence-State LSTM graph neural network.
Package mlpmixer implements the MLP-Mixer (Tolstikhin et al., 2021).
Package mlpmixer implements the MLP-Mixer (Tolstikhin et al., 2021).
normalization
adanorm
Package adanorm implements the Adaptive Normalization (AdaNorm) method.
Package adanorm implements the Adaptive Normalization (AdaNorm) method.
fixnorm
Package fixnorm implements the fixnorm normalization method.
Package fixnorm implements the fixnorm normalization method.
layernorm
Package layernorm implements the Layer Normalization (LayerNorm) i method.
Package layernorm implements the Layer Normalization (LayerNorm) i method.
layernormsimple
Package layernormsimple implements a simple version of LayerNorm (LayerNorm-simple).
Package layernormsimple implements a simple version of LayerNorm (LayerNorm-simple).
rmsnorm
Package rmsnorm implements the Root Mean Square Layer Normalization method.
Package rmsnorm implements the Root Mean Square Layer Normalization method.
recurrent
gru
srn
Package sgu implements the Spatial Gating Unit (SGU).
Package sgu implements the Spatial Gating Unit (SGU).

Jump to

Keyboard shortcuts

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