layers

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func LayerToIndex ¶

func LayerToIndex(layer Layer) int

Types ¶

type BatchNormCache ¶ added in v1.5.0

type BatchNormCache struct {
	Normed *mat.Dense
}

type BatchNormShift ¶ added in v1.5.0

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

func (*BatchNormShift) Apply ¶ added in v1.5.0

func (b *BatchNormShift) Apply(rawlayer Layer, scale float64)

func (*BatchNormShift) Combine ¶ added in v1.5.0

func (b *BatchNormShift) Combine(b2 ShiftType) ShiftType

func (*BatchNormShift) NumMatrices ¶ added in v1.5.0

func (b *BatchNormShift) NumMatrices() int

func (*BatchNormShift) Optimize ¶ added in v1.5.0

func (b *BatchNormShift) Optimize(opt optimizers.Optimizer, index int)

func (*BatchNormShift) Scale ¶ added in v1.5.0

func (b *BatchNormShift) Scale(f float64)

type BatchnormLayer ¶ added in v1.5.0

type BatchnormLayer struct {
	BatchSize     int
	GradientScale float64
	// contains filtered or unexported fields
}

func (*BatchnormLayer) Back ¶ added in v1.5.0

func (layer *BatchnormLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*BatchnormLayer) FromBytes ¶ added in v1.5.0

func (layer *BatchnormLayer) FromBytes(bytes []byte)

func (*BatchnormLayer) Initialize ¶ added in v1.5.0

func (layer *BatchnormLayer) Initialize(n_inputs int)

func (*BatchnormLayer) NumOutputs ¶ added in v1.5.0

func (layer *BatchnormLayer) NumOutputs() int

func (*BatchnormLayer) Pass ¶ added in v1.5.0

func (layer *BatchnormLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*BatchnormLayer) PrettyPrint ¶ added in v1.5.0

func (layer *BatchnormLayer) PrettyPrint() string

func (*BatchnormLayer) ToBytes ¶ added in v1.5.0

func (layer *BatchnormLayer) ToBytes() []byte

type CacheType ¶ added in v1.4.0

type CacheType interface{}

This is an interface for allowing layers to designate their own types of caches. For example, on Tanh layers, it is best to cache the output of the layer for backprop as it avoids recalculating tanh values, but for linear layers it is better to cache the inputs.

type Conv2DLayer ¶

type Conv2DLayer struct {
	InputShape  Shape
	KernelShape Shape
	NumKernels  int
	FirstLayer  bool
	// contains filtered or unexported fields
}

func (*Conv2DLayer) Back ¶

func (layer *Conv2DLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*Conv2DLayer) FromBytes ¶

func (layer *Conv2DLayer) FromBytes(bytes []byte)

func (*Conv2DLayer) Initialize ¶

func (layer *Conv2DLayer) Initialize(numInputs int)

func (*Conv2DLayer) NumOutputs ¶

func (layer *Conv2DLayer) NumOutputs() int

func (*Conv2DLayer) Pass ¶

func (layer *Conv2DLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*Conv2DLayer) PrettyPrint ¶

func (layer *Conv2DLayer) PrettyPrint() string

func (*Conv2DLayer) ToBytes ¶

func (layer *Conv2DLayer) ToBytes() []byte

type FlattenLayer ¶

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

func (*FlattenLayer) Back ¶

func (layer *FlattenLayer) Back(_ CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*FlattenLayer) FromBytes ¶

func (layer *FlattenLayer) FromBytes(bytes []byte)

func (*FlattenLayer) Initialize ¶

func (layer *FlattenLayer) Initialize(n_inputs int)

func (*FlattenLayer) NumOutputs ¶

func (layer *FlattenLayer) NumOutputs() int

func (*FlattenLayer) Pass ¶

func (layer *FlattenLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*FlattenLayer) PrettyPrint ¶

func (layer *FlattenLayer) PrettyPrint() string

func (*FlattenLayer) ToBytes ¶

func (layer *FlattenLayer) ToBytes() []byte

type InputCache ¶ added in v1.4.0

type InputCache struct {
	Input *mat.Dense
}

type KernelShift ¶

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

func (*KernelShift) Apply ¶

func (k *KernelShift) Apply(layer Layer, scale float64)

func (*KernelShift) Combine ¶

