9#ifdef MOOSE_LIBTORCH_ENABLED
24template <
typename SamplerType>
33template <
typename SamplerType>
52 virtual void setupGPData(
const std::vector<Real> & data_out,
const DenseMatrix<Real> & data_in);
88 virtual void getAcquisition(std::vector<Real> & acq_new, std::vector<unsigned int> & indices);
160template <
typename SamplerType>
166 params.
addClassDescription(
"A generic reporter to support parallel active learning: re-trains GP "
167 "and picks the next best batch.");
169 "Value of the model output from the SubApp.");
173 "Modified value of the model output from this reporter class.");
175 params.
addRequiredParam<UserObjectName>(
"al_gp",
"Active learning GP trainer.");
176 params.
addRequiredParam<UserObjectName>(
"gp_evaluator",
"Evaluator for the trained GP.");
180 "The sorted sample indices in order of importance to evaluate the subApp.");
182 "acquisition_function",
183 "acquisition_function",
184 "The values of the acquistion function in the current iteration.");
186 "convergence_value",
"convergence_value",
"Value to measure convergence of active learning.");
188 "inputs",
"inputs",
"Modified value of the model inputs from this reporter class.");
189 params.
addRequiredParam<UserObjectName>(
"acquisition",
"Name of the acquisition function.");
191 "penalize_acquisition",
193 "Set true to prevent clustering of the best batch inputs when operating in parallel.");
197template <
typename SamplerType>
203 _al_sampler(getSampler<SamplerType>(
"sampler")),
204 _n_dim(_al_sampler.getNumberOfCols()),
205 _props(_al_sampler.getNumParallelProposals()),
206 _inputs_test(_al_sampler.getSampleTries()),
208 _output_comm(declareValue<
std::vector<Real>>(
"outputs_required")),
209 _sorted_indices(declareValue<
std::vector<unsigned
int>>(
"sorted_indices")),
212 _acquisition_obj(getParallelAcquisitionFunctionByName(getParam<UserObjectName>(
"acquisition"))),
213 _acquisition_value(declareValue<
std::vector<Real>>(
"acquisition_function")),
214 _convergence_value(declareValue<Real>(
"convergence_value")),
215 _inputs_required(declareValue<
std::vector<
std::vector<Real>>>(
"inputs")),
216 _penalize_acquisition(getParam<bool>(
"penalize_acquisition")),
217 _check_step(
std::numeric_limits<
int>::max())
230template <
typename SamplerType>
233 const DenseMatrix<Real> & data_in)
235 for (
unsigned int i = 0; i < data_out.size(); ++i)
237 for (
unsigned int j = 0; j < _n_dim; ++j)
238 _inputs_required[i][j] = data_in(i, j);
239 _gp_inputs.push_back(_inputs_required[i]);
240 _gp_outputs.push_back(data_out[i]);
244template <
typename SamplerType>
248 for (
unsigned int i = 0; i < eval_outputs.size(); ++i)
249 eval_outputs[i] = _gp_eval.evaluate(_gp_inputs[i]);
252template <
typename SamplerType>
256 _generic = _gp_outputs;
259template <
typename SamplerType>
263 _inputs_test_modified = _inputs_test;
266template <
typename SamplerType>
269 std::vector<unsigned int> & indices)
271 std::vector<Real> acq;
272 acq.resize(_inputs_test.size());
273 includeAdditionalInputs();
274 _acquisition_obj.computeAcquisition(
275 acq, _gp_outputs_test, _gp_std_test, _inputs_test_modified, _gp_inputs, _generic);
277 if (_penalize_acquisition)
278 _acquisition_obj.penalizeAcquisition(
279 acq_new, indices, acq, _length_scales, _inputs_test_modified);
282template <
typename SamplerType>
286 Real convergence_value = 0.0;
287 for (
unsigned int ii = 0; ii < _output_comm.size(); ++ii)
288 convergence_value += Utility::pow<2>(_output_comm[ii] - _eval_outputs_current[ii]);
289 convergence_value = std::sqrt(convergence_value) / _output_comm.size();
290 return convergence_value;
293template <
typename SamplerType>
297 for (
unsigned int i = 0; i < _gp_outputs_test.size(); ++i)
298 _gp_outputs_test[i] = _gp_eval.evaluate(_inputs_test[i], _gp_std_test[i]);
301template <
typename SamplerType>
305 if (_al_sampler.getNumberOfLocalRows() == 0 || _check_step == _t_step)
307 _check_step = _t_step;
311 DenseMatrix<Real> data_in(_al_sampler.getNumberOfRows(), _al_sampler.getNumberOfCols());
312 for (dof_id_type ss = _al_sampler.getLocalRowBegin(); ss < _al_sampler.getLocalRowEnd(); ++ss)
314 const auto data = _al_sampler.getNextLocalRow();
315 for (
unsigned int j = 0; j < _al_sampler.getNumberOfCols(); ++j)
316 data_in(ss, j) = data[j];
318 _communicator.sum(data_in.get_values());
319 _output_comm = _output_value;
320 _communicator.allgather(_output_comm);
325 setupGPData(_output_comm, data_in);
330 computeGPOutput(_eval_outputs_current);
331 _convergence_value = computeConvergenceValue();
335 _al_gp.reTrain(_gp_inputs, _gp_outputs);
336 _length_scales = _al_gp.getLengthScales();
346 std::vector<Real> acq_new;
347 std::vector<unsigned int> indices;
348 indices.resize(_inputs_test.size());
349 getAcquisition(acq_new, indices);
352 std::copy_n(indices.begin(), _props, _sorted_indices.begin());
353 std::copy_n(acq_new.begin(), _props, _acquisition_value.begin());
356 std::iota(_sorted_indices.begin(), _sorted_indices.end(), 0);
359 _check_step = _t_step;
GenericActiveLearnerTempl< GenericActiveLearningSampler > GenericActiveLearner
const ReporterMode REPORTER_MODE_DISTRIBUTED
void ErrorVector unsigned int
static InputParameters validParams()
A generic reporter to support parallel active learning: re-trains GP and picks the next best batch.
std::vector< Real > _eval_outputs_current
The GP outputs from the current iteration before re-training (to evaluate convergence)
static InputParameters validParams()
Real & _convergence_value
For monitoring convergence of active learning.
GenericActiveLearnerTempl(const InputParameters ¶meters)
virtual void getAcquisition(std::vector< Real > &acq_new, std::vector< unsigned int > &indices)
Output the acquisition function values and ordering of the indices.
virtual void initialize() override
std::vector< std::vector< Real > > _gp_inputs
Storage for the GP re-training inputs.
const bool & _penalize_acquisition
Penalize acquisition to prevent clustering when operating in parallel.
std::vector< std::vector< Real > > & _inputs_required
Transmit the required inputs to the json file.
const SurrogateModel & _gp_eval
The GP evaluator object that permits re-evaluations.
unsigned int _n_dim
The input dimension for GP, equal to Sampler columns.
std::vector< Real > _gp_outputs
Storage for the GP re-training outputs.
const ActiveLearningGaussianProcess & _al_gp
The active learning GP trainer that permits re-training.
virtual void computeGPOutput(std::vector< Real > &eval_outputs)
Computes the outputs of the trained GP model.
std::vector< unsigned int > & _sorted_indices
The selected sample indices to evaluate the subApp.
std::vector< Real > _length_scales
Storage for the length scales after the GP training.
virtual void setupGPData(const std::vector< Real > &data_out, const DenseMatrix< Real > &data_in)
Sets up the training data for the GP model.
const std::vector< std::vector< Real > > & _inputs_test
Storage for all the proposed samples to test the GP model.
virtual void evaluateGPTest()
Evaluate the GP on all the test samples sent by the Sampler.
virtual void execute() override
std::vector< std::vector< Real > > _inputs_test_modified
Storage for all the modified proposed samples to test the GP model.
virtual void setupGeneric()
Setup the generic variable for acquisition computation (depends on the objective: optimization,...
SamplerType & _al_sampler
The base sampler.
dof_id_type _props
Storage for the number of parallel proposals.
std::vector< Real > _gp_std_test
Outputs of GP model standard deviation for the test samples.
virtual void includeAdditionalInputs()
Include additional inputs before evaluating the acquisition function.
ParallelAcquisitionFunctionBase & _acquisition_obj
Storage for the parallel acquisition object to be utilized.
std::vector< Real > & _acquisition_value
The acquistion function values in the current iteration.
std::vector< Real > _gp_outputs_test
Outputs of GP model for the test samples.
std::vector< Real > & _output_comm
Modified value of model output by this reporter class.
virtual Real computeConvergenceValue()
Computes the convergence value during active learning.
const std::vector< Real > & _output_value
Model output value from SubApp.
std::vector< Real > _generic
A generic parameter to be passed to the acquisition function.
virtual void finalize() override
int _check_step
Ensure that the MCMC algorithm proceeds in a sequential fashion.
const InputParameters & parameters() const
All ParallelAcquisition functions should inherit from this class.
static InputParameters validParams()
Interface for objects that need to use samplers.