Line data Source code
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 :
10 : #include "ActiveLearningGaussianProcess.h"
11 :
12 : #include <petsctao.h>
13 : #include <petscdmda.h>
14 :
15 : #include "libmesh/petsc_vector.h"
16 : #include "libmesh/petsc_matrix.h"
17 :
18 : #include <math.h>
19 :
20 : registerMooseObject("StochasticToolsApp", ActiveLearningGaussianProcess);
21 :
22 : InputParameters
23 200 : ActiveLearningGaussianProcess::validParams()
24 : {
25 200 : InputParameters params = SurrogateTrainerBase::validParams();
26 200 : params.addClassDescription(
27 : "Permit re-training Gaussian Process surrogate model for active learning.");
28 400 : params.addRequiredParam<UserObjectName>("covariance_function", "Name of covariance function.");
29 400 : params.addParam<bool>(
30 400 : "standardize_params", true, "Standardize (center and scale) training parameters (x values)");
31 400 : params.addParam<bool>(
32 400 : "standardize_data", true, "Standardize (center and scale) training data (y values)");
33 400 : params.addParam<unsigned int>("num_iters", 1000, "Tolerance value for Adam optimization");
34 400 : params.addParam<unsigned int>("batch_size", 0, "The batch size for Adam optimization");
35 400 : params.addParam<Real>("learning_rate", 0.001, "The learning rate for Adam optimization");
36 400 : params.addParam<unsigned int>(
37 : "show_every_nth_iteration",
38 400 : 0,
39 : "Switch to show Adam optimization loss values at every nth step. If 0, nothing is showed.");
40 400 : params.addParam<std::vector<std::string>>(
41 : "tune_parameters", {}, "Select hyperparameters to be tuned");
42 400 : params.addParam<std::vector<Real>>("tuning_min", {}, "Minimum allowable tuning value");
43 400 : params.addParam<std::vector<Real>>("tuning_max", {}, "Maximum allowable tuning value");
44 200 : return params;
45 0 : }
46 :
47 100 : ActiveLearningGaussianProcess::ActiveLearningGaussianProcess(const InputParameters & parameters)
48 : : SurrogateTrainerBase(parameters),
49 : CovarianceInterface(parameters),
50 : SurrogateModelInterface(this),
51 200 : _gp(declareModelData<StochasticTools::GaussianProcess>("_gp")),
52 200 : _training_params(declareModelData<RealEigenMatrix>("_training_params")),
53 200 : _training_data(declareModelData<RealEigenMatrix>("_training_data")),
54 200 : _standardize_params(getParam<bool>("standardize_params")),
55 200 : _standardize_data(getParam<bool>("standardize_data")),
56 500 : _optimization_opts(StochasticTools::GaussianProcess::GPOptimizerOptions(
57 300 : getParam<unsigned int>("show_every_nth_iteration"),
58 200 : getParam<unsigned int>("num_iters"),
59 200 : getParam<unsigned int>("batch_size"),
60 200 : getParam<Real>("learning_rate")))
61 : {
62 600 : _gp.initialize(getCovarianceFunctionByName(getParam<UserObjectName>("covariance_function")),
63 : getParam<std::vector<std::string>>("tune_parameters"),
64 : getParam<std::vector<Real>>("tuning_min"),
65 : getParam<std::vector<Real>>("tuning_max"));
66 100 : }
67 :
68 : void
69 239 : ActiveLearningGaussianProcess::reTrain(const std::vector<std::vector<Real>> & inputs,
70 : const std::vector<Real> & outputs) const
71 : {
72 : // Addtional error check for each re-train call of the GP surrogate
73 239 : if (inputs.size() != outputs.size())
74 0 : mooseError("Number of inputs (",
75 0 : inputs.size(),
76 : ") does not match number of outputs (",
77 0 : outputs.size(),
78 : ").");
79 239 : if (inputs.empty())
80 0 : mooseError("There is no data for retraining.");
81 :
82 239 : _training_params.setZero(outputs.size(), inputs[0].size());
83 239 : _training_data.setZero(outputs.size(), 1);
84 :
85 2774 : for (unsigned int i = 0; i < outputs.size(); ++i)
86 : {
87 2535 : _training_data(i, 0) = outputs[i];
88 8923 : for (unsigned int j = 0; j < inputs[i].size(); ++j)
89 6388 : _training_params(i, j) = inputs[i][j];
90 : }
91 :
92 : // Standardize (center and scale) training params
93 239 : if (_standardize_params)
94 239 : _gp.standardizeParameters(_training_params);
95 : // if not standardizing data set mean=0, std=1 for use in surrogate
96 : else
97 0 : _gp.paramStandardizer().set(0, 1, inputs[0].size());
98 :
99 : // Standardize (center and scale) training data
100 239 : if (_standardize_data)
101 239 : _gp.standardizeData(_training_data);
102 : // if not standardizing data set mean=0, std=1 for use in surrogate
103 : else
104 0 : _gp.dataStandardizer().set(0, 1, inputs[0].size());
105 :
106 : // Setup the covariance
107 239 : _gp.setupCovarianceMatrix(_training_params, _training_data, _optimization_opts);
108 239 : }
109 :
110 : const std::vector<Real> &
111 108 : ActiveLearningGaussianProcess::getLengthScales() const
112 : {
113 108 : return _gp.getLengthScales();
114 : }
115 :
116 : const StochasticTools::Standardizer &
117 0 : ActiveLearningGaussianProcess::getTrainingStandardizer() const
118 : {
119 0 : return _gp.getDataStandardizer();
120 : }
121 :
122 : void
123 0 : ActiveLearningGaussianProcess::getNormTrainingOuts(std::vector<Real> & norm_training_outs) const
124 : {
125 0 : for (unsigned int i = 0; i < norm_training_outs.size(); ++i)
126 0 : norm_training_outs[i] = _training_data(i, 0);
127 0 : }
|