9 #ifdef MOOSE_LIBTORCH_ENABLED 16 #include <petscdmda.h> 18 #include "libmesh/petsc_vector.h" 19 #include "libmesh/petsc_matrix.h" 29 params.
addClassDescription(
"Provides data preperation and training for a single- or multi-output " 30 "Gaussian Process surrogate model.");
32 params.
addRequiredParam<UserObjectName>(
"covariance_function",
"Name of covariance function.");
34 "standardize_params",
true,
"Standardize (center and scale) training parameters (x values)");
36 "standardize_data",
true,
"Standardize (center and scale) training data (y values)");
38 params.
addParam<
unsigned int>(
"num_iters", 1000,
"Tolerance value for Adam optimization");
39 params.
addParam<
unsigned int>(
"batch_size", 0,
"The batch size for Adam optimization");
40 params.
addParam<
Real>(
"learning_rate", 0.001,
"The learning rate for Adam optimization");
43 MooseEnum(
"adam=0 legacy_adam=1",
"adam"),
44 "The Adam optimizer semantics to use for Gaussian process hyperparameter tuning.");
46 "show_every_nth_iteration",
48 "Switch to show Adam optimization loss values at every nth step. If 0, nothing is showed.");
49 params.
addParam<std::vector<std::string>>(
"tune_parameters",
50 "Select hyperparameters to be tuned");
51 params.
addParam<std::vector<Real>>(
"tuning_min",
"Minimum allowable tuning value");
52 params.
addParam<std::vector<Real>>(
"tuning_max",
"Maximum allowable tuning value");
59 _predictor_row(getPredictorData()),
61 _training_params(declareModelData<torch::
Tensor>(
"_training_params")),
62 _standardize_params(getParam<bool>(
"standardize_params")),
63 _standardize_data(getParam<bool>(
"standardize_data")),
64 _do_tuning(isParamValid(
"tune_parameters")),
65 _optimization_opts(
StochasticTools::GaussianProcess::GPOptimizerOptions(
66 getParam<unsigned
int>(
"show_every_nth_iteration"),
67 getParam<unsigned
int>(
"num_iters"),
68 getParam<unsigned
int>(
"batch_size"),
69 getParam<
Real>(
"learning_rate"),
76 _sampler_row(getSamplerData())
81 paramError(
"batch_size",
"Batch size cannot be greater than the training data set size.");
83 std::vector<std::string> tune_parameters(
85 : std::vector<std::string>{});
88 (
getParam<std::vector<Real>>(
"tuning_min").size() != tune_parameters.size()))
89 mooseError(
"tuning_min size does not match tune_parameters");
91 (
getParam<std::vector<Real>>(
"tuning_max").size() != tune_parameters.size()))
92 mooseError(
"tuning_max size does not match tune_parameters");
94 std::vector<Real> lower_bounds, upper_bounds;
96 lower_bounds = getParam<std::vector<Real>>(
"tuning_min");
98 upper_bounds =
getParam<std::vector<Real>>(
"tuning_max");
123 mooseError(
"The size of the provided response (",
125 ") does not match the number of expected outputs from the covariance (",
const StochasticTools::GaussianProcess::GPOptimizerOptions _optimization_opts
Struct holding parameters necessary for parameter tuning.
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
virtual void train() override
const Real * _rval
Response value.
unsigned int _n_dims
Dimension of predictor data - either _sampler.getNumberOfCols() or _pvals.size() + _pcols...
void paramError(const std::string ¶m, Args... args) const
const T & getParam(const std::string &name) const
const std::vector< Real > * _rvecval
Vector response value.
const InputParameters & parameters() const
const Parallel::Communicator & _communicator
torch::Tensor _training_data
Data (y) used for training.
bool _do_tuning
Flag to toggle hyperparameter tuning/optimization.
unsigned int & _n_outputs
The number of outputs.
virtual void postTrain() override
GaussianProcessTrainer(const InputParameters ¶meters)
torch::Tensor & _training_params
Paramaters (x) used for training, along with statistics.
static InputParameters validParams()
torch::DeviceType getLibtorchDevice() const
registerMooseObject("StochasticToolsApp", GaussianProcessTrainer)
const std::vector< Real > & _predictor_row
Data from the current predictor row.
dof_id_type getNumberOfRows() const
virtual void preTrain() override
unsigned int getLocalSampleSize() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
This is the main trainer base class.
IntRange< T > make_range(T beg, T end)
void mooseError(Args &&... args) const
bool _standardize_data
Switch for training data(y) standardization.
std::vector< std::vector< Real > > _data_buffer
Data (y) used for training.
unsigned int numOutputs() const
Return the number of outputs assumed for this covariance function.
void moveToLibtorchDevice(torch::Tensor &tensor, const torch::DeviceType device_type)
bool isParamValid(const std::string &name) const
static InputParameters validParams()
StochasticTools::GaussianProcess & _gp
Gaussian process handler responsible for managing training related tasks.
std::vector< std::vector< Real > > _params_buffer
Parameters (x) used for training – we'll allgather these in postTrain().
void ErrorVector unsigned int
bool _standardize_params
Switch for training param (x) standardization.
CovarianceFunctionBase * getCovarianceFunctionByName(const UserObjectName &name) const
Lookup a CovarianceFunction object by name and return pointer.