Skip to content

tenzr::nn::Linear

Fully connected affine module applying a weight and optional bias.

#include <linear.h>

Inherits from tenzr::nn::Module

Public Functions

Name
Linear(int64_t in_features, int64_t out_features, bool use_bias, DType dtype, std::optional< uint64_t > seed =std::nullopt)
template <typename T =float>
Linear(int64_t in_features, int64_t out_features, bool use_bias =true, std::optional< uint64_t > seed =std::nullopt)
Construct with the parameter dtype inferred from T.
template <typename T >
Linear(std::type_identity< T > , int64_t in_features, int64_t out_features, 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 InFeatures() const
int64_t OutFeatures() const
bool HasBias() const
virtual std::string Name() const override
template <typename T =float>
Linear
Create(int64_t in_features, int64_t out_features, 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 Linear

Linear(
    int64_t in_features,
    int64_t out_features,
    bool use_bias,
    DType dtype,
    std::optional< uint64_t > seed =std::nullopt
)

Parameters:

  • in_features Number of input features.
  • out_features Number of output features.
  • use_bias Add a bias term when true.
  • dtype Parameter dtype.
  • seed Optional PRNG seed.
  • in_features Input feature count.
  • out_features Output feature count.
  • 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 linear layer with Glorot-uniform weights.

function Linear

template <typename T  =float>
inline Linear(
    int64_t in_features,
    int64_t out_features,
    bool use_bias =true,
    std::optional< uint64_t > seed =std::nullopt
)

Construct with the parameter dtype inferred from T.

function Linear

template <typename T >
inline explicit Linear(
    std::type_identity< T > ,
    int64_t in_features,
    int64_t out_features,
    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 (..., in_features).
  • input Input tensor of shape [..., in_features].

Return:

  • Output of shape (..., out_features).
  • Output tensor of shape [..., out_features].

Reimplements: tenzr::nn::Module::Forward

Apply the affine transform to input. Compute input @ weight + 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 InFeatures

inline int64_t InFeatures() const

Return: Number of input features.

function OutFeatures

inline int64_t OutFeatures() const

Return: Number of output features.

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: "Linear".

Reimplements: tenzr::nn::Module::Name

function Create

template <typename T  =float>
static inline Linear Create(
    int64_t in_features,
    int64_t out_features,
    bool use_bias =true,
    std::optional< uint64_t > seed =std::nullopt
)

Static factory mirroring the templated constructor.