Txeo v0.1
A Developer-Friendly TensorFlow C++ Wrapper
Loading...
Searching...
No Matches
txeo::Loss< T > Class Template Reference

Computes error metrics between predicted and validation tensors. More...

#include <Loss.h>

Collaboration diagram for txeo::Loss< T >:
Collaboration graph

Public Member Functions

 Loss (const Loss &)=default
 
 Loss (Loss &&)=default
 
Lossoperator= (const Loss &)=default
 
Lossoperator= (Loss &&)=default
 
 ~Loss ()=default
 
 Loss (txeo::Tensor< T > &&label, txeo::LossFunc func=txeo::LossFunc::MSE)
 Construct a new Loss object from a label rvalue.
 
 Loss (const txeo::Tensor< T > &label, txeo::LossFunc func=txeo::LossFunc::MSE)
 Construct a new Loss object.
 
T get_loss (const txeo::Tensor< T > &pred) const
 Compute loss using currently selected function.
 
void set_loss (txeo::LossFunc func)
 Set the active loss function.
 
T mean_squared_error (const txeo::Tensor< T > &pred) const
 Compute Mean Squared Error (MSE)
 
T mean_absolute_error (const txeo::Tensor< T > &pred) const
 Compute Mean Absolute Error (MAE)
 
T mean_squared_logarithmic_error (const txeo::Tensor< T > &pred) const
 Compute Mean Squared Logarithmic Error (MSLE)
 
T log_cosh_error (const txeo::Tensor< T > &pred) const
 Compute Log-Cosh Error (LCHE)
 
const txeo::Tensor< T > & label () const
 
Shorthand Aliases
T mse (const txeo::Tensor< T > &pred) const
 
T mae (const txeo::Tensor< T > &pred) const
 
T msle (const txeo::Tensor< T > &pred) const
 
T lche (const txeo::Tensor< T > &pred) const
 

Detailed Description

template<typename T>
class txeo::Loss< T >

Computes error metrics between predicted and validation tensors.

Supports multiple loss functions that can be selected at runtime:

  • MSE (Mean Squared Error)
  • MAE (Mean Absolute Error)
  • MSLE (Mean Squared Logarithmic Error)
  • LCHE (Log-Cosh Error)
Template Parameters
TNumeric type of tensor elements (float/double recommended)
Note
All operations validate tensor shape compatibility between predictions and validation data;
First axis's dimension of the tensors involved must refer to number of samples

Definition at line 30 of file Loss.h.

Constructor & Destructor Documentation

◆ Loss() [1/4]

template<typename T >
txeo::Loss< T >::Loss ( const Loss< T > &  )
default

◆ Loss() [2/4]

template<typename T >
txeo::Loss< T >::Loss ( Loss< T > &&  )
default

◆ ~Loss()

template<typename T >
txeo::Loss< T >::~Loss ( )
default

◆ Loss() [3/4]

template<typename T >
txeo::Loss< T >::Loss ( txeo::Tensor< T > &&  label,
txeo::LossFunc  func = txeo::LossFunc::MSE 
)
explicit

Construct a new Loss object from a label rvalue.

Parameters
labelLabe tensor containing ground truth values
funcInitial loss function (default: MSE)
Example:
// Create with MAE as default loss
A container for managing training, evaluation, and test data splits.
Definition DataTable.h:24

◆ Loss() [4/4]

template<typename T >
txeo::Loss< T >::Loss ( const txeo::Tensor< T > &  label,
txeo::LossFunc  func = txeo::LossFunc::MSE 
)
inlineexplicit

Construct a new Loss object.

Parameters
labelLabel tensor containing ground truth values
funcInitial loss function (default: MSE)
Example:
// Create with MAE as default loss

Definition at line 64 of file Loss.h.

65 : Loss{label.clone(), func} {};
DataTable< T > clone() const
const txeo::Tensor< T > & label() const
Definition Loss.h:136
Loss(const Loss &)=default

Member Function Documentation

◆ get_loss()

template<typename T >
T txeo::Loss< T >::get_loss ( const txeo::Tensor< T > &  pred) const

Compute loss using currently selected function.