func (k *KernelShift) Combine(k2 ShiftType) ShiftType

func (*KernelShift) NumMatrices ¶ added in v1.5.0

func (k *KernelShift) NumMatrices() int

func (*KernelShift) Optimize ¶ added in v1.5.0

func (k *KernelShift) Optimize(opt optimizers.Optimizer, index int)

func (*KernelShift) Scale ¶ added in v1.5.0

func (k *KernelShift) Scale(f float64)

type LSTMCache ¶ added in v1.4.0

type LSTMCache struct {
	Inputs           []*mat.Dense
	HiddenStates     []*mat.Dense
	CellStates       []*mat.Dense
	ForgetOutputs    []*mat.Dense
	InputOutputs     []*mat.Dense
	CandidateOutputs []*mat.Dense
	OutputOutputs    []*mat.Dense
}

type LSTMLayer ¶ added in v1.4.0

type LSTMLayer struct {
	Outputs   int
	InputSize int

	OutputSequence      bool
	ConstantLengthInput bool
	OutputChunks        int
	// contains filtered or unexported fields
}

func (*LSTMLayer) Back ¶ added in v1.4.0

func (layer *LSTMLayer) Back(cache CacheType, frontalPass *mat.Dense) (shift ShiftType, backpass *mat.Dense)

func (*LSTMLayer) FromBytes ¶ added in v1.4.0

func (layer *LSTMLayer) FromBytes(bytes []byte)

func (*LSTMLayer) Initialize ¶ added in v1.4.0

func (layer *LSTMLayer) Initialize(totalInputs int)

func (*LSTMLayer) NumOutputs ¶ added in v1.4.0

func (layer *LSTMLayer) NumOutputs() int

func (*LSTMLayer) Pass ¶ added in v1.4.0

func (layer *LSTMLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*LSTMLayer) PrettyPrint ¶ added in v1.4.0

func (layer *LSTMLayer) PrettyPrint() string

func (*LSTMLayer) ToBytes ¶ added in v1.4.0

func (layer *LSTMLayer) ToBytes() []byte

type LSTMShift ¶ added in v1.4.0

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

func (*LSTMShift) Apply ¶ added in v1.4.0

func (l *LSTMShift) Apply(layer Layer, scale float64)

func (*LSTMShift) Combine ¶ added in v1.4.0

func (l *LSTMShift) Combine(l2 ShiftType) ShiftType

func (*LSTMShift) NumMatrices ¶ added in v1.5.0

func (l *LSTMShift) NumMatrices() int

func (*LSTMShift) Optimize ¶ added in v1.5.0

func (l *LSTMShift) Optimize(opt optimizers.Optimizer, index int)

func (*LSTMShift) Scale ¶ added in v1.5.0

func (l *LSTMShift) Scale(f float64)

type LanhLayer ¶ added in v1.5.2

type LanhLayer struct {
	GradientScale float64
	// contains filtered or unexported fields
}

func (*LanhLayer) Back ¶ added in v1.5.2

func (layer *LanhLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*LanhLayer) FromBytes ¶ added in v1.5.2

func (layer *LanhLayer) FromBytes(bytes []byte)

func (*LanhLayer) Initialize ¶ added in v1.5.2

func (layer *LanhLayer) Initialize(n_inputs int)

func (*LanhLayer) NumOutputs ¶ added in v1.5.2

func (layer *LanhLayer) NumOutputs() int

func (*LanhLayer) Pass ¶ added in v1.5.2

func (layer *LanhLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*LanhLayer) PrettyPrint ¶ added in v1.5.2

func (layer *LanhLayer) PrettyPrint() string

func (*LanhLayer) ToBytes ¶ added in v1.5.2

func (layer *LanhLayer) ToBytes() []byte

type Layer ¶

type Layer interface {
	Initialize(int)
	Pass(*mat.Dense) (*mat.Dense, CacheType)
	Back(CacheType, *mat.Dense) (ShiftType, *mat.Dense)
	NumOutputs() int

	ToBytes() []byte
	FromBytes([]byte)
	PrettyPrint() string
}

LAYER - The basic interface for all inner layers of an ANN.

Initialize (numInputs int): Tells the layer how many inputs to expect, and sets up everything accordingly.

Pass (*mat.Dense) (*mat.Dense, CacheType): Passes the input through the layer to get an output, and cache necessary information to do backprop.

