Skip to content

nn.models

fn sequential #

fn sequential[T]() &Sequential[T]

sequential creates a new sequential network with a new context.

fn sequential_from_ctx #

fn sequential_from_ctx[T](ctx &autograd.Context[T]) &Sequential[T]

sequential_from_ctx creates a new sequential network with the given context.

fn sequential_from_ctx_with_layers #

fn sequential_from_ctx_with_layers[T](ctx &autograd.Context[T], given_layers []types.Layer[T]) &Sequential[T]

sequential_from_ctx_with_layers creates a new sequential network with the given context and the given layers.

fn sequential_info #

fn sequential_info[T](ctx &autograd.Context[T], layers_ []types.Layer[T]) &SequentialInfo[T]

sequential_info creates a new neural network container with an empty list of layers.

fn sequential_with_layers #

fn sequential_with_layers[T](given_layers []types.Layer[T]) &Sequential[T]

sequential_with_layers creates a new sequential network with a new context and the given layers.

fn validate_model_compatibility #

fn validate_model_compatibility[T](saved_path string, model_layers []types.Layer[T]) !bool

validate_model_compatibility checks if a saved model is compatible with the given layers. Returns an error with details if incompatible.

fn Sequential.load_checkpoint #

fn Sequential.load_checkpoint[T](path string) !(int, f64)

load_checkpoint restores a Sequential model's weights and training metadata from a JSON file. Returns the epoch and loss from the checkpoint.

Note: The model must already be constructed with the same architecture. This method loads weights into an existing model.

fn (SequentialInfo[T]) input #

fn (mut ls SequentialInfo[T]) input(shape []int)

input adds a new input layer to the network with the given shape.

fn (SequentialInfo[T]) linear #

fn (mut ls SequentialInfo[T]) linear(output_size int)

linear adds a new linear layer to the network with the given output size

fn (SequentialInfo[T]) maxpool2d #

fn (mut ls SequentialInfo[T]) maxpool2d(kernel []int, padding []int, stride []int)

maxpool2d adds a new maxpool2d layer to the network with the given kernel size and stride.

fn (SequentialInfo[T]) mse_loss #

fn (mut ls SequentialInfo[T]) mse_loss()

mse_loss sets the loss function to the mean squared error loss.

fn (SequentialInfo[T]) sigmoid_cross_entropy_loss #

fn (mut ls SequentialInfo[T]) sigmoid_cross_entropy_loss()

sigmoid_cross_entropy_loss sets the loss function to the sigmoid cross entropy loss.

fn (SequentialInfo[T]) softmax_cross_entropy_loss #

fn (mut ls SequentialInfo[T]) softmax_cross_entropy_loss()

softmax_cross_entropy_loss sets the loss function to the softmax cross entropy loss.

fn (SequentialInfo[T]) flatten #

fn (mut ls SequentialInfo[T]) flatten()

flatten adds a new flatten layer to the network.

fn (SequentialInfo[T]) relu #

fn (mut ls SequentialInfo[T]) relu()

relu adds a new relu layer to the network.

fn (SequentialInfo[T]) leaky_relu #

fn (mut ls SequentialInfo[T]) leaky_relu()

leaky_relu adds a new leaky_relu layer to the network.

fn (SequentialInfo[T]) elu #

fn (mut ls SequentialInfo[T]) elu()

elu adds a new elu layer to the network.

fn (SequentialInfo[T]) sigmoid #

fn (mut ls SequentialInfo[T]) sigmoid()

sigmoid adds a new sigmoid layer to the network.

fn (SequentialInfo[T]) tanh #

fn (mut ls SequentialInfo[T]) tanh()

tanh adds a new tanh layer to the network.

fn (SequentialInfo[T]) softmax #

fn (mut ls SequentialInfo[T]) softmax()

softmax adds a new softmax layer to the network.

fn (SequentialInfo[T]) gelu #

fn (mut ls SequentialInfo[T]) gelu()

gelu adds a new GELU layer to the network.

fn (SequentialInfo[T]) swish #

fn (mut ls SequentialInfo[T]) swish()

swish adds a new Swish layer to the network.

fn (SequentialInfo[T]) mish #

fn (mut ls SequentialInfo[T]) mish()

mish adds a new Mish layer to the network.

fn (SequentialInfo[T]) conv2d #

fn (mut ls SequentialInfo[T]) conv2d(in_channels int, out_channels int, kernel_size []int, config layers.Conv2DConfig)

conv2d adds a new Conv2D layer to the network.

fn (SequentialInfo[T]) batchnorm1d #

fn (mut ls SequentialInfo[T]) batchnorm1d(num_features int, config layers.BatchNorm1DConfig)

batchnorm1d adds a new BatchNorm1D layer to the network.

fn (SequentialInfo[T]) avgpool2d #

fn (mut ls SequentialInfo[T]) avgpool2d(kernel []int, padding []int, stride []int)

avgpool2d adds a new AveragePool2D layer to the network.

fn (SequentialInfo[T]) global_avgpool2d #

fn (mut ls SequentialInfo[T]) global_avgpool2d()

global_avgpool2d adds a new GlobalAveragePool2D layer to the network.

fn (SequentialInfo[T]) layer_norm #

fn (mut ls SequentialInfo[T]) layer_norm(normalized_shape []int, config layers.LayerNormConfig)

layer_norm adds a new LayerNorm layer to the network.

fn (SequentialInfo[T]) embedding #

fn (mut ls SequentialInfo[T]) embedding(vocab_size int, embedding_dim int)

embedding adds a new Embedding layer to the network.

fn (SequentialInfo[T]) lstm #

fn (mut ls SequentialInfo[T]) lstm(input_size int, hidden_size int, num_layers int)

lstm adds a new LSTM layer to the network.

fn (SequentialInfo[T]) multihead_attention #

fn (mut ls SequentialInfo[T]) multihead_attention(embed_dim int, num_heads int)

multihead_attention adds a new MultiHeadAttention layer to the network.

fn (SequentialInfo[T]) positional_encoding #

fn (mut ls SequentialInfo[T]) positional_encoding(embed_dim int, max_len int)

positional_encoding adds a new PositionalEncoding layer to the network.

fn (SequentialInfo[T]) cross_entropy_loss #

fn (mut ls SequentialInfo[T]) cross_entropy_loss()

cross_entropy_loss sets the loss function to cross entropy loss.

fn (SequentialInfo[T]) bce_loss #

fn (mut ls SequentialInfo[T]) bce_loss()

bce_loss sets the loss function to binary cross entropy loss.

fn (SequentialInfo[T]) huber_loss #

fn (mut ls SequentialInfo[T]) huber_loss()

huber_loss sets the loss function to Huber loss.

fn (SequentialInfo[T]) nll_loss #

fn (mut ls SequentialInfo[T]) nll_loss()

nll_loss sets the loss function to negative log likelihood loss.

fn (SequentialInfo[T]) kl_div_loss #

fn (mut ls SequentialInfo[T]) kl_div_loss()

kl_div_loss sets the loss function to KL divergence loss.

fn (Sequential[T]) avgpool2d #

fn (mut nn Sequential[T]) avgpool2d(kernel []int, padding []int, stride []int)

avgpool2d adds a new AveragePool2D layer to the network.

fn (Sequential[T]) batchnorm1d #

fn (mut nn Sequential[T]) batchnorm1d(num_features int, config layers.BatchNorm1DConfig)

batchnorm1d adds a new BatchNorm1D layer to the network.

fn (Sequential[T]) bce_loss #

fn (mut nn Sequential[T]) bce_loss()

bce_loss sets the loss function to binary cross entropy loss.

fn (Sequential[T]) conv2d #

fn (mut nn Sequential[T]) conv2d(in_channels int, out_channels int, kernel_size []int, config layers.Conv2DConfig)

conv2d adds a new Conv2D layer to the network.

fn (Sequential[T]) cross_entropy_loss #

fn (mut nn Sequential[T]) cross_entropy_loss()

cross_entropy_loss sets the loss function to cross entropy loss.

fn (Sequential[T]) elu #

fn (mut nn Sequential[T]) elu()

elu adds a new elu layer to the network.

fn (Sequential[T]) embedding #

fn (mut nn Sequential[T]) embedding(vocab_size int, embedding_dim int)

embedding adds a new Embedding layer to the network.

fn (Sequential[T]) flatten #

fn (mut nn Sequential[T]) flatten()

flatten adds a new flatten layer to the network.

fn (Sequential[T]) forward #

fn (mut nn Sequential[T]) forward(train &autograd.Variable[T]) !&autograd.Variable[T]

forward exposes this operation as part of the public API.

fn (Sequential[T]) gelu #

fn (mut nn Sequential[T]) gelu()

gelu adds a new GELU layer to the network.

fn (Sequential[T]) global_avgpool2d #

fn (mut nn Sequential[T]) global_avgpool2d()

global_avgpool2d adds a new GlobalAveragePool2D layer to the network.

fn (Sequential[T]) huber_loss #

fn (mut nn Sequential[T]) huber_loss()

huber_loss sets the loss function to Huber loss.

fn (Sequential[T]) input #

fn (mut nn Sequential[T]) input(shape []int)

input adds a new input layer to the network with the given shape.

fn (Sequential[T]) kl_div_loss #

fn (mut nn Sequential[T]) kl_div_loss()

kl_div_loss sets the loss function to KL divergence loss.

fn (Sequential[T]) layer_norm #

fn (mut nn Sequential[T]) layer_norm(normalized_shape []int, config layers.LayerNormConfig)

layer_norm adds a new LayerNorm layer to the network.

fn (Sequential[T]) leaky_relu #

fn (mut nn Sequential[T]) leaky_relu()

leaky_relu adds a new leaky_relu layer to the network.

fn (Sequential[T]) linear #

fn (mut nn Sequential[T]) linear(output_size int)

linear adds a new linear layer to the network with the given output size

fn (Sequential[T]) load_weights #

fn (nn &Sequential[T]) load_weights(path string) !

load_weights restores weights into an existing model. The model's layers must already be constructed in the same order as when saved.

fn (Sequential[T]) loss #

fn (mut nn Sequential[T]) loss(output &autograd.Variable[T], target &vtl.Tensor[T]) !&autograd.Variable[T]

loss exposes this operation as part of the public API.

fn (Sequential[T]) lstm #

fn (mut nn Sequential[T]) lstm(input_size int, hidden_size int, num_layers int)

lstm adds a new LSTM layer to the network.

fn (Sequential[T]) maxpool2d #

fn (mut nn Sequential[T]) maxpool2d(kernel []int, padding []int, stride []int)

maxpool2d adds a new maxpool2d layer to the network with the given kernel size and stride.

fn (Sequential[T]) mish #

fn (mut nn Sequential[T]) mish()

mish adds a new Mish layer to the network.

fn (Sequential[T]) mse_loss #

fn (mut nn Sequential[T]) mse_loss()

mse_loss sets the loss function to the mean squared error loss.

fn (Sequential[T]) multihead_attention #

fn (mut nn Sequential[T]) multihead_attention(embed_dim int, num_heads int)

multihead_attention adds a new MultiHeadAttention layer to the network.

fn (Sequential[T]) nll_loss #

fn (mut nn Sequential[T]) nll_loss()

nll_loss sets the loss function to negative log likelihood loss.

fn (Sequential[T]) positional_encoding #

fn (mut nn Sequential[T]) positional_encoding(embed_dim int, max_len int)

positional_encoding adds a new PositionalEncoding layer to the network.

fn (Sequential[T]) relu #

fn (mut nn Sequential[T]) relu()

relu adds a new relu layer to the network.

fn (Sequential[T]) save #

fn (nn &Sequential[T]) save(path string) !

save saves a Sequential model's weights to a JSON file. Does NOT save optimizer state - use save_checkpoint for full state.

fn (Sequential[T]) save_checkpoint #

fn (nn &Sequential[T]) save_checkpoint(path string, epoch int, loss f64) !

save_checkpoint saves a Sequential model's weights and training metadata to a JSON file.

fn (Sequential[T]) sigmoid #

fn (mut nn Sequential[T]) sigmoid()

sigmoid adds a new sigmoid layer to the network.

fn (Sequential[T]) sigmoid_cross_entropy_loss #

fn (mut nn Sequential[T]) sigmoid_cross_entropy_loss()

sigmoid_cross_entropy_loss sets the loss function to the sigmoid cross entropy loss.

fn (Sequential[T]) softmax #

fn (mut nn Sequential[T]) softmax()

softmax adds a new softmax layer to the network.

fn (Sequential[T]) softmax_cross_entropy_loss #

fn (mut nn Sequential[T]) softmax_cross_entropy_loss()

softmax_cross_entropy_loss sets the loss function to the softmax cross entropy loss.

fn (Sequential[T]) swish #

fn (mut nn Sequential[T]) swish()

swish adds a new Swish layer to the network.

fn (Sequential[T]) tanh #

fn (mut nn Sequential[T]) tanh()

tanh adds a new tanh layer to the network.

struct ModelMetadata #

struct ModelMetadata {
pub:
	epoch    int
	loss     f64
	saved_at string
	version  string = model_version
}

ModelMetadata stores training metadata for checkpoints.

struct Sequential #

struct Sequential[T] {
pub mut:
	info &SequentialInfo[T] = unsafe { nil }
}

Sequential defines a public data structure for this module.

struct SequentialInfo #

struct SequentialInfo[T] {
	ctx &autograd.Context[T] = unsafe { nil }
pub mut:
	layers        []types.Layer[T]
	layer_types   []string
	layer_configs []map[string]int
	loss          types.Loss[T]
}

SequentialInfo defines a public data structure for this module.

struct SerializationError #

struct SerializationError {
	msg string
}

SerializationError represents errors during model save/load operations.

fn (SerializationError) msg #

fn (e &SerializationError) msg() string

msg exposes this operation as part of the public API.