docs/cpp/source/api/nn/containers.md
Container modules hold other modules and define how they are composed together. Use containers to build complex architectures from simpler building blocks.
PyTorch's C++ API uses the PIMPL (Pointer to Implementation) pattern. You create
modules using the public class name (e.g., `torch::nn::Sequential`), which
internally wraps an implementation class (`SequentialImpl`). The documentation
below shows the implementation classes, which contain all the actual methods.
Sequential is a container that chains modules together. Each module's output
becomes the next module's input. This is the simplest way to build feed-forward
networks.
:members:
:undoc-members:
Example:
torch::nn::Sequential seq(
torch::nn::Conv2d(torch::nn::Conv2dOptions(1, 32, 3)),
torch::nn::ReLU(),
torch::nn::Conv2d(torch::nn::Conv2dOptions(32, 64, 3)),
torch::nn::ReLU()
);
auto output = seq->forward(input);
ModuleList stores modules in a list for indexed or iterated access. Unlike
Sequential, it does not have a built-in forward() method—you control how
modules are called.
:members:
:undoc-members:
Example:
torch::nn::ModuleList layers;
layers->push_back(torch::nn::Linear(10, 20));
layers->push_back(torch::nn::Linear(20, 30));
torch::Tensor x = input;
for (const auto& layer : *layers) {
x = layer->as<torch::nn::Linear>()->forward(x);
}
ModuleDict stores modules in a dictionary for named access. Useful when you
need to select modules by name at runtime.
:members:
:undoc-members:
ParameterList stores parameters directly without wrapping them in modules.
:members:
:undoc-members:
:members:
:undoc-members:
ParameterDict stores parameters in a dictionary for named access.
:members:
:undoc-members:
:members:
:undoc-members: