types #
vtl.nn.types
Shared adapter types for neural-network components. Layer[T] erases a concrete layer behind callbacks for output shape, variables, and forward execution; Loss[T] provides a common loss interface; Optimizer[T] exposes registered variables, learning rate, parameter building, and update. These adapters let the sequential model and optimizers work with multiple implementations.
This package is mainly useful when implementing custom layers or integrating new components. Most application code can use constructors from layers, loss, and models.
The contracts are defined in layer.v, loss.v, and optimizer.v. Implementations should keep tensor shapes and context ownership consistent across forward and backward calls.
fn layer #
fn layer[T](ptr voidptr, output_shape_fn LayerOutputShapeFn, variables_fn LayerVariablesFn, forward_fn LayerForwardFn) Layer[T]
layer creates a typed wrapper around a concrete neural network layer.
fn layer_with_mode #
fn layer_with_mode[T](ptr voidptr, output_shape_fn LayerOutputShapeFn, variables_fn LayerVariablesFn, forward_fn LayerForwardFn, forward_mode_fn LayerForwardModeFn) Layer[T]
layer_with_mode creates a typed wrapper for a layer whose behavior depends on whether its model is training or evaluating.
fn loss #
fn loss[T](ptr voidptr, loss_fn LossFn) Loss[T]
loss creates a typed loss wrapper.
fn variable_ptrs_to_voidptrs #
fn variable_ptrs_to_voidptrs[T](vars []&autograd.Variable[T]) []voidptr
variable_ptrs_to_voidptrs converts typed variable pointers at wrapper edges.
interface Optimizer #
interface Optimizer[T] {
mut:
params []&autograd.Variable[T]
learning_rate f64
update() !
build_params(layers Layer[T])
}
Optimizer is a generic interface for all optimizers.
type LayerForwardFn #
type LayerForwardFn = fn (layer voidptr, input voidptr) !voidptr
LayerForwardFn forwards a layer using opaque wrapper pointers.
type LayerForwardModeFn #
type LayerForwardModeFn = fn (layer voidptr, input voidptr, training bool) !voidptr
LayerForwardModeFn forwards a layer with an explicit training mode.
type LayerOutputShapeFn #
type LayerOutputShapeFn = fn (layer voidptr) []int
LayerOutputShapeFn returns a layer output shape from an opaque wrapper pointer.
type LayerVariablesFn #
type LayerVariablesFn = fn (layer voidptr) []voidptr
LayerVariablesFn returns trainable variable pointers from an opaque wrapper pointer.
fn (Layer[T]) output_shape #
fn (layer Layer[T]) output_shape() []int
output_shape returns the layer output shape.
fn (Layer[T]) variables #
fn (layer Layer[T]) variables() []&autograd.Variable[T]
variables returns the trainable variables owned by the layer.
fn (Layer[T]) forward #
fn (layer Layer[T]) forward(input &autograd.Variable[T]) !&autograd.Variable[T]
forward runs a forward pass through the wrapped layer.
fn (Layer[T]) forward_with_mode #
fn (layer Layer[T]) forward_with_mode(input &autograd.Variable[T], training bool) !&autograd.Variable[T]
forward_with_mode runs the wrapped layer with an explicit training mode. Layers without a mode-specific implementation keep their regular behavior.
type LossFn #
type LossFn = fn (loss voidptr, input voidptr, target voidptr) !voidptr
LossFn computes a loss value using opaque wrapper pointers.
fn (Loss[T]) loss #
fn (loss Loss[T]) loss(input &autograd.Variable[T], target &vtl.Tensor[T]) !&autograd.Variable[T]
loss computes a scalar loss variable for model output and target tensors.
struct Layer #
struct Layer[T] {
ptr voidptr
output_shape_fn LayerOutputShapeFn = unsafe { nil }
variables_fn LayerVariablesFn = unsafe { nil }
forward_fn LayerForwardFn = unsafe { nil }
forward_mode_fn LayerForwardModeFn = unsafe { nil }
has_forward_mode bool
}
Layer is an opaque wrapper for a neural network layer.
struct Loss #
struct Loss[T] {
ptr voidptr
loss_fn LossFn = unsafe { nil }
}
Loss wraps a concrete loss implementation without storing a generic interface.