Parameters
predPrediction tensor
Returns
T Calculated loss value
Exceptions
LossErrorIf shapes mismatch or invalid input values
Example:
auto error = loss.get_loss(predictions);
std::cout << "Current loss: " << error << std::endl;

◆ label()

template<typename T >
const txeo::Tensor< T > & txeo::Loss< T >::label ( ) const
inline

Definition at line 136 of file Loss.h.

136{ return _label; }

◆ lche()

template<typename T >
T txeo::Loss< T >::lche ( const txeo::Tensor< T > &  pred) const
inline
See also
mean_squared_logarithmic_error

Definition at line 152 of file Loss.h.

152 {
153 return log_cosh_error(pred);
154 };
T log_cosh_error(const txeo::Tensor< T > &pred) const
Compute Log-Cosh Error (LCHE)
Here is the call graph for this function:

◆ log_cosh_error()

template<typename T >
T txeo::Loss< T >::log_cosh_error ( const txeo::Tensor< T > &  pred) const

Compute Log-Cosh Error (LCHE)

\[ LCHE = \frac{1}{N}\sum_{i=1}^{N}\log(\cosh(y_i - \hat{y}_i)) \]

Parameters
predPrediction tensor
Returns
T LCHE value
Here is the caller graph for this function:

◆ mae()

template<typename T >
T txeo::Loss< T >::mae ( const txeo::Tensor< T > &  pred) const
inline
See also
mean_squared_error

Definition at line 144 of file Loss.h.

144 {
145 return mean_absolute_error(pred);
146 };
T mean_absolute_error(const txeo::Tensor< T > &pred) const
Compute Mean Absolute Error (MAE)
Here is the call graph for this function:

◆ mean_absolute_error()

template<typename T >
T txeo::Loss< T >::mean_absolute_error ( const txeo::Tensor< T > &  pred) const

Compute Mean Absolute Error (MAE)

\[ MAE = \frac{1}{N}\sum_{i=1}^{N}|y_i - \hat{y}_i| \]

Parameters
predPrediction tensor
Returns
T MAE value
Here is the caller graph for this function:

◆ mean_squared_error()

template<typename T >
T txeo::Loss< T >::mean_squared_error ( const txeo::Tensor< T > &  pred) const

Compute Mean Squared Error (MSE)

\[ MSE = \frac{1}{N}\sum_{i=1}^{N}(y_i - \hat{y}_i)^2 \]

Parameters
predPrediction tensor
Returns
T MSE value
Here is the caller graph for this function:

◆ mean_squared_logarithmic_error()

template<typename T >
T txeo::Loss< T >::mean_squared_logarithmic_error ( const txeo::Tensor< T > &  pred) const

Compute Mean Squared Logarithmic Error (MSLE)

\[ MSLE = \frac{1}{N}\sum_{i=1}^{N}(\log(1+y_i) - \log(1+\hat{y}_i))^2 \]

Parameters
predPrediction tensor
Returns
T MSLE value
Exceptions
LossErrorIf any values are negative
Here is the caller graph for this function:

◆ mse()

template<typename T >
T txeo::Loss< T >::mse ( const txeo::Tensor< T > &  pred) const
inline

Definition at line 140 of file Loss.h.

140 {
141 return mean_squared_error(pred);
142 };
T mean_squared_error(const txeo::Tensor< T > &pred) const
Compute Mean Squared Error (MSE)
Here is the call graph for this function:

◆ msle()

template<typename T >
T txeo::Loss< T >::msle ( const txeo::Tensor< T > &  pred) const
inline
See also
mean_absolute_error

Definition at line 148 of file Loss.h.

148 {
150 };
T mean_squared_logarithmic_error(const txeo::Tensor< T > &pred) const
Compute Mean Squared Logarithmic Error (MSLE)
Here is the call graph for this function:

◆ operator=() [1/2]

template<typename T >
Loss & txeo::Loss< T >::operator= ( const Loss< T > &  )
default

◆ operator=() [2/2]

template<typename T >
Loss & txeo::Loss< T >::operator= ( Loss< T > &&  )
default

◆ set_loss()

template<typename T >
void txeo::Loss< T >::set_loss ( txeo::LossFunc  func)

Set the active loss function.

Parameters
funcLoss function to use for subsequent calculations
Example:
// Switch to log-cosh error

The documentation for this class was generated from the following file: