Model containers
Sequential, collections, and residual connections.
01Sequential#
Sequential(vector<shared_ptr<Module>>) runs forward in order and backward in reverse. add appends a layer. Intermediate activations are not cached separately. An empty Sequential passes input through.
02ModuleList / ModuleDict#
Registered collections whose own forward/backward throw ModelError. ModuleList provides indexing or at(index); ModuleDict provides name lookup. You implement execution flow; parameter traversal and train/eval still work.
ModuleDict heads;heads.add("classify", std::make_shared<Linear>(64,10));auto x=Tensor::zeros({4,64});auto y=heads["classify"]->forward(x);03ResidualBlock#
ResidualBlock(shared_ptr<Module>) computes y=F(x)+x. Shapes must match; there is no automatic projection. The identity-path derivative adds directly to the gradient.
04Flatten / Reshape#
Flatten maps rank≥2 input to [N,remaining] and uses memcpy in the implementation, despite a zero-copy header comment. Reshape({concrete dimensions}) returns a view in forward and copies into the old shape in backward.