Skip to content

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 dtype tag.
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 dtype tag.

function Forward

virtual Tensor Forward(
    const Tensor & input
) override

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

inline Tensor & Weight()

Return: Mutable reference to the weight tensor.

function Weight

inline const Tensor & Weight() const

Return: Const reference to the weight tensor.

function Bias

inline Tensor & Bias()

Return: Mutable reference to the bias tensor.

function Bias

inline const Tensor & Bias() const

Return: Const reference to the bias tensor.

function InChannels

inline int64_t InChannels() const

Return: Number of input channels.

function OutChannels

inline int64_t OutChannels() const

Return: Number of output channels.

function KernelSize

inline std::pair< int64_t, int64_t > KernelSize() const

Return: (height, width) kernel size.

function Stride

inline std::pair< int64_t, int64_t > Stride() const

Return: (height, width) stride.

function Padding

inline std::pair< int64_t, int64_t > Padding() const

Return: (height, width) zero padding.

function HasBias

inline bool HasBias() const

Return: true when the layer has a bias term.

function Name

inline virtual std::string Name() const override

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.