docs/cpp/source/api/nn/functional.md
The torch::nn::functional namespace provides stateless versions of neural
network operations. Unlike module classes, functional operations do not hold
learnable parameters — you pass weights explicitly.
When to use functional vs modules:
torch::nn::Conv2d) when you need learnable parameters
managed automatically (training, saving, loading).torch::nn::functional::conv2d) when you already have
weights as tensors, or for operations without parameters (e.g., relu).#include <torch/nn/functional.h>
namespace F = torch::nn::functional;
// Stateless activation — no module needed
auto output = F::relu(input);
// Convolution with explicit weight tensor
auto output = F::conv2d(input, weight, F::Conv2dFuncOptions().stride(1).padding(1));
// Softmax along a dimension
auto probs = F::softmax(logits, F::SoftmaxFuncOptions(/*dim=*/1));
Each functional operation that takes configuration uses a corresponding options
struct. The naming convention is <Operation>FuncOptions.
Activation Options:
Convolution Options:
Pooling Options:
Other Options: