tenzr::nn::Conv2D¶
2-D convolution module with configurable kernel, stride, and padding.
#include <conv2d.h>
Inherits from tenzr::nn::Module
Public Functions¶
| Name | |
|---|---|
| Conv2D(int64_t in_channels, int64_t out_channels, std::pair< int64_t, int64_t > kernel_size, std::pair< int64_t, int64_t > stride, std::pair< int64_t, int64_t > padding, bool use_bias, DType dtype, std::optional< uint64_t > seed =std::nullopt) | |
| template <typename T =float> |
Conv2D(int64_t in_channels, int64_t out_channels, std::pair< int64_t, int64_t > kernel_size, std::pair< int64_t, int64_t > stride ={1, 1}, std::pair< int64_t, int64_t > padding ={0, 0}, bool use_bias =true, std::optional< uint64_t > seed =std::nullopt) Construct with the parameter dtype inferred from T. |
| template <typename T > |
Conv2D(std::type_identity< T > , int64_t in_channels, int64_t out_channels, std::pair< int64_t, int64_t > kernel_size, std::pair< int64_t, int64_t > stride ={1, 1}, std::pair< int64_t, int64_t > padding ={0, 0}, bool use_bias =true, std::optional< uint64_t > seed =std::nullopt) Construct from an explicit std::type_identity |
| virtual Tensor | Forward(const Tensor & input) override |
| Tensor & | Weight() |
| const Tensor & | Weight() const |
| Tensor & | Bias() |
| const Tensor & | Bias() const |
| int64_t | InChannels() const |
| int64_t | OutChannels() const |
| std::pair< int64_t, int64_t > | KernelSize() const |
| std::pair< int64_t, int64_t > | Stride() const |
| std::pair< int64_t, int64_t > | Padding() const |
| bool | HasBias() const |
| virtual std::string | Name() const override |
| template <typename T =float> Conv2D |
Create(int64_t in_channels, int64_t out_channels, std::pair< int64_t, int64_t > kernel_size, std::pair< int64_t, int64_t > stride ={1, 1}, std::pair< int64_t, int64_t > padding ={0, 0}, bool use_bias =true, std::optional< uint64_t > seed =std::nullopt) Static factory mirroring the templated constructor. |
Additional inherited members¶
Public Functions inherited from tenzr::nn::Module
| Name | |
|---|---|
| virtual | ~Module() =default |
| Tensor | operator()(const Tensor & input) |
| virtual void | Train(bool mode) |
| void | Train() Switch to training mode. |
| void | Eval() Switch to evaluation mode. |
| bool | IsTraining() const |
| Tensor & | RegisterParameter(std::string name, Tensor param) |
| template <typename T ,typename... Args> std::shared_ptr< T > |
RegisterSubmodule(std::string name, Args &&... args) |
| std::shared_ptr< Module > | RegisterSubmodule(std::string name, std::shared_ptr< Module > submod) |
| std::vector< Tensor > | Parameters(bool recurse =true) const |
| std::unordered_map< std::string, Tensor > | NamedParameters(std::string prefix ="", bool recurse =true) const |
| std::unordered_map< std::string, Tensor > | StateDict() const |
| void | LoadStateDict(const std::unordered_map< std::string, Tensor > & state) |
Protected Attributes inherited from tenzr::nn::Module
| Name | |
|---|---|
| std::unordered_map< std::string, Tensor > | parameters_ |
| std::unordered_map< std::string, std::shared_ptr< Module > > | submodules_ |
| bool | training_ |
Public Functions Documentation¶
function Conv2D¶
Conv2D(
int64_t in_channels,
int64_t out_channels,
std::pair< int64_t, int64_t > kernel_size,
std::pair< int64_t, int64_t > stride,
std::pair< int64_t, int64_t > padding,
bool use_bias,
DType dtype,
std::optional< uint64_t > seed =std::nullopt
)
Parameters:
- in_channels Number of input channels.
- out_channels Number of output channels.
- kernel_size (height, width) of the kernel.
- stride (height, width) stride.
- padding (height, width) zero padding.
- use_bias Add a bias term when true.
- dtype Parameter dtype.
- seed Optional PRNG seed.
- in_channels Number of input channels.
- out_channels Number of output filters.
- kernel_size Kernel height and width.
- stride Convolution stride.
- padding Zero padding.
- use_bias Whether to allocate a bias term.
- dtype Parameter dtype.
- seed Optional seed for weight initialization.
Construct with an explicit runtime dtype. Create a 2D convolution with Kaiming-uniform weights.
function Conv2D¶
template <typename T =float>
inline Conv2D(
int64_t in_channels,
int64_t out_channels,
std::pair< int64_t, int64_t > kernel_size,
std::pair< int64_t, int64_t > stride ={1, 1},
std::pair< int64_t, int64_t > padding ={0, 0},
bool use_bias =true,
std::optional< uint64_t > seed =std::nullopt
)
Construct with the parameter dtype inferred from T.
function Conv2D¶
template <typename T >
inline explicit Conv2D(
std::type_identity< T > ,
int64_t in_channels,
int64_t out_channels,
std::pair< int64_t, int64_t > kernel_size,
std::pair< int64_t, int64_t > stride ={1, 1},
std::pair< int64_t, int64_t > padding ={0, 0},
bool use_bias =true,
std::optional< uint64_t > seed =std::nullopt
)
Construct from an explicit std::type_identity
function Forward¶
Parameters:
- input Input of shape (N, in_channels, H, W).
- input Input tensor in NCHW layout.
Return:
- Output of shape (N, out_channels, H_out, W_out).
- Output tensor of shape [N, out_channels, out_h, out_w].
Reimplements: tenzr::nn::Module::Forward
Apply the convolution to input. Convolve a rank-4 [N, C, H, W] input with the layer's filters, adding bias.
function Weight¶
Return: Mutable reference to the weight tensor.
function Weight¶
Return: Const reference to the weight tensor.
function Bias¶
Return: Mutable reference to the bias tensor.
function Bias¶
Return: Const reference to the bias tensor.
function InChannels¶
Return: Number of input channels.
function OutChannels¶
Return: Number of output channels.
function KernelSize¶
Return: (height, width) kernel size.
function Stride¶
Return: (height, width) stride.
function Padding¶
Return: (height, width) zero padding.
function HasBias¶
Return: true when the layer has a bias term.
function Name¶
Return: "Conv2D".
Reimplements: tenzr::nn::Module::Name
function Create¶
template <typename T =float>
static inline Conv2D Create(
int64_t in_channels,
int64_t out_channels,
std::pair< int64_t, int64_t > kernel_size,
std::pair< int64_t, int64_t > stride ={1, 1},
std::pair< int64_t, int64_t > padding ={0, 0},
bool use_bias =true,
std::optional< uint64_t > seed =std::nullopt
)
Static factory mirroring the templated constructor.