https://mooseframework.inl.gov
BayesianActiveLearner.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 #ifdef MOOSE_LIBTORCH_ENABLED
10 
11 #include "BayesianActiveLearner.h"
12 
13 registerMooseObject("StochasticToolsApp", BayesianActiveLearner);
14 
17 {
20  params.addClassDescription(
21  "A reporter to support parallel active learning for Bayesian UQ tasks.");
22  params.addRequiredParam<std::vector<UserObjectName>>("likelihoods", "Names of likelihoods.");
23  params.addParam<ReporterValueName>(
24  "noise", "noise", "Name of the model noise term to pass to Likelihoods object.");
25  return params;
26 }
27 
30  LikelihoodInterface(parameters),
31  _new_var_samples(_al_sampler.getVarSamples()),
32  _var_prior(_al_sampler.getVarPrior()),
33  _var_test(_al_sampler.getVarSampleTries()),
34  _noise(declareValue<Real>("noise"))
35 {
36  // Filling the `likelihoods` vector with the user-provided distributions.
37  for (const UserObjectName & name : getParam<std::vector<UserObjectName>>("likelihoods"))
39 
42 
43  // Resize the length scales depending upon whether variance is included
45  _n_dim_plus_var = _n_dim + 1;
46  if (_var_prior)
48  else
49  _length_scales.resize(_n_dim);
50 
51  // Resize the log-likelihood vector to the number of parallel proposals
52  _log_likelihood.resize(_props);
53 }
54 
55 void
56 BayesianActiveLearner::setupGPData(const std::vector<Real> & data_out,
57  const DenseMatrix<Real> & data_in)
58 {
59  std::vector<Real> tmp;
60  computeLogLikelihood(data_out);
61  if (_var_prior)
62  tmp.resize(_n_dim_plus_var);
63  else
64  tmp.resize(_n_dim);
65  for (unsigned int i = 0; i < _props; ++i)
66  {
67  for (unsigned int j = 0; j < _n_dim; ++j)
68  tmp[j] = data_in(i, j);
69  if (_var_prior)
70  tmp[_n_dim] = _new_var_samples[i];
71  if (!std::isnan(_log_likelihood[i]))
72  {
73  _gp_inputs.push_back(tmp);
74  _gp_outputs.push_back(_log_likelihood[i]);
75  }
76  }
77 }
78 
79 void
80 BayesianActiveLearner::computeLogLikelihood(const std::vector<Real> & data_out)
81 {
82  _log_likelihood.assign(_props, 0.0);
83  std::vector<Real> out1(_num_confg_values);
84  for (unsigned int i = 0; i < _props; ++i)
85  {
86  for (unsigned int j = 0; j < _num_confg_values; ++j)
87  out1[j] = data_out[j * _props + i];
88  if (_var_prior)
89  {
90  _noise = std::sqrt(_new_var_samples[i]);
91  _log_likelihood[i] += _likelihoods[0]->function(out1);
92  }
93  else
94  _log_likelihood[i] += _likelihoods[0]->function(out1);
95  }
96 }
97 
98 Real
100 {
101  Real convergence_value = 0.0;
102  unsigned int num_valid = 0;
103  for (unsigned int ii = 0; ii < _props; ++ii)
104  {
105  if (!std::isnan(_log_likelihood[ii]))
106  {
107  convergence_value += Utility::pow<2>(_log_likelihood[ii] - _eval_outputs_current[ii]);
108  ++num_valid;
109  }
110  }
111  convergence_value = std::sqrt(convergence_value) / num_valid;
112  return convergence_value;
113 }
114 
115 void
117 {
118  std::vector<Real> tmp;
119  if (_var_prior)
120  tmp.resize(_n_dim_plus_var);
121  else
122  tmp.resize(_n_dim);
123  for (unsigned int i = 0; i < _gp_outputs_test.size(); ++i)
124  {
125  std::copy(_inputs_test[i].begin(), _inputs_test[i].end(), tmp.begin());
126  if (_var_prior)
127  tmp[_n_dim] = _var_test[i];
129  }
130 }
131 
132 void
134 {
136  if (_var_prior)
137  for (unsigned int i = 0; i < _inputs_test.size(); ++i)
138  _inputs_test_modified[i].push_back(_var_test[i]);
139 }
140 
141 #endif
virtual void setupGPData(const std::vector< Real > &data_out, const DenseMatrix< Real > &data_in) override
Sets up the training data for the GP model.
A generic reporter to support parallel active learning: re-trains GP and picks the next best batch...
std::vector< std::vector< Real > > _inputs_test_modified
Storage for all the modified proposed samples to test the GP model.
std::vector< Real > _log_likelihood
Storage for the computed log-likelihood values in each iteration of active learning.
Fast Bayesian inference with the parallel active learning (partly inspired from El Gammal et al...
dof_id_type getNumberOfConfigValues() const
Return the number of configuration parameters.
Definition: PMCMCBase.h:29
std::vector< std::vector< Real > > _gp_inputs
Storage for the GP re-training inputs.
virtual void evaluateGPTest() override
Evaluate the GP on all the test samples sent by the Sampler.
BayesianActiveLearner(const InputParameters &parameters)
dof_id_type getNumberOfConfigParams() const
Return the number of configuration parameters.
Definition: PMCMCBase.h:34
const T & getParam(const std::string &name) const
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
virtual Real computeConvergenceValue() override
Computes the convergence value during active learning.
std::vector< Real > _eval_outputs_current
The GP outputs from the current iteration before re-training (to evaluate convergence) ...
std::vector< const LikelihoodFunctionBase * > _likelihoods
Storage for the likelihood objects to be utilized.
registerMooseObject("StochasticToolsApp", BayesianActiveLearner)
const SurrogateModel & _gp_eval
The GP evaluator object that permits re-evaluations.
void computeLogLikelihood(const std::vector< Real > &data_out)
Sets up the training data for the GP model for Bayesian UQ tasks.
std::vector< Real > _length_scales
Storage for the length scales after the GP training.
static InputParameters validParams()
dof_id_type _props
Storage for the number of parallel proposals.
void addRequiredParam(const std::string &name, const std::string &doc_string)
unsigned int _n_dim
The input dimension for GP, equal to Sampler columns.
const Distribution * _var_prior
Storage for the prior over the variance.
Real & _noise
Model noise term to pass to Likelihoods object.
const std::string & name() const
A reporter to support parallel active learning for Bayesian UQ tasks.
const std::vector< std::vector< Real > > & _inputs_test
Storage for all the proposed samples to test the GP model.
const std::vector< Real > & _new_var_samples
Storage for new proposed variance samples.
unsigned int _n_dim_plus_var
The input dimension for GP for Bayesian problems with var, equal to Sampler columns + 1...
static InputParameters validParams()
dof_id_type _num_confg_params
Storage for the number of experimental configuration parameters.
virtual Real evaluate(const std::vector< Real > &x) const
Evaluate surrogate model given a row of parameters.
static InputParameters validParams()
virtual void includeAdditionalInputs() override
Include additional inputs before evaluating the acquisition function.
dof_id_type _num_confg_values
Storage for the number of experimental configuration values.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
LikelihoodFunctionBase * getLikelihoodFunctionByName(const UserObjectName &name) const
Lookup a LikelihoodFunction object by name and return pointer.
void addClassDescription(const std::string &doc_string)
std::vector< Real > _gp_outputs
Storage for the GP re-training outputs.
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
std::vector< Real > _gp_std_test
Outputs of GP model standard deviation for the test samples.
BayesianActiveLearningSampler & _al_sampler
The base sampler.
const std::vector< Real > & _var_test
Storage for all the proposed variance samples to test the GP model.
dof_id_type getNumberOfCols() const
std::vector< Real > _gp_outputs_test
Outputs of GP model for the test samples.