std1.0.0-beta-7 SafeC docs

Module ml::trainer

#include <std/ml/trainer.h> source

the training loop.

trainer_fit runs epochs of mini-batch gradient descent over a Dataset: for each batch it calls your loss function, back-propagates, optionally clips the gradient norm, and steps the optimizer. Each step's tensors go to a TensorArena that is reset after the step, so a long run allocates nothing per step once the stepArena has warmed up. The model can be any struct: the loss function receives it, the batch inputs and the batch targets, and returns a 1-element loss tensor.

static &Tensor clf_loss(&Mlp m, const &Tensor x, const &Tensor y) {
    return tensor_cross_entropy_loss(m.forward(x), y);
}
struct Optimizer opt = Optimizer::new(OPT_ADAM, (float)0.01);
model_add_params(&net, &opt);   // module.h
struct TrainConfig cfg = TrainConfig::default();
struct TrainReport rep = trainer_fit(&net, clf_loss, &opt, &train, &cfg);

The model, the forward pass and the loss are all resolved at compile time (trainer_fit is generic over the model type). For a built-in loss on any Module, or a model's own loss (autoencoders, VAEs), module.h's module_fit / model_fit need no loss function at all.

The same loops train on a GPU (device.h): set TrainConfig.device, or place the model with model_to; every op then runs where its operands live.

Structs

TrainConfig
TrainReport
TrainLossSumInternal: an epoch's running loss sum, kept where the losses are -- a scalar on the device for a device's losses, read once when the epoch ends -- so a step on a GPU doesn't wait for its loss to reach the host.

Functions

trainer_fit
trainer_evaluateMean loss over a dataset without gradients (validation).
trainer_accuracyClassification accuracy over a dataset: 'logitsFn' maps a batch to [rows, classes] logits.