https://mooseframework.inl.gov
GaussianProcess.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "Standardizer.h"
13 #include <Eigen/Dense>
14 
15 #include "CovarianceFunctionBase.h"
16 
17 namespace StochasticTools
18 {
19 
26 {
27 public:
29 
40  void initialize(CovarianceFunctionBase * covariance_function,
41  const std::vector<std::string> & params_to_tune,
42  const std::vector<Real> & min = std::vector<Real>(),
43  const std::vector<Real> & max = std::vector<Real>());
44 
48  {
65  const unsigned int num_iter = 1000,
66  const unsigned int batch_size = 0,
67  const Real learning_rate = 1e-3,
68  const Real b1 = 0.9,
69  const Real b2 = 0.999,
70  const Real eps = 1e-7,
71  const Real lambda = 0.0);
72 
74  const unsigned int show_every_nth_iteration = false;
76  const unsigned int num_iter = 1000;
78  const unsigned int batch_size = 0;
80  const Real learning_rate = 1e-3;
82  const Real b1 = 0.9;
84  const Real b2 = 0.999;
86  const Real eps = 1e-7;
88  const Real lambda = 0.0;
89  };
98  void setupCovarianceMatrix(const RealEigenMatrix & training_params,
99  const RealEigenMatrix & training_data,
100  const GPOptimizerOptions & opts);
101 
106  void setupStoredMatrices(const RealEigenMatrix & input);
107 
114  void linkCovarianceFunction(CovarianceFunctionBase * covariance_function);
115 
122  void generateTuningMap(const std::vector<std::string> & params_to_tune,
123  const std::vector<Real> & min = std::vector<Real>(),
124  const std::vector<Real> & max = std::vector<Real>());
125 
131  void standardizeParameters(RealEigenMatrix & parameters, bool keep_moments = false);
132 
138  void standardizeData(RealEigenMatrix & data, bool keep_moments = false);
139 
140  // Tune hyperparameters using Adam
141  void tuneHyperParamsAdam(const RealEigenMatrix & training_params,
142  const RealEigenMatrix & training_data,
143  const GPOptimizerOptions & opts);
144 
145  // Computes the loss function
146  Real getLoss(RealEigenMatrix & inputs, RealEigenMatrix & outputs);
147 
148  // Computes Gradient of the loss function
149  std::vector<Real> getGradient(RealEigenMatrix & inputs) const;
150 
153  void mapToVec(
154  const std::unordered_map<std::string, std::tuple<unsigned int, unsigned int, Real, Real>> &
155  tuning_data,
156  const std::unordered_map<std::string, Real> & scalar_map,
157  const std::unordered_map<std::string, std::vector<Real>> & vector_map,
158  std::vector<Real> & vec) const;
159 
161  void vecToMap(
162  const std::unordered_map<std::string, std::tuple<unsigned int, unsigned int, Real, Real>> &
163  tuning_data,
164  std::unordered_map<std::string, Real> & scalar_map,
165  std::unordered_map<std::string, std::vector<Real>> & vector_map,
166  const std::vector<Real> & vec) const;
167 
169 
174  const RealEigenMatrix & getK() const { return _K; }
176  const Eigen::LLT<RealEigenMatrix> & getKCholeskyDecomp() const { return _K_cho_decomp; }
179  const std::string & getCovarType() const { return _covar_type; }
180  const std::string & getCovarName() const { return _covar_name; }
181  const std::vector<UserObjectName> & getDependentCovarNames() const
182  {
183  return _dependent_covar_names;
184  }
185  const std::map<UserObjectName, std::string> & getDependentCovarTypes() const
186  {
187  return _dependent_covar_types;
188  }
189  const unsigned int & getCovarNumOutputs() const { return _num_outputs; }
190  const unsigned int & getNumTunableParams() const { return _num_tunable; }
191  const std::unordered_map<std::string, Real> & getHyperParamMap() const { return _hyperparam_map; }
192  const std::unordered_map<std::string, std::vector<Real>> & getHyperParamVectorMap() const
193  {
194  return _hyperparam_vec_map;
195  }
197 
199 
205  RealEigenMatrix & K() { return _K; }
207  Eigen::LLT<RealEigenMatrix> & KCholeskyDecomp() { return _K_cho_decomp; }
210  std::string & covarType() { return _covar_type; }
211  std::string & covarName() { return _covar_name; }
212  std::map<UserObjectName, std::string> & dependentCovarTypes() { return _dependent_covar_types; }
213  std::vector<UserObjectName> & dependentCovarNames() { return _dependent_covar_names; }
214  unsigned int & covarNumOutputs() { return _num_outputs; }
215  std::unordered_map<std::string, std::tuple<unsigned int, unsigned int, Real, Real>> & tuningData()
216  {
217  return _tuning_data;
218  }
219  std::unordered_map<std::string, Real> & hyperparamMap() { return _hyperparam_map; }
220  std::unordered_map<std::string, std::vector<Real>> & hyperparamVectorMap()
221  {
222  return _hyperparam_vec_map;
223  }
225 
226 protected:
229 
231  std::unordered_map<std::string, std::tuple<unsigned int, unsigned int, Real, Real>> _tuning_data;
232 
234  unsigned int _num_tunable;
235 
237  std::string _covar_type;
238 
240  std::string _covar_name;
241 
243  std::vector<UserObjectName> _dependent_covar_names;
244 
246  std::map<UserObjectName, std::string> _dependent_covar_types;
247 
249  unsigned int _num_outputs;
250 
252  std::unordered_map<std::string, Real> _hyperparam_map;
253 
255  std::unordered_map<std::string, std::vector<Real>> _hyperparam_vec_map;
256 
259 
262 
265 
268 
270  Eigen::LLT<RealEigenMatrix> _K_cho_decomp;
271 
274 
277 
279  unsigned int _batch_size;
280 };
281 
282 } // StochasticTools namespac
283 
284 template <>
285 void dataStore(std::ostream & stream, Eigen::LLT<RealEigenMatrix> & decomp, void * context);
286 template <>
287 void dataLoad(std::istream & stream, Eigen::LLT<RealEigenMatrix> & decomp, void * context);
288 
289 template <>
290 void dataStore(std::ostream & stream, StochasticTools::GaussianProcess & gp_utils, void * context);
291 template <>
292 void dataLoad(std::istream & stream, StochasticTools::GaussianProcess & gp_utils, void * context);
unsigned int _num_tunable
Number of tunable hyperparameters.
void linkCovarianceFunction(CovarianceFunctionBase *covariance_function)
Finds and links the covariance function to this object.
RealEigenMatrix _K_results_solve
A solve of Ax=b via Cholesky.
const std::vector< UserObjectName > & getDependentCovarNames() const
void setupCovarianceMatrix(const RealEigenMatrix &training_params, const RealEigenMatrix &training_data, const GPOptimizerOptions &opts)
Sets up the covariance matrix given data and optimization options.
CovarianceFunctionBase * _covariance_function
Covariance function object.
const RealEigenMatrix & getKResultsSolve() const
std::unordered_map< std::string, std::vector< Real > > _hyperparam_vec_map
Vector hyperparameters. Stored for use in surrogate.
StochasticTools::Standardizer _data_standardizer
Standardizer for use with data (y)
const Real lambda
Tuning parameter from the paper.
const unsigned int show_every_nth_iteration
Switch to enable verbose output for parameter tuning at every n-th iteration.
std::map< UserObjectName, std::string > & dependentCovarTypes()
std::unordered_map< std::string, std::tuple< unsigned int, unsigned int, Real, Real > > & tuningData()
std::unordered_map< std::string, Real > _hyperparam_map
Scalar hyperparameters. Stored for use in surrogate.
void dataStore(std::ostream &stream, Eigen::LLT< RealEigenMatrix > &decomp, void *context)
const CovarianceFunctionBase & getCovarFunction() const
Real getLoss(RealEigenMatrix &inputs, RealEigenMatrix &outputs)
const Real eps
Tuning parameter from the paper.
void generateTuningMap(const std::vector< std::string > &params_to_tune, const std::vector< Real > &min=std::vector< Real >(), const std::vector< Real > &max=std::vector< Real >())
Sets up the tuning map which is used if the user requires parameter tuning.
Base class for covariance functions that are used in Gaussian Processes.
StochasticTools::Standardizer _param_standardizer
Standardizer for use with params (x)
std::string _covar_name
The name of the covariance function used in this GP.
std::map< UserObjectName, std::string > _dependent_covar_types
The types of the covariance functions the used covariance function depends on.
const unsigned int & getCovarNumOutputs() const
void standardizeData(RealEigenMatrix &data, bool keep_moments=false)
Standardizes the vector of responses (y values).
const unsigned int batch_size
The batch isize for Adam optimizer.
auto max(const L &left, const R &right)
const unsigned int & getNumTunableParams() const
Structure containing the optimization options for hyperparameter-tuning.
void tuneHyperParamsAdam(const RealEigenMatrix &training_params, const RealEigenMatrix &training_data, const GPOptimizerOptions &opts)
unsigned int _num_outputs
The number of outputs of the GP.
CovarianceFunctionBase & covarFunction()
Enum for batch type in stochastic tools MultiApp.
StochasticTools::Standardizer & dataStandardizer()
StochasticTools::Standardizer & paramStandardizer()
Get non-constant reference to the contained structures (if they need to be modified from the utside) ...
const std::string & getCovarName() const
const Real b2
Tuning parameter from the paper.
std::vector< UserObjectName > _dependent_covar_names
The names of the covariance functions the used covariance function depends on.
std::unordered_map< std::string, std::vector< Real > > & hyperparamVectorMap()
const CovarianceFunctionBase * getCovarFunctionPtr() const
RealEigenMatrix _K
An _n_sample by _n_sample covariance matrix constructed from the selected kernel function.
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealEigenMatrix
const Real b1
Tuning parameter from the paper.
void initialize(CovarianceFunctionBase *covariance_function, const std::vector< std::string > &params_to_tune, const std::vector< Real > &min=std::vector< Real >(), const std::vector< Real > &max=std::vector< Real >())
Initializes the most important structures in the Gaussian Process: the covariance function and a tuni...
void dataLoad(std::istream &stream, Eigen::LLT< RealEigenMatrix > &decomp, void *context)
const Eigen::LLT< RealEigenMatrix > & getKCholeskyDecomp() const
std::unordered_map< std::string, std::tuple< unsigned int, unsigned int, Real, Real > > _tuning_data
Contains tuning inforation. Index of hyperparam, size, and min/max bounds.
Eigen::LLT< RealEigenMatrix > _K_cho_decomp
Cholesky decomposition Eigen object.
const std::unordered_map< std::string, std::vector< Real > > & getHyperParamVectorMap() const
const StochasticTools::Standardizer & getParamStandardizer() const
Get constant reference to the contained structures.
const std::map< UserObjectName, std::string > & getDependentCovarTypes() const
unsigned int _batch_size
The batch size for Adam optimization.
std::vector< Real > getGradient(RealEigenMatrix &inputs) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const unsigned int num_iter
The number of iterations for Adam optimizer.
Class for standardizing data (centering and scaling)
Definition: Standardizer.h:21
Eigen::LLT< RealEigenMatrix > & KCholeskyDecomp()
const RealEigenMatrix & getK() const
std::string _covar_type
Type of covariance function used for this GP.
const StochasticTools::Standardizer & getDataStandardizer() const
std::unordered_map< std::string, Real > & hyperparamMap()
const std::string & getCovarType() const
void vecToMap(const std::unordered_map< std::string, std::tuple< unsigned int, unsigned int, Real, Real >> &tuning_data, std::unordered_map< std::string, Real > &scalar_map, std::unordered_map< std::string, std::vector< Real >> &vector_map, const std::vector< Real > &vec) const
Function used to convert the vectors back to hyperparameter maps.
Utility class dedicated to hold structures and functions commont to Gaussian Processes.
std::vector< UserObjectName > & dependentCovarNames()
CovarianceFunctionBase * covarFunctionPtr()
const RealEigenMatrix * _training_data
Data (y) used for training.
void setupStoredMatrices(const RealEigenMatrix &input)
Sets up the Cholesky decomposition and inverse action of the covariance matrix.
auto min(const L &left, const R &right)
void mapToVec(const std::unordered_map< std::string, std::tuple< unsigned int, unsigned int, Real, Real >> &tuning_data, const std::unordered_map< std::string, Real > &scalar_map, const std::unordered_map< std::string, std::vector< Real >> &vector_map, std::vector< Real > &vec) const
Function used to convert the hyperparameter maps in this object to vectors.
void standardizeParameters(RealEigenMatrix &parameters, bool keep_moments=false)
Standardizes the vector of input parameters (x values).
const Real learning_rate
The learning rate for Adam optimizer.
const RealEigenMatrix * _training_params
Paramaters (x) used for training, along with statistics.
const std::unordered_map< std::string, Real > & getHyperParamMap() const