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 
10 #include "BayesianActiveLearner.h"
11 
12 registerMooseObject("StochasticToolsApp", BayesianActiveLearner);
13 
16 {
19  params.addClassDescription(
20  "A reporter to support parallel active learning for Bayesian UQ tasks.");
21  params.addRequiredParam<std::vector<UserObjectName>>("likelihoods", "Names of likelihoods.");
22  params.addParam<ReporterValueName>(
23  "noise", "noise", "Name of the model noise term to pass to Likelihoods object.");
24  return params;
25 }
26 
29  LikelihoodInterface(parameters),
30  _new_var_samples(_al_sampler.getVarSamples()),
31  _var_prior(_al_sampler.getVarPrior()),
32  _var_test(_al_sampler.getVarSampleTries()),
33  _noise(declareValue<Real>("noise"))
34 {
35  // Filling the `likelihoods` vector with the user-provided distributions.
36  for (const UserObjectName & name : getParam<std::vector<UserObjectName>>("likelihoods"))
38 
41 
42  // Resize the length scales depending upon whether variance is included
44  _n_dim_plus_var = _n_dim + 1;
45  if (_var_prior)
47  else
48  _length_scales.resize(_n_dim);
49 
50  // Resize the log-likelihood vector to the number of parallel proposals
51  _log_likelihood.resize(_props);
52 }
53 
54 void
55 BayesianActiveLearner::setupGPData(const std::vector<Real> & data_out,
56  const DenseMatrix<Real> & data_in)
57 {
58  std::vector<Real> tmp;
59  computeLogLikelihood(data_out);
60  if (_var_prior)
61  tmp.resize(_n_dim_plus_var);
62  else
63  tmp.resize(_n_dim);
64  for (unsigned int i = 0; i < _props; ++i)
65  {
66  for (unsigned int j = 0; j < _n_dim; ++j)
67  tmp[j] = data_in(i, j);
68  if (_var_prior)
69  tmp[_n_dim] = _new_var_samples[i];
70  if (!std::isnan(_log_likelihood[i]))
71  {
72  _gp_inputs.push_back(tmp);
73  _gp_outputs.push_back(_log_likelihood[i]);
74  }
75  }
76 }
77 
78 void
79 BayesianActiveLearner::computeLogLikelihood(const std::vector<Real> & data_out)
80 {
81  _log_likelihood.assign(_props, 0.0);
82  std::vector<Real> out1(_num_confg_values);
83  for (unsigned int i = 0; i < _props; ++i)
84  {
85  for (unsigned int j = 0; j < _num_confg_values; ++j)
86  out1[j] = data_out[j * _props + i];
87  if (_var_prior)
88  {
89  _noise = std::sqrt(_new_var_samples[i]);
90  _log_likelihood[i] += _likelihoods[0]->function(out1);
91  }
92  else
93  _log_likelihood[i] += _likelihoods[0]->function(out1);
94  }
95 }
96 
97 Real
99 {
100  Real convergence_value = 0.0;
101  unsigned int num_valid = 0;
102  for (unsigned int ii = 0; ii < _props; ++ii)
103  {
104  if (!std::isnan(_log_likelihood[ii]))
105  {
106  convergence_value += Utility::pow<2>(_log_likelihood[ii] - _eval_outputs_current[ii]);
107  ++num_valid;
108  }
109  }
110  convergence_value = std::sqrt(convergence_value) / num_valid;
111  return convergence_value;
112 }
113 
114 void
116 {
117  std::vector<Real> tmp;
118  if (_var_prior)
119  tmp.resize(_n_dim_plus_var);
120  else
121  tmp.resize(_n_dim);
122  for (unsigned int i = 0; i < _gp_outputs_test.size(); ++i)
123  {
124  std::copy(_inputs_test[i].begin(), _inputs_test[i].end(), tmp.begin());
125  if (_var_prior)
126  tmp[_n_dim] = _var_test[i];
128  }
129 }
130 
131 void
133 {
135  if (_var_prior)
136  for (unsigned int i = 0; i < _inputs_test.size(); ++i)
137  _inputs_test_modified[i].push_back(_var_test[i]);
138 }
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.