Back (cache CacheType, forwardGradients *mat.Dense) (shift ShiftType, backwardsGradients *mat.Dense): Takes the partial derivatives from the layers in front, calculates the gradient for itself, and passes it back to the last layer.

func IndexToLayer ¶

func IndexToLayer(index int) Layer

This allows for mapping between layer types and ints for the sake of saving to files and reconstructing.

type LinearLayer ¶

type LinearLayer struct {
	Outputs int
	NoBias  bool
	// contains filtered or unexported fields
}

Linear (or Dense) layer type

func (*LinearLayer) Back ¶

func (layer *LinearLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*LinearLayer) FromBytes ¶

func (layer *LinearLayer) FromBytes(bytes []byte)

func (*LinearLayer) Initialize ¶

func (layer *LinearLayer) Initialize(numInputs int)

func (*LinearLayer) NumOutputs ¶

func (layer *LinearLayer) NumOutputs() int

func (*LinearLayer) Pass ¶

func (layer *LinearLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*LinearLayer) PrettyPrint ¶

func (layer *LinearLayer) PrettyPrint() string

func (*LinearLayer) ToBytes ¶

func (layer *LinearLayer) ToBytes() []byte

type MaxPool2DLayer ¶

type MaxPool2DLayer struct {
	PoolShape Shape
	// contains filtered or unexported fields
}

func (*MaxPool2DLayer) Back ¶

func (layer *MaxPool2DLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*MaxPool2DLayer) FromBytes ¶

func (layer *MaxPool2DLayer) FromBytes(bytes []byte)

func (*MaxPool2DLayer) Initialize ¶

func (layer *MaxPool2DLayer) Initialize(n_inputs int)

func (*MaxPool2DLayer) NumOutputs ¶

func (layer *MaxPool2DLayer) NumOutputs() int

func (*MaxPool2DLayer) Pass ¶

func (layer *MaxPool2DLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*MaxPool2DLayer) PrettyPrint ¶

func (layer *MaxPool2DLayer) PrettyPrint() string

func (*MaxPool2DLayer) ToBytes ¶

func (layer *MaxPool2DLayer) ToBytes() []byte

type NilShift ¶

type NilShift struct{}

func (*NilShift) Apply ¶

func (n *NilShift) Apply(_ Layer, _ float64)

func (*NilShift) Combine ¶

func (n *NilShift) Combine(other ShiftType) ShiftType

func (*NilShift) NumMatrices ¶ added in v1.5.0

func (n *NilShift) NumMatrices() int

func (*NilShift) Optimize ¶ added in v1.5.0

func (n *NilShift) Optimize(_ optimizers.Optimizer, _ int)

func (*NilShift) Scale ¶ added in v1.5.0

func (n *NilShift) Scale(f float64)

type OutputCache ¶ added in v1.4.0

type OutputCache struct {
	Output *mat.Dense
}

type ReluLayer ¶

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

func (*ReluLayer) Back ¶

func (layer *ReluLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*ReluLayer) FromBytes ¶

func (layer *ReluLayer) FromBytes(bytes []byte)

func (*ReluLayer) Initialize ¶

func (layer *ReluLayer) Initialize(n_inputs int)

func (*ReluLayer) NumOutputs ¶

func (layer *ReluLayer) NumOutputs() int

func (*ReluLayer) Pass ¶

func (layer *ReluLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*ReluLayer) PrettyPrint ¶

func (layer *ReluLayer) PrettyPrint() string

func (*ReluLayer) ToBytes ¶

func (layer *ReluLayer) ToBytes() []byte

type Shape ¶

type Shape struct {
	Rows int
	Cols int
}

type ShiftType ¶

type ShiftType interface {
	Apply(Layer, float64)
	Combine(ShiftType) ShiftType
	Optimize(optimizers.Optimizer, int)

	NumMatrices() int
	Scale(float64)
}

This is an interface for carrying all the different gradient steps that will be applied after backprop. The default NilShift is defined here, but most layer specific shift types are defined in their own files.

type SigmoidLayer ¶

type SigmoidLayer struct {
	GradientScale float64
	// contains filtered or unexported fields
}

func (*SigmoidLayer) Back ¶

func (layer *SigmoidLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*SigmoidLayer) FromBytes ¶

