Training loop
Loss, gradient accumulation, and NexusOptim.
01Order of a step#
zero_grad → forward → loss.backward → model.backward → optimizer.step. Gradients accumulate across backward calls. This enables micro-batch accumulation, but requires clearing between independent steps.
02Borrowed parameter bridge#
as_param_group passes data and gradient pointers only for requires_grad=true parameters. Do not replace parameter storage or destroy the model while the optimizer is alive. The adapter is included separately from the core.
#include <nexus_model/adapters/nexus_optim.hpp>#include <nexus_optim/algorithms/adamw.hpp>nexus_optim::ParamGroupOptions group;group.learning_rate = 1e-2;nexus_optim::AdamW<float>::Options options;options.lr = 1e-2;options.weight_decay = 1e-2;nexus_optim::AdamW<float> optimizer( {nexus_model::as_param_group(model, group)}, options);03From logits to gradients#
The example calls CrossEntropyLoss<float> per row. Copy backward output before the next row and divide by batch size. Pass logits to CrossEntropy; do not prepend Softmax.
04End-to-end example#
NexusData, NexusLoss, and NexusOptim must be sibling directories. The example uses 96 synthetic samples, 8 features, 3 classes, and 8 AdamW epochs. This is an integration check.
cmake -S . -B build-e2e -DNEXUS_MODEL_BUILD_EXAMPLES=ONcmake --build build-e2e --config Release.\build-e2e\Release\train_end_to_end.exe