https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ActiveLearningGPDecision.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
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
12#include "Sampler.h"
14
15#include <math.h>
16
18
21{
24 "Evaluates a GP surrogate model, determines its prediction quality, "
25 "launches full model if GP prediction is inadequate, and retrains GP.");
26 MooseEnum learning_function("Ufunction COV");
28 "learning_function", learning_function, "The learning function for active learning.");
29 params.addRequiredParam<Real>("learning_function_threshold", "The learning function threshold.");
30 params.addParam<Real>("learning_function_parameter",
31 std::numeric_limits<Real>::max(),
32 "The learning function parameter.");
33 params.addRequiredParam<UserObjectName>("al_gp", "Active learning GP trainer.");
34 params.addRequiredParam<UserObjectName>("gp_evaluator", "Evaluate the trained GP.");
35 params.addRequiredParam<SamplerName>("sampler", "The sampler object.");
36 params.addParam<ReporterValueName>("flag_sample", "flag_sample", "Flag samples.");
37 params.addRequiredParam<int>("n_train", "Number of training steps.");
38 params.addParam<ReporterValueName>("inputs", "inputs", "The inputs.");
39 params.addParam<ReporterValueName>("gp_mean", "gp_mean", "The GP mean prediction.");
40 params.addParam<ReporterValueName>("gp_std", "gp_std", "The GP standard deviation.");
41 return params;
42}
43
45 : ActiveLearningReporterTempl<Real>(parameters),
47 _learning_function(getParam<MooseEnum>("learning_function")),
48 _learning_function_threshold(getParam<Real>("learning_function_threshold")),
49 _learning_function_parameter(getParam<Real>("learning_function_parameter")),
50 _inputs_batch(declareRecoverableData<std::vector<std::vector<Real>>>("inputs_batch")),
51 _outputs_batch(declareRecoverableData<std::vector<Real>>("outputs_batch")),
52 _al_gp(getUserObject<ActiveLearningGaussianProcess>("al_gp")),
53 _gp_eval(getSurrogateModel<GaussianProcessSurrogate>("gp_evaluator")),
54 _flag_sample(declareValue<std::vector<bool>>(
55 "flag_sample", std::vector<bool>(sampler().getNumberOfRows(), false))),
56 _n_train(getParam<int>("n_train")),
57 _inputs(declareValue<std::vector<std::vector<Real>>>(
58 "inputs",
59 std::vector<std::vector<Real>>(sampler().getNumberOfRows(),
60 std::vector<Real>(sampler().getNumberOfCols())))),
61 _gp_mean(
62 declareValue<std::vector<Real>>("gp_mean", std::vector<Real>(sampler().getNumberOfRows()))),
63 _gp_std(
64 declareValue<std::vector<Real>>("gp_std", std::vector<Real>(sampler().getNumberOfRows()))),
65 _decision(declareRecoverableData<bool>("decision", true)),
66 _inputs_global(getGlobalInputData()),
67 _outputs_global(getGlobalOutputData())
68{
69 if (_learning_function == "Ufunction" &&
70 !parameters.isParamSetByUser("learning_function_parameter"))
71 paramError("learning_function",
72 "The Ufunction requires the model failure threshold ('learning_function_parameter') "
73 "to be specified.");
74}
75
76bool
77ActiveLearningGPDecision::learningFunction(const Real & gp_mean, const Real & gp_std) const
78{
79 if (_learning_function == "Ufunction")
80 return (std::abs(gp_mean - _learning_function_parameter) / gp_std) >
82 else if (_learning_function == "COV")
83 return (gp_std / std::abs(gp_mean)) < _learning_function_threshold;
84 else
85 mooseError("Invalid learning function ", std::string(_learning_function));
86 return false;
87}
88
89void
90ActiveLearningGPDecision::setupData(const std::vector<std::vector<Real>> & inputs,
91 const std::vector<Real> & outputs)
92{
93 _inputs_batch.insert(_inputs_batch.end(), inputs.begin(), inputs.end());
94 _outputs_batch.insert(_outputs_batch.end(), outputs.begin(), outputs.end());
95}
96
97bool
99{
100 for (dof_id_type i = 0; i < _inputs.size(); ++i)
101 {
104 }
105
106 for (const auto & fs : _flag_sample)
107 if (!fs)
108 return false;
109 return true;
110}
111
112void
114{
115 // Accumulate inputs and outputs if we previously decided we needed a sample
116 if (_t_step > 1 && _decision)
117 {
118 // Accumulate data into _batch members
120
121 // Retrain if we are outside the training phase
122 if (_t_step > _n_train)
124 }
125
126 // Gather inputs for the current step
128
129 // Evaluate GP and decide if we need more data if outside training phase
130 if (_t_step > _n_train)
132}
133
134bool
135ActiveLearningGPDecision::needSample(const std::vector<Real> &,
136 dof_id_type,
137 dof_id_type global_ind,
138 Real & val)
139{
140 if (!_decision)
141 val = _gp_mean[global_ind];
142 return _decision;
143}
144
145#endif
registerMooseObject("StochasticToolsApp", ActiveLearningGPDecision)
void ErrorVector unsigned int
ActiveLearningGPDecision(const InputParameters &parameters)
bool & _decision
GP pass/fail decision.
const MooseEnum & _learning_function
The learning function for active learning.
std::vector< bool > & _flag_sample
Flag samples when the GP fails.
const int _n_train
Number of initial training points for GP.
const std::vector< std::vector< Real > > & _inputs_global
Reference to global input data requested from base class.
std::vector< Real > & _gp_std
Broadcast the GP standard deviation to JSON.
virtual bool facilitateDecision()
Make decisions whether to call the full model or not based on GP prediction and uncertainty.
virtual bool needSample(const std::vector< Real > &row, dof_id_type local_ind, dof_id_type global_ind, Real &val) override
Based on the computations in preNeedSample, the decision to get more data is passed and results from ...
std::vector< std::vector< Real > > & _inputs_batch
Store all the input vectors used for training.
const std::vector< Real > & _outputs_global
Reference to global output data requested from base class.
const SurrogateModel & _gp_eval
The GP evaluator object that permits re-evaluations.
virtual void setupData(const std::vector< std::vector< Real > > &inputs, const std::vector< Real > &outputs)
This sets up data for re-training the GP.
virtual void preNeedSample() override
This is where most of the computations happen:
static InputParameters validParams()
std::vector< std::vector< Real > > & _inputs
Storage for the input vectors to be transferred to the output file.
const Real & _learning_function_parameter
The learning function parameter.
const ActiveLearningGaussianProcess & _al_gp
The active learning GP trainer that permits re-training.
std::vector< Real > & _gp_mean
Broadcast the GP mean prediciton to JSON.
const Real & _learning_function_threshold
The learning function threshold.
std::vector< Real > & _outputs_batch
Store all the outputs used for training.
bool learningFunction(const Real &gp_mean, const Real &gp_std) const
This method evaluates the active learning acquisition function and returns bool that indicates whethe...
virtual void reTrain(const std::vector< std::vector< Real > > &inputs, const std::vector< Real > &outputs) const final
This is a base class for performing active learning routines, meant to be used in conjunction with Sa...
bool isParamSetByUser(const std::string &name) const
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
const InputParameters & parameters() const
void paramError(const std::string &param, Args... args) const
void mooseError(Args &&... args) const
Interface for objects that need to use samplers.
virtual Real evaluate(const std::vector< Real > &x) const
Evaluate surrogate model given a row of parameters.