Skip to content

tenzr::nn::Module

Base class for neural-network layers holding parameters, submodules, and training state.

#include <module.h>

Inherited by tenzr::nn::BatchNorm, tenzr::nn::Conv2D, tenzr::nn::Dropout, tenzr::nn::Embedding, tenzr::nn::GELU, tenzr::nn::LayerNorm, tenzr::nn::Linear, tenzr::nn::LogSoftmax, tenzr::nn::RMSNorm, tenzr::nn::ReLU, tenzr::nn::Sequential, tenzr::nn::SiLU, tenzr::nn::Sigmoid, tenzr::nn::Softmax, tenzr::nn::Tanh

Public Functions

Name
virtual ~Module() =default
virtual Tensor Forward(const Tensor & input) =0
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)
virtual std::string Name() const

Protected Attributes

Name
std::unordered_map< std::string, Tensor > parameters_
std::unordered_map< std::string, std::shared_ptr< Module > > submodules_
bool training_

Public Functions Documentation

function ~Module

virtual ~Module() =default

function Forward

virtual Tensor Forward(
    const Tensor & input
) =0

Parameters:

  • input Input tensor.

Return: Module output.

Reimplemented by: tenzr::nn::Embedding::Forward, tenzr::nn::ReLU::Forward, tenzr::nn::Sigmoid::Forward, tenzr::nn::Tanh::Forward, tenzr::nn::SiLU::Forward, tenzr::nn::GELU::Forward, tenzr::nn::Softmax::Forward, tenzr::nn::LogSoftmax::Forward, tenzr::nn::Conv2D::Forward, tenzr::nn::Dropout::Forward, tenzr::nn::Linear::Forward, tenzr::nn::LayerNorm::Forward, tenzr::nn::RMSNorm::Forward, tenzr::nn::BatchNorm::Forward, tenzr::nn::Sequential::Forward

Run the forward pass.

function operator()

inline Tensor operator()(
    const Tensor & input
)

function Train

inline virtual void Train(
    bool mode
)

Parameters:

  • mode true for training, false for evaluation.

Set training mode and propagate it to all submodules.

function Train

inline void Train()

Switch to training mode.

function Eval

inline void Eval()

Switch to evaluation mode.

function IsTraining

inline bool IsTraining() const

Return: true when the module is in training mode.

function RegisterParameter

inline Tensor & RegisterParameter(
    std::string name,
    Tensor param
)

Parameters:

  • name Parameter name.
  • param Parameter tensor.

Return: Reference to the stored parameter.

Register a named trainable parameter.

function RegisterSubmodule

template <typename T ,
typename... Args>
inline std::shared_ptr< T > RegisterSubmodule(
    std::string name,
    Args &&... args
)

Parameters:

  • name Submodule name.
  • args Forwarded constructor arguments.

Return: Shared pointer to the new submodule.

Construct and register a submodule of type T.

function RegisterSubmodule

inline std::shared_ptr< Module > RegisterSubmodule(
    std::string name,
    std::shared_ptr< Module > submod
)

Parameters:

  • name Submodule name.
  • submod Submodule to register.

Return: The registered submodule.

Register an existing submodule instance.

function Parameters

inline std::vector< Tensor > Parameters(
    bool recurse =true
) const

Parameters:

  • recurse Include submodule parameters when true.

Return: Flat list of parameter tensors.

Collect all registered parameters.

function NamedParameters

inline std::unordered_map< std::string, Tensor > NamedParameters(
    std::string prefix ="",
    bool recurse =true
) const

Parameters:

  • prefix Prefix prepended to each name.
  • recurse Include submodule parameters when true.

Return: Map from name to parameter tensor.

Collect parameters keyed by dotted fully-qualified name.

function StateDict

inline std::unordered_map< std::string, Tensor > StateDict() const

Return: Map from name to cloned parameter tensor.

Build a deep copy of all named parameters.

function LoadStateDict

inline void LoadStateDict(
    const std::unordered_map< std::string, Tensor > & state
)

Parameters:

  • state Map from name to parameter tensor.

Copy values from state into matching parameters.

function Name

inline virtual std::string Name() const

Return: Human-readable module name.

Reimplemented by: tenzr::nn::ReLU::Name, tenzr::nn::Sigmoid::Name, tenzr::nn::Tanh::Name, tenzr::nn::SiLU::Name, tenzr::nn::GELU::Name, tenzr::nn::Softmax::Name, tenzr::nn::LogSoftmax::Name, tenzr::nn::Conv2D::Name, tenzr::nn::Dropout::Name, tenzr::nn::Embedding::Name, tenzr::nn::Linear::Name, tenzr::nn::LayerNorm::Name, tenzr::nn::RMSNorm::Name, tenzr::nn::BatchNorm::Name, tenzr::nn::Sequential::Name

Protected Attributes Documentation

variable parameters_

std::unordered_map< std::string, Tensor > parameters_;

variable submodules_

std::unordered_map< std::string, std::shared_ptr< Module > > submodules_;

variable training_

bool training_ {true};