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