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 |
| 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
function Forward¶
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¶
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 InFeatures¶
Return: Number of input features.
function OutFeatures¶
Return: Number of output features.
function HasBias¶
Return: true when the layer has a bias term.
function Name¶
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.