https://mooseframework.inl.gov
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
CovarianceFunctionBase Class Referenceabstract

Base class for covariance functions that are used in Gaussian Processes. More...

#include <CovarianceFunctionBase.h>

Inheritance diagram for CovarianceFunctionBase:
[legend]

Public Types

using HyperParameterMap = std::unordered_map< std::string, torch::Tensor >
 
typedef DataFileName DataFileParameterType
 

Public Member Functions

 CovarianceFunctionBase (const InputParameters &parameters)
 
virtual void computeCovarianceMatrix (torch::Tensor &K, const torch::Tensor &x, const torch::Tensor &xp, const bool is_self_covariance) const =0
 Generates the Covariance Matrix given two sets of points in the parameter space. More...
 
void loadHyperParamMap (const HyperParameterMap &map)
 Load some hyperparameters into the local map contained in this object. More...
 
void buildHyperParamMap (HyperParameterMap &map) const
 Populates the input maps with the owned hyperparameters. More...
 
virtual bool getTuningData (const std::string &name, unsigned int &size, Real &min, Real &max) const
 Get the default minimum and maximum and size of a hyperparameter. More...
 
void dependentCovarianceTypes (std::map< UserObjectName, std::string > &name_type_map) const
 Populate a map with the names and types of the dependent covariance functions. More...
 
const std::vector< UserObjectName > & dependentCovarianceNames () const
 Get the names of the dependent covariances. More...
 
virtual bool computedKdhyper (torch::Tensor &dKdhp, const torch::Tensor &x, const std::string &hyper_param_name, unsigned int ind) const
 Redirect dK/dhp for hyperparameter "hp". More...
 
virtual bool isTunable (const std::string &name) const
 Check if a given parameter is tunable. More...
 
unsigned int numOutputs () const
 Return the number of outputs assumed for this covariance function. More...
 
virtual bool enabled () const
 
std::shared_ptr< MooseObjectgetSharedPtr ()
 
std::shared_ptr< const MooseObjectgetSharedPtr () const
 
bool isKokkosObject () const
 
MooseAppgetMooseApp () const
 
const std::string & type () const
 
const std::string & name () const
 
std::string typeAndName () const
 
MooseObjectParameterName uniqueParameterName (const std::string &parameter_name) const
 
MooseObjectName uniqueName () const
 
const InputParametersparameters () const
 
const hit::Node * getHitNode () const
 
bool hasBase () const
 
const std::string & getBase () const
 
const TgetParam (const std::string &name) const
 
std::vector< std::pair< T1, T2 > > getParam (const std::string &param1, const std::string &param2) const
 
const TqueryParam (const std::string &name) const
 
const TgetRenamedParam (const std::string &old_name, const std::string &new_name) const
 
T getCheckedPointerParam (const std::string &name, const std::string &error_string="") const
 
bool isParamValid (const std::string &name) const
 
bool isParamSetByUser (const std::string &name) const
 
