loss

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package loss provides various loss functions for neural networks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CrossEntropyLoss

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

CrossEntropyLoss computes the cross-entropy loss.

func NewCrossEntropyLoss

func NewCrossEntropyLoss[T tensor.Numeric](engine compute.Engine[T]) *CrossEntropyLoss[T]

NewCrossEntropyLoss creates a new CrossEntropyLoss layer.

func (*CrossEntropyLoss[T]) Backward

func (cel *CrossEntropyLoss[T]) Backward(ctx context.Context, dOut *tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for CrossEntropyLoss. dOut is typically a scalar (1.0) for loss functions.

func (*CrossEntropyLoss[T]) Forward

func (cel *CrossEntropyLoss[T]) Forward(ctx context.Context, predictions *tensor.TensorNumeric[T], targets *tensor.TensorNumeric[int]) (*tensor.TensorNumeric[T], error)

Forward computes the cross-entropy loss. Inputs: predictions (logits), targets (int labels).

func (*CrossEntropyLoss[T]) OutputShape

func (cel *CrossEntropyLoss[T]) OutputShape() []int

OutputShape returns the output shape of the loss (a scalar).

func (*CrossEntropyLoss[T]) Parameters

func (cel *CrossEntropyLoss[T]) Parameters() []graph.Parameter[T]

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

type Loss

type Loss[T tensor.Numeric] interface {
	// Forward computes the loss and its gradient.
	Forward(ctx context.Context, predictions, targets *tensor.TensorNumeric[T]) (T, *tensor.TensorNumeric[T], error)
}

Loss defines the interface for loss functions.

type MSE

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

MSE calculates the mean squared error between predictions and targets.

func NewMSE

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

NewMSE creates a new MSE loss function.

func (*MSE[T]) Backward

func (m *MSE[T]) Backward(ctx context.Context, predictions, targets *tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Backward computes the initial gradient of the loss.

func (*MSE[T]) Forward

func (m *MSE[T]) Forward(ctx context.Context, predictions, targets *tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the loss value.

Jump to

Keyboard shortcuts

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