func (layer *SigmoidLayer) FromBytes(bytes []byte)

func (*SigmoidLayer) Initialize ¶

func (layer *SigmoidLayer) Initialize(n_inputs int)

func (*SigmoidLayer) NumOutputs ¶

func (layer *SigmoidLayer) NumOutputs() int

func (*SigmoidLayer) Pass ¶

func (layer *SigmoidLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*SigmoidLayer) PrettyPrint ¶

func (layer *SigmoidLayer) PrettyPrint() string

func (*SigmoidLayer) ToBytes ¶

func (layer *SigmoidLayer) ToBytes() []byte

type SoftmaxLayer ¶

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

func (*SoftmaxLayer) Back ¶

func (layer *SoftmaxLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*SoftmaxLayer) FromBytes ¶

func (layer *SoftmaxLayer) FromBytes(bytes []byte)

func (*SoftmaxLayer) Initialize ¶

func (layer *SoftmaxLayer) Initialize(n_inputs int)

func (*SoftmaxLayer) NumOutputs ¶

func (layer *SoftmaxLayer) NumOutputs() int

func (*SoftmaxLayer) Pass ¶

func (layer *SoftmaxLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*SoftmaxLayer) PrettyPrint ¶

func (layer *SoftmaxLayer) PrettyPrint() string

func (*SoftmaxLayer) ToBytes ¶

func (layer *SoftmaxLayer) ToBytes() []byte

type TanhLayer ¶

type TanhLayer struct {
	GradientScale float64
	// contains filtered or unexported fields
}

func (*TanhLayer) Back ¶

func (layer *TanhLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*TanhLayer) FromBytes ¶

func (layer *TanhLayer) FromBytes(bytes []byte)

func (*TanhLayer) Initialize ¶

func (layer *TanhLayer) Initialize(n_inputs int)

func (*TanhLayer) NumOutputs ¶

func (layer *TanhLayer) NumOutputs() int

func (*TanhLayer) Pass ¶

func (layer *TanhLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*TanhLayer) PrettyPrint ¶

func (layer *TanhLayer) PrettyPrint() string

func (*TanhLayer) ToBytes ¶

func (layer *TanhLayer) ToBytes() []byte

type VariableLinearLayer ¶ added in v1.5.4

type VariableLinearLayer struct {
	InputSize  int
	OutputSize int

	ConstantLengthInput bool
	// contains filtered or unexported fields
}

Linear (or Dense) layer type

func (*VariableLinearLayer) Back ¶ added in v1.5.4

func (layer *VariableLinearLayer) Back(cache CacheType, forwardGradients *mat.Dense) (ShiftType, *mat.Dense)

func (*VariableLinearLayer) FromBytes ¶ added in v1.5.4

func (layer *VariableLinearLayer) FromBytes(bytes []byte)

func (*VariableLinearLayer) Initialize ¶ added in v1.5.4

func (layer *VariableLinearLayer) Initialize(numInputs int)

func (*VariableLinearLayer) NumOutputs ¶ added in v1.5.4

func (layer *VariableLinearLayer) NumOutputs() int

func (*VariableLinearLayer) Pass ¶ added in v1.5.4

func (layer *VariableLinearLayer) Pass(input *mat.Dense) (*mat.Dense, CacheType)

func (*VariableLinearLayer) PrettyPrint ¶ added in v1.5.4

func (layer *VariableLinearLayer) PrettyPrint() string

func (*VariableLinearLayer) ToBytes ¶ added in v1.5.4

func (layer *VariableLinearLayer) ToBytes() []byte

type WeightShift ¶

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

ShiftType used by LinearLayers

func (*WeightShift) Apply ¶

func (w *WeightShift) Apply(layer Layer, scale float64)

func (*WeightShift) Combine ¶

func (w *WeightShift) Combine(w2 ShiftType) ShiftType

func (*WeightShift) NumMatrices ¶ added in v1.5.0

func (w *WeightShift) NumMatrices() int

func (*WeightShift) Optimize ¶ added in v1.5.0

func (w *WeightShift) Optimize(opt optimizers.Optimizer, index int)

func (*WeightShift) Scale ¶ added in v1.5.0

func (w *WeightShift) Scale(f float64)

Jump to

Keyboard shortcuts

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