activations

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 activations provides neural network activation functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActivationLayer

type ActivationLayer[T tensor.Numeric] interface {
	Forward(ctx context.Context, inputs ...*tensor.Tensor[T]) (*tensor.Tensor[T], error)
	Backward(ctx context.Context, outputGradient *tensor.Tensor[T]) ([]*tensor.Tensor[T], error)
}

ActivationLayer defines the interface for activation layers used in tests.

type BaseActivation

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

BaseActivation provides common functionality for unary activation functions.

func NewBaseActivation

func NewBaseActivation[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], forwardOp, backwardOp func(T) T) *BaseActivation[T]

NewBaseActivation creates a new base activation with the given forward and backward operations.

func (*BaseActivation[T]) Backward

func (b *BaseActivation[T]) Backward(ctx context.Context, outputGradient *tensor.Tensor[T]) ([]*tensor.Tensor[T], error)

Backward performs the backward pass of the activation function.

func (*BaseActivation[T]) Forward

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

Forward performs the forward pass of the activation function.

func (*BaseActivation[T]) OutputShape

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

OutputShape returns the output shape of the activation.

type LeakyReLU

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

LeakyReLU implements the Leaky Rectified Linear Unit activation function.

func NewLeakyReLU

func NewLeakyReLU[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], alpha float64) *LeakyReLU[T]

NewLeakyReLU creates a new LeakyReLU activation function.

func (*LeakyReLU[T]) Backward

func (l *LeakyReLU[T]) Backward(ctx context.Context, outputGradient *tensor.Tensor[T]) ([]*tensor.Tensor[T], error)

Backward computes the gradients for the LeakyReLU activation.

func (*LeakyReLU[T]) Forward

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

Forward computes the LeakyReLU activation for the given input.

func (*LeakyReLU[T]) OutputShape

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

OutputShape returns the output shape of the LeakyReLU layer.

type ReLU

type ReLU[T tensor.Numeric] struct {
	*BaseActivation[T]
}

ReLU implements the Rectified Linear Unit activation function.

func NewReLU

func NewReLU[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T]) *ReLU[T]

NewReLU creates a new ReLU activation function.

type Sigmoid

type Sigmoid[T tensor.Numeric] struct {
	*BaseActivation[T]
}

Sigmoid implements the sigmoid activation function.

func NewSigmoid

func NewSigmoid[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T]) *Sigmoid[T]

NewSigmoid creates a new Sigmoid activation function.

type SwiGLU

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

SwiGLU implements the SwiGLU activation function.

func NewSwiGLU

func NewSwiGLU[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T]) *SwiGLU[T]

NewSwiGLU creates a new SwiGLU activation layer.

func (*SwiGLU[T]) Backward

func (s *SwiGLU[T]) Backward(ctx context.Context, dOut *tensor.Tensor[T], inputs ...*tensor.Tensor[T]) ([]*tensor.Tensor[T], error)

Backward computes the gradients for SwiGLU.

func (*SwiGLU[T]) Forward

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

Forward computes the SwiGLU activation. Input: A tensor with its last dimension being 2 * feature_dim.

func (*SwiGLU[T]) OutputShape

func (s *SwiGLU[T]) OutputShape(inputShapes ...[]int) ([]int, error)

OutputShape returns the output shape of SwiGLU. Input shape is (..., 2 * feature_dim). Output shape is (..., feature_dim).

func (*SwiGLU[T]) Parameters

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

Parameters returns an empty slice as SwiGLU has no trainable parameters.

type Tanh

type Tanh[T tensor.Numeric] struct {
	*BaseActivation[T]
}

Tanh implements the hyperbolic tangent activation function.

func NewTanh

func NewTanh[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T]) *Tanh[T]

NewTanh creates a new Tanh activation function.

Jump to

Keyboard shortcuts

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