|
|
lucid
0.0.1
Lifting-based Uncertain Control Invariant Dynamics
|
Ridge regressor with a kernel function. More...
#include <KernelRidgeRegressor.h>
Public Member Functions | |
| KernelRidgeRegressor (const Kernel &kernel, Scalar regularization_constant=0, const std::shared_ptr< const Tuner > &tuner=nullptr) | |
| Construct a new Kernel Ridge Regressor object with the given parameters. | |
| KernelRidgeRegressor (std::unique_ptr< Kernel > &&kernel, Scalar regularization_constant=0, const std::shared_ptr< const Tuner > &tuner=nullptr) | |
| Construct a new Kernel Ridge Regressor object with the given parameters. | |
| Matrix | predict (ConstMatrixRef x) const override |
| A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X}
\) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \). | |
| Matrix | operator() (ConstMatrixRef x, const FeatureMap &feature_map) const |
| A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X}
\) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \). | |
| Matrix | predict (ConstMatrixRef x, const FeatureMap &feature_map) const |
| A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X}
\) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \). | |
| const std::unique_ptr< Kernel > & | kernel () const |
| Get read-only access to the kernel of the regressor. | |
| const Matrix & | training_inputs () const |
| Get read-only access to the training inputs of the regressor. | |
| const Matrix & | coefficients () const |
| Get read-only access to the coefficients of the regressor. | |
| double | regularization_constant () const |
| Get read-only access to the regularization constant of the regressor. | |
| double | log_marginal_likelihood () const |
| Get read-only access to the log marginal likelihood of the regressor. | |
| void | set (Parameter parameter, int value) override |
Set the parameter to the indicated value. | |
| void | set (Parameter parameter, double value) override |
Set the parameter to the indicated value. | |
| void | set (Parameter parameter, const Vector &value) override |
Set the parameter to the indicated value. | |
| Estimator & | consolidate_impl (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs, Requests requests) override |
| Concrete implementation of the consolidate method. | |
| double | score (ConstMatrixRef evaluation_inputs, ConstMatrixRef evaluation_outputs) const override |
Score the estimator assigning a numerical value to its accuracy in predicting the evaluation_outputs given the evaluation_inputs. | |
| std::unique_ptr< Estimator > | clone () const override |
| Clone the estimator by creating a new instance with the same parameters. | |
| std::string | to_string () const override |
| Obtain the string representation of this object. | |
| Matrix | operator() (ConstMatrixRef x) const |
| A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X}
\) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \). | |
| Estimator & | consolidate (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs, const Request request=Request::_) |
| Consolidate the model, making sure it is ready for use. | |
| Estimator & | consolidate (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs, Requests requests) |
| Consolidate the model, making sure it is ready for use. | |
| Estimator & | fit (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs, const Tuner &tuner) |
| Fit the model to the given data. | |
| Estimator & | fit (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs) |
| Fit the model to the given data. | |
Public Member Functions inherited from lucid::GradientOptimizable | |
| double | objective_value () const |
| Get read-only access to the objective value of the this object. | |
| const Vector & | gradient () const |
| Get read-only access to the gradient of the this object. | |
Public Member Functions inherited from lucid::Estimator | |
| Estimator (Parameters parameters=NoParameters, const std::shared_ptr< const Tuner > &tuner=nullptr) | |
| Construct a new Estimator object. | |
| Matrix | operator() (ConstMatrixRef x) const |
| A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X}
\) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \). | |
| Estimator & | fit (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs, const Tuner &tuner) |
| Fit the model to the given data. | |
| Estimator & | fit (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs) |
| Fit the model to the given data. | |
| Estimator & | fit_online (ConstMatrixRef training_inputs, const OutputComputer &training_outputs, const Tuner &tuner) |
Fit the model to the data produced by the training_outputs OutputComputer. | |
| Estimator & | fit_online (ConstMatrixRef training_inputs, const OutputComputer &training_outputs) |
Fit the model to the data produced by the training_outputs OutputComputer. | |
| Estimator & | consolidate (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs, const Request request=Request::_) |
| Consolidate the model, making sure it is ready for use. | |
| Estimator & | consolidate (ConstMatrixRef training_inputs, ConstMatrixRef training_outputs, Requests requests) |
| Consolidate the model, making sure it is ready for use. | |
| const std::shared_ptr< const Tuner > & | tuner () const |
| Get read-only access to the tuner of the estimator. | |
| std::shared_ptr< const Tuner > & | m_tuner () |
| Get read-write access to the tuner of the estimator. | |
Public Member Functions inherited from lucid::Parametrizable | |
| template<IsAnyOf< int, double, const Vector & > T> | |
| T | get (Parameter parameter) const |
Get the value of the specified parameter. | |
| template<Parameter P> | |
| internal::ParameterType< P >::ref_type | get () const |
Get the value of the specified parameter. | |
| std::variant< int, double, Vector > | get (Parameter parameter) const |
Get the value of the specified parameter. | |
| void | set (Parameter parameter, const std::variant< int, double, Vector > &value) |
Set the parameter to the indicated value. | |
| template<Parameter P> | |
| void | set (const std::variant< int, double, Vector > &value) |
Set the parameter to the indicated value. | |
| void | set (Parameter parameter, std::size_t idx, const std::variant< std::vector< int >, std::vector< double >, std::vector< Vector > > &values) |
Set the parameter to the index-th value among the indicated values. | |
| template<Parameter P> | |
| void | set (std::size_t idx, const std::variant< std::vector< int >, std::vector< double >, std::vector< Vector > > &values) |
Set the parameter to the index-th value among the indicated values. | |
| template<Parameter P> | |
| void | set (typename internal::ParameterType< P >::ref_type value) |
Set the parameter to the indicated value. | |
| bool | has (const Parameter parameter) const |
Check whether the parameter is present in this object. | |
| Parameters | parameters () const |
| Get read-only access to the parameters of the parametrizable object. | |
| std::vector< Parameter > | parameters_list () const |
| Get read-only access to the list of parameters of the parametrizable object. | |
| Parametrizable & | load (const Parametrizable &o) |
Load parameters from another Parametrizable object. | |
Private Member Functions | |
| int | get_i (Parameter parameter) const override |
Get the value of the specified parameter. | |
| double | get_d (Parameter parameter) const override |
Get the value of the specified parameter. | |
| const Vector & | get_v (Parameter parameter) const override |
Get the value of the specified parameter. | |
Private Attributes | |
| std::unique_ptr< Kernel > | kernel_ |
| Kernel function. | |
| double | regularization_constant_ |
| Regularization constant. | |
| Matrix | training_inputs_ |
| Training inputs. | |
| Matrix | coefficients_ |
| Coefficients of the linear combination describing the regression model. | |
Additional Inherited Members | |
Protected Attributes inherited from lucid::GradientOptimizable | |
| double | objective_value_ |
| Objective value of the optimisation problem. | |
| Vector | gradient_ |
| Gradient of the objective function. | |
Protected Attributes inherited from lucid::Parametrizable | |
| Parameters | parameters_ |
| Parameters supported by this object. | |
Ridge regressor with a kernel function.
This is a linear regressor with \( L_2 \) regularization. Given two vector spaces \( \mathcal{X} \subseteq \mathbb{R}^{d_x}, \mathcal{Y} \subseteq \mathbb{R}^{d_y} \) and the training datasets \( \{ (x_i, y_i) \}_{i=1}^n \), where \( x_i \in \mathcal{X} \) and \( y_i \in \mathcal{Y} \), the goal is to find the function \( f^*: \mathcal{X} \to \mathcal{Y} \) such that the sum of the squared errors is minimized, i.e.
\[ f^* = \arg\min_{f \in \mathcal{H}} \left\{ \frac{1}{n} \sum_{i=1}^n (y_i - f(x_i))^2 + \lambda \|f\|_{\mathcal{H}}^2 \right\} \]
where \( \mathcal{H} \) is a reproducing kernel Hilbert space (RKHS) with kernel \( k: \mathcal{X} \times \mathcal{X} \to \mathbb{R} \). Due to the reproducing property of the RKHS, \( f^* \) can be expressed as the linear combination
\[ f^*(x) = \sum_{i=1}^n w_i k(x_i, x) \]
where \( w \in \mathbb{R}^n \) are some coefficients we want to find. We can rewrite the loss function in vector form as
\[\frac{1}{n} \|y - Kw\|^2 + \lambda w^T K w \]
where \( K \) is the Gram matrix with elements \( K_{ij} = k(x_i, x_j) \). Looking for the minimum, the partial gradient of the loss function with respect to \( w \) is set to zero
\[\frac{\partial}{\partial w} \left\{ \frac{1}{n} \|y - Kw\|^2 + \lambda w^T K w \right\} = 0 \]
leads us to the closed-form solution:
\[w = (K + \lambda n I)^{-1} y . \]
Finally, when we want to predict the output for a new input \( x \), we can just compute
\[f^*(x) = k(x, x_1) w_1 + k(x, x_2) w_2 + \cdots + k(x, x_n) w_n \]
of, in matrix form
\[f^*(x) = K(x, x_\text{traning}) w \]
where \( K(x, x_\text{traning}) \) is the vector of kernel evaluations between \( x \) and the training inputs \( x_\text{traning} \).
|
explicit |
Construct a new Kernel Ridge Regressor object with the given parameters.
| kernel | kernel function used to compute the Gram matrix |
| regularization_constant | regularization constant. Avoids overfitting by penalizing large coefficients |
| tuner | tuner used to fit the model |
|
explicit |
Construct a new Kernel Ridge Regressor object with the given parameters.
| kernel | kernel function used to compute the Gram matrix |
| regularization_constant | regularization constant. Avoids overfitting by penalizing large coefficients |
| tuner | tuner used to fit the model |
|
nodiscardoverridevirtual |
Clone the estimator by creating a new instance with the same parameters.
Implements lucid::Estimator.
|
inlinenodiscard |
Get read-only access to the coefficients of the regressor.
|
inline |
Consolidate the model, making sure it is ready for use.
No fitting process is performed, and the hyperparameters are not updated, but the estimator may change its internal state so it can be used for predictions. After the process is completed, the estimator can be used to make predictions on new data. The request parameter is used to specify an additional option for the consolidation process, like whether to compute some additional values used by some tuners.
| training_inputs | training input data. The number of rows should be equal to the number of training outputs |
| training_outputs | training output data. The number of rows should be equal to the number of training inputs |
| request | request for the consolidation process. Defaults to no specific request being made |
| Estimator & lucid::Estimator::consolidate | ( | ConstMatrixRef | training_inputs, |
| ConstMatrixRef | training_outputs, | ||
| Requests | requests ) |
Consolidate the model, making sure it is ready for use.
No fitting process is performed, and the hyperparameters are not updated, but the estimator may change its internal state so it can be used for predictions. After the process is completed, the estimator can be used to make predictions on new data. The requests parameter can be used to specify additional options for the consolidation process, like whether to compute some additional values used by some tuners.
| training_inputs | training input data |
| training_outputs | training output data |
| requests | requests for the consolidation process |
|
overridevirtual |
Concrete implementation of the consolidate method.
| training_inputs | training input data |
| training_outputs | training output data |
| requests | requests for the consolidation process |
Implements lucid::Estimator.
| Estimator & lucid::Estimator::fit | ( | ConstMatrixRef | training_inputs, |
| ConstMatrixRef | training_outputs ) |
Fit the model to the given data.
This method will use the object's tuner to find the best hyperparameters for the model. After the process is completed, the estimator can be used to make predictions on new data. If no tuner has been provided during construction, the method is equivalent to consolidate with no specific request.
| training_inputs | training input data |
| training_outputs | training output data |
| Estimator & lucid::Estimator::fit | ( | ConstMatrixRef | training_inputs, |
| ConstMatrixRef | training_outputs, | ||
| const Tuner & | tuner ) |
Fit the model to the given data.
This method will use the provided tuner to find the best hyperparameters for the model. After the process is completed, the estimator can be used to make predictions on new data.
| training_inputs | training input data |
| training_outputs | training output data |
| tuner | Tuner object used to find the best hyperparameters for the model |
|
nodiscardoverrideprivatevirtual |
Get the value of the specified parameter.
| parameter | parameter to retrieve |
parameter must be present and be associated with a value of type double. Reimplemented from lucid::Parametrizable.
|
nodiscardoverrideprivatevirtual |
Get the value of the specified parameter.
| parameter | parameter to retrieve |
parameter must be present and be associated with a value of type int. Reimplemented from lucid::Parametrizable.
|
nodiscardoverrideprivatevirtual |
Get the value of the specified parameter.
| parameter | parameter to retrieve |
parameter must be present and be associated with a value of type Vector. Reimplemented from lucid::Parametrizable.
|
inlinenodiscard |
Get read-only access to the kernel of the regressor.
|
inlinenodiscard |
Get read-only access to the log marginal likelihood of the regressor.
|
nodiscard |
A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X} \) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \).
| x | \( n \times d_x \) matrix of row vectors in \( \mathcal{X} \) |
|
nodiscard |
A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X} \) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \).
The feature_map is used to approximate the kernel vector \( k(x, x_i) \).
| x | \( n \times d_x \) matrix of row vectors in \( \mathcal{X} \) |
| feature_map | feature map used to approximate the kernel vector \( k(x, x_i) \) |
|
nodiscardoverridevirtual |
A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X} \) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \).
| x | \( n \times d_x \) matrix of row vectors in \( \mathcal{X} \) |
Implements lucid::Estimator.
|
nodiscard |
A model is a function that takes a \( n \times d_x \) matrix of row vectors in the input space \( \mathcal{X} \) and returns a \( n \times d_y \) matrix of row vectors in the output space \( \mathcal{Y} \).
The feature_map is used to approximate the kernel vector \( k(x, x_i) \).
| x | \( n \times d_x \) matrix of row vectors in \( \mathcal{X} \) |
| feature_map | feature map used to approximate the kernel vector \( k(x, x_i) \) |
|
inlinenodiscard |
Get read-only access to the regularization constant of the regressor.
|
nodiscardoverridevirtual |
Score the estimator assigning a numerical value to its accuracy in predicting the evaluation_outputs given the evaluation_inputs.
Given the evaluation inputs \( x = \{ x_1, \dots, x_n \} \), where \( x_i \in \mathcal{X} \subseteq \mathbb{R}^{d_x}, 0 \le i \le n \), we want to give a numerical score to the model's predictions \( \hat{y} = \{ \hat{y}_1, \dots, \hat{y}_n \} \) with respect to the true outputs \( y = \{ y_1, \dots, y_n \} \), where \( y_i \in \mathcal{Y} \subseteq \mathbb{R}^{d_y}, 0 \le i \le n \).
| evaluation_inputs | \( \texttip{n}{Number of samples} \times \texttip{d_x}{Dimension of the input vector space} \) evaluation input data |
| evaluation_outputs | \( \texttip{n}{Number of samples} \times \texttip{d_y}{Dimension of the output vector space} \) evaluation output data |
Implements lucid::Estimator.
|
overridevirtual |
Set the parameter to the indicated value.
| parameter | parameter to set |
| value | value to assign to the specified parameter |
parameter must be present and be associated with a value of type Vector. Reimplemented from lucid::Parametrizable.
|
overridevirtual |
Set the parameter to the indicated value.
| parameter | parameter to set |
| value | value to assign to the specified parameter |
parameter must be present and be associated with a value of type double. Reimplemented from lucid::Parametrizable.
|
overridevirtual |
Set the parameter to the indicated value.
| parameter | parameter to set |
| value | value to assign to the specified parameter |
parameter must be present and be associated with a value of type int. Reimplemented from lucid::Parametrizable.
|
nodiscardoverridevirtual |
Obtain the string representation of this object.
Reimplemented from lucid::Estimator.
|
inlinenodiscard |
Get read-only access to the training inputs of the regressor.