void connectControllableParams (const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
 
void paramError (const std::string &param, Args... args) const
 
void paramWarning (const std::string &param, Args... args) const
 
void paramWarning (const std::string &param, Args... args) const
 
void paramInfo (const std::string &param, Args... args) const
 
std::string messagePrefix (const bool hit_prefix=true) const
 
std::string errorPrefix (const std::string &) const
 
void mooseError (Args &&... args) const
 
void mooseDocumentedError (const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
 
void mooseErrorNonPrefixed (Args &&... args) const
 
void mooseWarning (Args &&... args) const
 
void mooseWarning (Args &&... args) const
 
void mooseWarningNonPrefixed (Args &&... args) const
 
void mooseWarningNonPrefixed (Args &&... args) const
 
void mooseDeprecated (Args &&... args) const
 
void mooseDeprecated (Args &&... args) const
 
void mooseDeprecatedNoTrace (Args &&... args) const
 
void mooseInfo (Args &&... args) const
 
void callMooseError (std::string msg, const bool with_prefix, const hit::Node *node=nullptr, const bool show_trace=true) const
 
std::string getDataFileName (const std::string &param) const
 
std::string getDataFileNameByName (const std::string &relative_path) const
 
std::string getDataFilePath (const std::string &relative_path) const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static bool isScalarHyperParameter (const torch::Tensor &tensor)
 Return true if a hyperparameter tensor stores one scalar value. More...
 
static bool isVectorHyperParameter (const torch::Tensor &tensor)
 Return true if a hyperparameter tensor stores a vector of values. More...
 
static InputParameters validParams ()
 
static void callMooseError (MooseApp *const app, const InputParameters &params, std::string msg, const bool with_prefix, const hit::Node *node, const bool show_trace=true)
 

Public Attributes

 usingCombinedWarningSolutionWarnings
 
const ConsoleStream _console
 

Static Public Attributes

static const std::string type_param
 
static const std::string name_param
 
static const std::string unique_name_param
 
static const std::string app_param
 
static const std::string moose_base_param
 
static const std::string kokkos_object_param
 

Protected Member Functions

torch::TensoraddRealHyperParameter (const std::string &name, const Real value, const bool is_tunable)
 Register a scalar hyperparameter to this covariance function. More...
 
torch::TensoraddVectorRealHyperParameter (const std::string &name, const std::vector< Real > &value, const bool is_tunable)
 Register a vector hyperparameter to this covariance function. More...
 
void flagInvalidSolutionInternal (const InvalidSolutionID invalid_solution_id) const
 
InvalidSolutionID registerInvalidSolutionInternal (const std::string &message, const bool warning) const
 
CovarianceFunctionBasegetCovarianceFunctionByName (const UserObjectName &name) const
 Lookup a CovarianceFunction object by name and return pointer. More...
 

Protected Attributes

HyperParameterMap _hyperparameters
 Map of hyperparameters stored as rank-0 or rank-1 tensors. More...
 
std::unordered_set< std::string > _tunable_hp
 list of tunable hyper-parameters More...
 
const unsigned int _num_outputs
 The number of outputs this covariance function is used to describe. More...
 
const std::vector< UserObjectName > _dependent_covariance_names
 The names of the dependent covariance functions. More...
 
std::vector< std::string > _dependent_covariance_types
 The types of the dependent covariance functions. More...
 
std::vector< CovarianceFunctionBase * > _covariance_functions
 Vector of pointers to the dependent covariance functions. More...
 
const bool & _enabled
 
MooseApp_app
 
Factory_factory
 
ActionFactory_action_factory
 
const std::string & _type
 
const std::string & _name
 
const InputParameters_pars
 
const Parallel::Communicator & _communicator
 

Detailed Description

Base class for covariance functions that are used in Gaussian Processes.

Definition at line 20 of file CovarianceFunctionBase.h.

Member Typedef Documentation

◆ HyperParameterMap

using CovarianceFunctionBase::HyperParameterMap = std::unordered_map<std::string, torch::Tensor>

Definition at line 23 of file CovarianceFunctionBase.h.

Constructor & Destructor Documentation

◆ CovarianceFunctionBase()

CovarianceFunctionBase::CovarianceFunctionBase ( const InputParameters parameters)

Definition at line 69 of file CovarianceFunctionBase.C.

72  _num_outputs(getParam<unsigned int>("num_outputs")),
73  _dependent_covariance_names(getParam<std::vector<UserObjectName>>("covariance_functions"))
74 
75 {
76  // Fetch the dependent covariance functions
77  for (const auto & name : _dependent_covariance_names)
78  {
80  _dependent_covariance_types.push_back(_covariance_functions.back()->type());
81  }
82 }
std::vector< std::string > _dependent_covariance_types
The types of the dependent covariance functions.
const T & getParam(const std::string &name) const
const InputParameters & parameters() const
MooseObject(const InputParameters &parameters)
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
const std::string & name() const
const unsigned int _num_outputs
The number of outputs this covariance function is used to describe.
const std::vector< UserObjectName > _dependent_covariance_names
The names of the dependent covariance functions.
CovarianceInterface(const InputParameters &parameters)
CovarianceFunctionBase * getCovarianceFunctionByName(const UserObjectName &name) const
Lookup a CovarianceFunction object by name and return pointer.

Member Function Documentation

◆ addRealHyperParameter()

torch::Tensor & CovarianceFunctionBase::addRealHyperParameter ( const std::string &  name,
const Real  value,
const bool  is_tunable 
)
protected

Register a scalar hyperparameter to this covariance function.

Parameters
nameThe name of the parameter
valueThe initial value of the parameter
is_tunableIf the parameter is tunable during optimization

Definition at line 95 of file CovarianceFunctionBase.C.

98 {
99  const auto prefixed_name = _name + ":" + name;
100  return insertHyperParameter(
101  _hyperparameters, _tunable_hp, prefixed_name, makeScalarHyperParameter(value), is_tunable);
102 }
HyperParameterMap _hyperparameters
Map of hyperparameters stored as rank-0 or rank-1 tensors.
std::unordered_set< std::string > _tunable_hp
list of tunable hyper-parameters
const std::string & _name
const std::string & name() const

◆ addVectorRealHyperParameter()

torch::Tensor & CovarianceFunctionBase::addVectorRealHyperParameter ( const std::string &  name,
const std::vector< Real > &  value,
const bool  is_tunable 
)
protected

Register a vector hyperparameter to this covariance function.

Parameters
nameThe name of the parameter
valueThe initial value of the parameter
is_tunableIf the parameter is tunable during optimization

Definition at line 105 of file CovarianceFunctionBase.C.

Referenced by LMC::LMC().

108 {
109  const auto prefixed_name = _name + ":" + name;
110  return insertHyperParameter(
111  _hyperparameters, _tunable_hp, prefixed_name, makeVectorHyperParameter(value), is_tunable);
112 }
HyperParameterMap _hyperparameters
Map of hyperparameters stored as rank-0 or rank-1 tensors.
std::unordered_set< std::string > _tunable_hp
list of tunable hyper-parameters
const std::string & _name
const std::string & name() const

◆ buildHyperParamMap()

void CovarianceFunctionBase::buildHyperParamMap ( HyperParameterMap map) const

Populates the input maps with the owned hyperparameters.

Parameters
mapMap of hyperparameters that should be populated

Definition at line 153 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::setupCovarianceMatrix().

154 {
155  // First, add the hyperparameters of the dependent covariance functions
156  for (const auto dependent_covar : _covariance_functions)
157  dependent_covar->buildHyperParamMap(map);
158 
159  // At the end we just append the hyperparameters this object owns
160  for (const auto & iter : _hyperparameters)
161  if (!isScalarHyperParameter(iter.second) && !isVectorHyperParameter(iter.second))
162  mooseError("Unsupported hyperparameter rank ", iter.second.dim(), " for ", iter.first, ".");
163  else
164  map[iter.first] = iter.second.clone();
165 }
HyperParameterMap _hyperparameters
Map of hyperparameters stored as rank-0 or rank-1 tensors.
static bool isVectorHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores a vector of values.
static bool isScalarHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores one scalar value.
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
void mooseError(Args &&... args) const

◆ computeCovarianceMatrix()

virtual void CovarianceFunctionBase::computeCovarianceMatrix ( torch::Tensor K,
const torch::Tensor x,
const torch::Tensor xp,
const bool  is_self_covariance 
) const
pure virtual

Generates the Covariance Matrix given two sets of points in the parameter space.

Parameters
KReference to a matrix which should be populated by the covariance entries
xReference to the first set of points
xpReference to the second set of points
is_self_covarianceSwitch to enable adding the noise variance to the diagonal of the covariance matrix

Implemented in LMC, ExponentialCovariance, MaternHalfIntCovariance, and SquaredExponentialCovariance.

Referenced by GaussianProcessSurrogate::evaluate(), StochasticTools::GaussianProcess::getLoss(), and StochasticTools::GaussianProcess::setupCovarianceMatrix().

◆ computedKdhyper()

bool CovarianceFunctionBase::computedKdhyper ( torch::Tensor dKdhp,
const torch::Tensor x,
const std::string &  hyper_param_name,
unsigned int  ind 
) const
virtual

Redirect dK/dhp for hyperparameter "hp".

Returns false is the parameter has not been found in this covariance object.

Parameters
dKdhpThe matrix which should be populated with the derivatives
xThe input vector for which the derivatives of the covariance matrix is computed
hyper_param_nameThe name of the hyperparameter
indThe index within the hyperparameter. 0 if it is a scalar parameter. If it is a vector parameter, it should be the index within the vector.

Reimplemented in ExponentialCovariance, MaternHalfIntCovariance, SquaredExponentialCovariance, and LMC.

Definition at line 85 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::getGradient().

89 {
90  mooseError("Hyperparameter tuning not set up for this covariance function. Please define "
91  "computedKdhyper() to compute gradient.");
92 }
void mooseError(Args &&... args) const

◆ dependentCovarianceNames()

const std::vector<UserObjectName>& CovarianceFunctionBase::dependentCovarianceNames ( ) const
inline

Get the names of the dependent covariances.

Definition at line 67 of file CovarianceFunctionBase.h.

Referenced by StochasticTools::GaussianProcess::linkCovarianceFunction().

68  {
70  }
const std::vector< UserObjectName > _dependent_covariance_names
The names of the dependent covariance functions.

◆ dependentCovarianceTypes()

void CovarianceFunctionBase::dependentCovarianceTypes ( std::map< UserObjectName, std::string > &  name_type_map) const

Populate a map with the names and types of the dependent covariance functions.

Parameters
name_type_mapReference to the map which should be populated

Definition at line 204 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::linkCovarianceFunction().

206 {
207  for (const auto dependent_covar : _covariance_functions)
208  {
209  dependent_covar->dependentCovarianceTypes(name_type_map);
210  name_type_map.insert(std::make_pair(dependent_covar->name(), dependent_covar->type()));
211  }
212 }
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.

◆ getCovarianceFunctionByName()

CovarianceFunctionBase * CovarianceInterface::getCovarianceFunctionByName ( const UserObjectName &  name) const
protectedinherited

Lookup a CovarianceFunction object by name and return pointer.

Definition at line 26 of file CovarianceInterface.C.

Referenced by ActiveLearningGaussianProcess::ActiveLearningGaussianProcess(), CovarianceFunctionBase(), GaussianProcessTrainer::GaussianProcessTrainer(), and GaussianProcessSurrogate::setupCovariance().

27 {
28  std::vector<CovarianceFunctionBase *> models;
30  .query()
31  .condition<AttribName>(name)
32  .condition<AttribSystem>("CovarianceFunction")
33  .queryInto(models);
34  if (models.empty())
35  mooseError("Unable to find a CovarianceFunction object with the name '" + name + "'");
36  return models[0];
37 }
void mooseError(Args &&... args)
TheWarehouse & theWarehouse() const
const std::string name
Definition: Setup.h:21
Query query()
FEProblemBase & _covar_feproblem
Reference to FEProblemBase instance.

◆ getTuningData()

bool CovarianceFunctionBase::getTuningData ( const std::string &  name,
unsigned int size,
Real min,
Real max 
) const
virtual

Get the default minimum and maximum and size of a hyperparameter.

Returns false is the parameter has not been found in this covariance object.

Parameters
nameThe name of the hyperparameter
sizeReference to an unsigned int that will contain the size of the hyperparameter (will be populated with 1 if it is scalar)
minReference to a number which will be populated by the maximum allowed value of the hyperparameter
maxReference to a number which will be populated by the minimum allowed value of the hyperparameter

Definition at line 168 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::generateTuningMap().

172 {
173  // First, check the dependent covariances
174  for (const auto dependent_covar : _covariance_functions)
175  if (dependent_covar->getTuningData(name, size, min, max))
176  return true;
177 
178  min = 1e-9;
179  max = 1e9;
180 
181  const auto tensor_value = _hyperparameters.find(name);
182  if (tensor_value == _hyperparameters.end())
183  {
184  size = 0;
185  return false;
186  }
187 
188  if (isScalarHyperParameter(tensor_value->second))
189  {
190  size = 1;
191  return true;
192  }
193 
194  if (isVectorHyperParameter(tensor_value->second))
195  {
196  size = tensor_value->second.numel();
197  return true;
198  }
199 
200  mooseError("Unsupported hyperparameter rank ", tensor_value->second.dim(), " for ", name, ".");
201 }
HyperParameterMap _hyperparameters
Map of hyperparameters stored as rank-0 or rank-1 tensors.
static bool isVectorHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores a vector of values.
static bool isScalarHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores one scalar value.
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
auto max(const L &left, const R &right)
const std::string & name() const
void mooseError(Args &&... args) const
auto min(const L &left, const R &right)

◆ isScalarHyperParameter()

bool CovarianceFunctionBase::isScalarHyperParameter ( const torch::Tensor tensor)
static

Return true if a hyperparameter tensor stores one scalar value.

Definition at line 44 of file CovarianceFunctionBase.C.

Referenced by buildHyperParamMap(), getTuningData(), GaussianProcessData::initialize(), and loadHyperParamMap().

45 {
46  return tensor.dim() == 0;
47 }

◆ isTunable()

bool CovarianceFunctionBase::isTunable ( const std::string &  name) const
virtual

Check if a given parameter is tunable.

Parameters
Thename of the hyperparameter

Definition at line 115 of file CovarianceFunctionBase.C.

Referenced by StochasticTools::GaussianProcess::generateTuningMap().

116 {
117  // First, we check if the dependent covariances have the parameter
118  for (const auto dependent_covar : _covariance_functions)
119  if (dependent_covar->isTunable(name))
120  return true;
121 
122  if (_tunable_hp.find(name) != _tunable_hp.end())
123  return true;
124  else if (_hyperparameters.find(name) != _hyperparameters.end())
125  mooseError("We found hyperparameter ", name, " but it was not declared tunable!");
126 
127  return false;
128 }
HyperParameterMap _hyperparameters
Map of hyperparameters stored as rank-0 or rank-1 tensors.
std::unordered_set< std::string > _tunable_hp
list of tunable hyper-parameters
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
const std::string & name() const
void mooseError(Args &&... args) const

◆ isVectorHyperParameter()

bool CovarianceFunctionBase::isVectorHyperParameter ( const torch::Tensor tensor)
static

Return true if a hyperparameter tensor stores a vector of values.

Definition at line 50 of file CovarianceFunctionBase.C.

Referenced by buildHyperParamMap(), getTuningData(), GaussianProcessData::initialize(), and loadHyperParamMap().

51 {
52  return tensor.dim() == 1;
53 }

◆ loadHyperParamMap()

void CovarianceFunctionBase::loadHyperParamMap ( const HyperParameterMap map)

Load some hyperparameters into the local map contained in this object.

Parameters
mapInput map of hyperparameters

Definition at line 131 of file CovarianceFunctionBase.C.

Referenced by LoadCovarianceDataAction::load(), StochasticTools::GaussianProcess::setupCovarianceMatrix(), and StochasticTools::GaussianProcess::tuneHyperParamsAdam().

132 {
133  // First, load the hyperparameters of the dependent covariance functions
134  for (const auto dependent_covar : _covariance_functions)
135  dependent_covar->loadHyperParamMap(map);
136 
137  // Then we load the hyperparameters of this object
138  for (auto & iter : _hyperparameters)
139  {
140  const auto map_iter = map.find(iter.first);
141  if (map_iter == map.end())
142  continue;
143 
144  if (!isScalarHyperParameter(map_iter->second) && !isVectorHyperParameter(map_iter->second))
145  mooseError(
146  "Unsupported hyperparameter rank ", map_iter->second.dim(), " for ", iter.first, ".");
147 
148  iter.second = map_iter->second.clone();
149  }
150 }
HyperParameterMap _hyperparameters
Map of hyperparameters stored as rank-0 or rank-1 tensors.
static bool isVectorHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores a vector of values.
static bool isScalarHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores one scalar value.
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
void mooseError(Args &&... args) const

◆ numOutputs()

unsigned int CovarianceFunctionBase::numOutputs ( ) const
inline

Return the number of outputs assumed for this covariance function.

Definition at line 90 of file CovarianceFunctionBase.h.

Referenced by GaussianProcessSurrogate::evaluate(), GaussianProcessTrainer::GaussianProcessTrainer(), and StochasticTools::GaussianProcess::linkCovarianceFunction().

90 { return _num_outputs; }
const unsigned int _num_outputs
The number of outputs this covariance function is used to describe.

◆ validParams()

InputParameters CovarianceFunctionBase::validParams ( )
static

Definition at line 56 of file CovarianceFunctionBase.C.

Referenced by ExponentialCovariance::validParams(), SquaredExponentialCovariance::validParams(), MaternHalfIntCovariance::validParams(), and LMC::validParams().

57 {
59  params.addParam<std::vector<UserObjectName>>(
60  "covariance_functions", {}, "Covariance functions that this covariance function depends on.");
61  params.addParam<unsigned int>(
62  "num_outputs", 1, "The number of outputs expected for this covariance function.");
63  params.addClassDescription("Base class for covariance functions");
64  params.registerBase("CovarianceFunctionBase");
65  params.registerSystemAttributeName("CovarianceFunction");
66  return params;
67 }
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void registerSystemAttributeName(const std::string &value)
void registerBase(const std::string &value)
void addClassDescription(const std::string &doc_string)
static InputParameters validParams()

Member Data Documentation

◆ _covariance_functions

std::vector<CovarianceFunctionBase *> CovarianceFunctionBase::_covariance_functions
protected

◆ _dependent_covariance_names

const std::vector<UserObjectName> CovarianceFunctionBase::_dependent_covariance_names
protected

The names of the dependent covariance functions.

Definition at line 118 of file CovarianceFunctionBase.h.

Referenced by CovarianceFunctionBase(), and dependentCovarianceNames().

◆ _dependent_covariance_types

std::vector<std::string> CovarianceFunctionBase::_dependent_covariance_types
protected

The types of the dependent covariance functions.

Definition at line 121 of file CovarianceFunctionBase.h.

Referenced by CovarianceFunctionBase().

◆ _hyperparameters

HyperParameterMap CovarianceFunctionBase::_hyperparameters
protected

Map of hyperparameters stored as rank-0 or rank-1 tensors.

Definition at line 109 of file CovarianceFunctionBase.h.

Referenced by addRealHyperParameter(), addVectorRealHyperParameter(), buildHyperParamMap(), getTuningData(), isTunable(), and loadHyperParamMap().

◆ _num_outputs

const unsigned int CovarianceFunctionBase::_num_outputs
protected

The number of outputs this covariance function is used to describe.

Definition at line 115 of file CovarianceFunctionBase.h.

Referenced by LMC::computeCovarianceMatrix(), LMC::computedKdhyper(), LMC::computeLambdaGradient(), LMC::LMC(), and numOutputs().

◆ _tunable_hp

std::unordered_set<std::string> CovarianceFunctionBase::_tunable_hp
protected

list of tunable hyper-parameters

Definition at line 112 of file CovarianceFunctionBase.h.

Referenced by addRealHyperParameter(), addVectorRealHyperParameter(), LMC::computedKdhyper(), and isTunable().


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