https://mooseframework.inl.gov
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | List of all members
LibtorchANNSurrogate Class Reference

#include <LibtorchANNSurrogate.h>

Inheritance diagram for LibtorchANNSurrogate:
[legend]

Public Types

typedef DataFileName DataFileParameterType
 

Public Member Functions

 LibtorchANNSurrogate (const InputParameters &parameters)
 
virtual Real evaluate (const std::vector< Real > &x) const override
 Evaluate surrogate model given a row of parameters. More...
 
virtual Real evaluate (const std::vector< Real > &x) const
 Evaluate surrogate model given a row of parameters. More...
 
virtual void evaluate (const std::vector< Real > &x, std::vector< Real > &y) const
 Various evaluate methods that can be overriden. More...
 
virtual Real evaluate (const std::vector< Real > &x, Real &std) const
 Evaluate methods that also return predicted standard deviation (see GaussianProcess.h) More...
 
virtual void evaluate (const std::vector< Real > &x, std::vector< Real > &y, std::vector< Real > &) const
 
virtual bool enabled () const
 
std::shared_ptr< MooseObjectgetSharedPtr ()
 
std::shared_ptr< const MooseObjectgetSharedPtr () const
 
MooseAppgetMooseApp () const
 
const std::string & type () const
 
virtual const std::string & name () const
 
std::string typeAndName () const
 
std::string errorPrefix (const std::string &error_type) const
 
void callMooseError (std::string msg, const bool with_prefix) const
 
MooseObjectParameterName uniqueParameterName (const std::string &parameter_name) const
 
const InputParametersparameters () const
 
MooseObjectName uniqueName () const
 
const T & getParam (const std::string &name) const
 
std::vector< std::pair< T1, T2 > > getParam (const std::string &param1, const std::string &param2) const
 
const T * queryParam (const std::string &name) const
 
const T & getRenamedParam (const std::string &old_name, const std::string &new_name) const
 
getCheckedPointerParam (const std::string &name, const std::string &error_string="") const
 
bool isParamValid (const std::string &name) const
 
bool isParamSetByUser (const std::string &nm) const
 
void paramError (const std::string &param, Args... args) const
 
void paramWarning (const std::string &param, Args... args) const
 
void paramInfo (const std::string &param, Args... args) const
 
void connectControllableParams (const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
 
void mooseError (Args &&... args) const
 
void mooseErrorNonPrefixed (Args &&... args) const
 
void mooseDocumentedError (const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
 
void mooseWarning (Args &&... args) const
 
void mooseWarningNonPrefixed (Args &&... args) const
 
void mooseDeprecated (Args &&... args) const
 
void mooseInfo (Args &&... args) const
 
std::string getDataFileName (const std::string &param) const
 
std::string getDataFileNameByName (const std::string &relative_path) const
 
std::string getDataFilePath (const std::string &relative_path) const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 
T & getSampler (const std::string &name)
 
SamplergetSampler (const std::string &name)
 
T & getSamplerByName (const SamplerName &name)
 
SamplergetSamplerByName (const SamplerName &name)
 
template<>
SurrogateModelgetSurrogateModel (const std::string &name) const
 
template<>
SurrogateTrainerBasegetSurrogateTrainer (const std::string &name) const
 
template<>
SurrogateModelgetSurrogateModelByName (const UserObjectName &name) const
 
template<>
SurrogateTrainerBasegetSurrogateTrainerByName (const UserObjectName &name) const
 
const std::string & modelMetaDataName () const
 Accessor for the name of the model meta data. More...
 
const FileName & getModelDataFileName () const
 Get the associated filename. More...
 
bool hasModelData () const
 Check if we need to load model data (if the filename parameter is used) More...
 
virtual void evaluate (const std::vector< Real > &x, std::vector< Real > &y) const
 Various evaluate methods that can be overriden. More...
 
virtual Real evaluate (const std::vector< Real > &x, Real &std) const
 Evaluate methods that also return predicted standard deviation (see GaussianProcess.h) More...
 
virtual void evaluate (const std::vector< Real > &x, std::vector< Real > &y, std::vector< Real > &) const
 
template<typename T = SurrogateModel>
T & getSurrogateModel (const std::string &name) const
 Get a SurrogateModel/Trainer with a given name. More...
 
template<typename T = SurrogateTrainerBase>
T & getSurrogateTrainer (const std::string &name) const
 
template<typename T = SurrogateModel>
T & getSurrogateModelByName (const UserObjectName &name) const
 Get a sampler with a given name. More...
 
template<typename T = SurrogateTrainerBase>
T & getSurrogateTrainerByName (const UserObjectName &name) const
 
template<typename T , typename... Args>
T & declareModelData (const std::string &data_name, Args &&... args)
 Declare model data for loading from file as well as restart. More...
 
template<typename T , typename... Args>
const T & getModelData (const std::string &data_name, Args &&... args) const
 Retrieve model data from the interface. More...
 

Static Public Member Functions

static InputParameters validParams ()
 
static MooseEnum defaultPredictorTypes ()
 
static MooseEnum defaultResponseTypes ()
 

Public Attributes

const ConsoleStream _console
 

Protected Attributes

const std::shared_ptr< Moose::LibtorchArtificialNeuralNet > & _nn
 Pointer to the neural net object (initialized as null) More...
 
const StochasticTools::Standardizer_input_standardizer
 Standardizer for use with input (x) More...
 
const StochasticTools::Standardizer_output_standardizer
 Standardizer for use with output response (y) More...
 
const bool & _enabled
 
MooseApp_app
 
const std::string _type
 
const std::string _name
 
const InputParameters_pars
 
Factory_factory
 
ActionFactory_action_factory
 
const Parallel::Communicator & _communicator
 

Detailed Description

Definition at line 19 of file LibtorchANNSurrogate.h.

Constructor & Destructor Documentation

◆ LibtorchANNSurrogate()

LibtorchANNSurrogate::LibtorchANNSurrogate ( const InputParameters parameters)

Definition at line 24 of file LibtorchANNSurrogate.C.

26  _nn(getModelData<std::shared_ptr<Moose::LibtorchArtificialNeuralNet>>("nn")),
27  _input_standardizer(getModelData<StochasticTools::Standardizer>("input_standardizer")),
28  _output_standardizer(getModelData<StochasticTools::Standardizer>("output_standardizer"))
29 {
30  // We check if MOOSE is compiled with torch, if not this throws an error
32 }
const StochasticTools::Standardizer & _output_standardizer
Standardizer for use with output response (y)
const StochasticTools::Standardizer & _input_standardizer
Standardizer for use with input (x)
static void requiresTorch(const MooseObject &obj)
const T & getModelData(const std::string &data_name, Args &&... args) const
Retrieve model data from the interface.
const InputParameters & parameters() const
SurrogateModel(const InputParameters &parameters)
const std::shared_ptr< Moose::LibtorchArtificialNeuralNet > & _nn
Pointer to the neural net object (initialized as null)

Member Function Documentation

◆ declareModelData()

template<typename T , typename... Args>
T & RestartableModelInterface::declareModelData ( const std::string &  data_name,
Args &&...  args 
)
inherited

Declare model data for loading from file as well as restart.

Definition at line 78 of file RestartableModelInterface.h.

79 {
80  return _model_restartable.declareRestartableData<T>(data_name, std::forward<Args>(args)...);
81 }
T & declareRestartableData(const std::string &data_name, Args &&... args)
Declare a piece of data as "restartable" and initialize it.
PublicRestartable _model_restartable
Member for interfacing with the framework&#39;s restartable system.

◆ defaultPredictorTypes()

static MooseEnum SurrogateModel::defaultPredictorTypes ( )
inlinestaticinherited

Definition at line 27 of file SurrogateModel.h.

27 { return MooseEnum("real"); }

◆ defaultResponseTypes()

static MooseEnum SurrogateModel::defaultResponseTypes ( )
inlinestaticinherited

Definition at line 28 of file SurrogateModel.h.

Referenced by EvaluateSurrogate::validParams().

28 { return MooseEnum("real vector_real"); }

◆ evaluate() [1/8]

virtual void SurrogateModel::evaluate
inline

Various evaluate methods that can be overriden.

Definition at line 43 of file SurrogateModel.h.

44  {
45  evaluateError(x, y);
46  }
void evaluateError(P x, R y, bool with_std=false) const
const std::vector< double > y
const std::vector< double > x

◆ evaluate() [2/8]

virtual Real SurrogateModel::evaluate
inline

Evaluate methods that also return predicted standard deviation (see GaussianProcess.h)

Definition at line 53 of file SurrogateModel.h.

54  {
55  evaluateError(x, std, true);
56  return 0.0;
57  }
void evaluateError(P x, R y, bool with_std=false) const
const std::vector< double > x

◆ evaluate() [3/8]

virtual Real SurrogateModel::evaluate
inline

Evaluate surrogate model given a row of parameters.

Definition at line 33 of file SurrogateModel.h.

34  {
35  evaluateError(x, Real());
36  return 0.0;
37  };
void evaluateError(P x, R y, bool with_std=false) const
const std::vector< double > x
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ evaluate() [4/8]

virtual void SurrogateModel::evaluate
inline

Definition at line 59 of file SurrogateModel.h.

60  {
61  evaluateError(x, y, true);
62  }
void evaluateError(P x, R y, bool with_std=false) const
const std::vector< double > y
const std::vector< double > x

◆ evaluate() [5/8]

Real LibtorchANNSurrogate::evaluate ( const std::vector< Real > &  x) const
overridevirtual

Evaluate surrogate model given a row of parameters.

Reimplemented from SurrogateModel.

Definition at line 35 of file LibtorchANNSurrogate.C.

36 {
37  Real val(0.0);
38 
39  // Check whether input point has same dimensionality as training data
40  mooseAssert(_nn->numInputs() == x.size(),
41  "Input point does not match dimensionality of training data.");
42 
43  std::vector<Real> converted_input(x.size(), 0);
44  const auto & input_mean = _input_standardizer.getMean();
45  const auto & input_std = _input_standardizer.getStdDev();
46 
47  mooseAssert(input_mean.size() == converted_input.size() &&
48  input_std.size() == converted_input.size(),
49  "The input standardizer's dimensions should be the same as the input dimension!");
50 
51  for (auto input_i : index_range(converted_input))
52  converted_input[input_i] = (x[input_i] - input_mean[input_i]) / input_std[input_i];
53 
54  torch::Tensor x_tf =
55  torch::tensor(torch::ArrayRef<Real>(converted_input.data(), converted_input.size()))
56  .to(at::kDouble);
57 
58  const auto & output_mean = _output_standardizer.getMean();
59  const auto & output_std = _output_standardizer.getStdDev();
60 
61  mooseAssert(output_mean.size() == 1 && output_std.size() == 1,
62  "The output standardizer's dimensions should be 1!");
63 
64  // Compute prediction
65  val = _nn->forward(x_tf).item<double>();
66  val = val * output_std[0] + output_mean[0];
67 
68  return val;
69 }
const StochasticTools::Standardizer & _output_standardizer
Standardizer for use with output response (y)
const StochasticTools::Standardizer & _input_standardizer
Standardizer for use with input (x)
const std::vector< double > x
const std::vector< Real > & getStdDev() const
Get the standard deviation vector.
Definition: Standardizer.h:39
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const std::vector< Real > & getMean() const
Get the mean vector.
Definition: Standardizer.h:37
const std::shared_ptr< Moose::LibtorchArtificialNeuralNet > & _nn
Pointer to the neural net object (initialized as null)
auto index_range(const T &sizable)

◆ evaluate() [6/8]

virtual void SurrogateModel::evaluate ( const std::vector< Real > &  x,
std::vector< Real > &  y 
) const
inlinevirtualinherited

Various evaluate methods that can be overriden.

Reimplemented in GaussianProcessSurrogate, PolynomialRegressionSurrogate, and NearestPointSurrogate.

Definition at line 43 of file SurrogateModel.h.

44  {
45  evaluateError(x, y);
46  }
void evaluateError(P x, R y, bool with_std=false) const
const std::vector< double > y
const std::vector< double > x

◆ evaluate() [7/8]

virtual Real SurrogateModel::evaluate ( const std::vector< Real > &  x,
Real std 
) const
inlinevirtualinherited

Evaluate methods that also return predicted standard deviation (see GaussianProcess.h)

Reimplemented in GaussianProcessSurrogate.

Definition at line 53 of file SurrogateModel.h.

54  {
55  evaluateError(x, std, true);
56  return 0.0;
57  }
void evaluateError(P x, R y, bool with_std=false) const
const std::vector< double > x

◆ evaluate() [8/8]

virtual void SurrogateModel::evaluate ( const std::vector< Real > &  x,
std::vector< Real > &  y,
std::vector< Real > &   
) const
inlinevirtualinherited

Reimplemented in GaussianProcessSurrogate.

Definition at line 59 of file SurrogateModel.h.

60  {
61  evaluateError(x, y, true);
62  }
void evaluateError(P x, R y, bool with_std=false) const
const std::vector< double > y
const std::vector< double > x

◆ getModelData()

template<typename T , typename... Args>
const T & RestartableModelInterface::getModelData ( const std::string &  data_name,
Args &&...  args 
) const
inherited

Retrieve model data from the interface.

Definition at line 85 of file RestartableModelInterface.h.

86 {
87  return _model_restartable.getRestartableData<T>(data_name, std::forward<Args>(args)...);
88 }
const T & getRestartableData(const std::string &data_name) const
Declare a piece of data as "restartable" and initialize it Similar to declareRestartableData but retu...
PublicRestartable _model_restartable
Member for interfacing with the framework&#39;s restartable system.

◆ getModelDataFileName()

const FileName & RestartableModelInterface::getModelDataFileName ( ) const
inherited

Get the associated filename.

Definition at line 33 of file RestartableModelInterface.C.

34 {
35  return _model_object.getParam<FileName>("filename");
36 }
const T & getParam(const std::string &name) const
const MooseObject & _model_object
Reference to the MooseObject that uses this interface.

◆ getSurrogateModel() [1/2]

template<>
SurrogateModel& SurrogateModelInterface::getSurrogateModel ( const std::string &  name) const
inherited

Definition at line 46 of file SurrogateModelInterface.C.

47 {
48  return getSurrogateModelByName<SurrogateModel>(_smi_params.get<UserObjectName>(name));
49 }
const InputParameters & _smi_params
Parameters of the object with this interface.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
const std::string name
Definition: Setup.h:20

◆ getSurrogateModel() [2/2]

template<typename T >
T & SurrogateModelInterface::getSurrogateModel ( const std::string &  name) const
inherited

Get a SurrogateModel/Trainer with a given name.

Parameters
nameThe name of the parameter key of the sampler to retrieve
Returns
The sampler with name associated with the parameter 'name'

Definition at line 81 of file SurrogateModelInterface.h.

Referenced by SurrogateTrainer::initialize().

82 {
83  return getSurrogateModelByName<T>(_smi_params.get<UserObjectName>(name));
84 }
const InputParameters & _smi_params
Parameters of the object with this interface.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
const std::string name
Definition: Setup.h:20

◆ getSurrogateModelByName() [1/2]

template<>
SurrogateModel& SurrogateModelInterface::getSurrogateModelByName ( const UserObjectName &  name) const
inherited

Definition at line 31 of file SurrogateModelInterface.C.

32 {
33  std::vector<SurrogateModel *> models;
35  .query()
36  .condition<AttribName>(name)
37  .condition<AttribSystem>("SurrogateModel")
38  .queryInto(models);
39  if (models.empty())
40  mooseError("Unable to find a SurrogateModel object with the name '" + name + "'");
41  return *(models[0]);
42 }
void mooseError(Args &&... args)
FEProblemBase & _smi_feproblem
Reference to FEProblemBase instance.
TheWarehouse & theWarehouse() const
const std::string name
Definition: Setup.h:20
Query query()

◆ getSurrogateModelByName() [2/2]

template<typename T >
T & SurrogateModelInterface::getSurrogateModelByName ( const UserObjectName &  name) const
inherited

Get a sampler with a given name.

Parameters
nameThe name of the sampler to retrieve
Returns
The sampler with name 'name'

Definition at line 88 of file SurrogateModelInterface.h.

Referenced by CrossValidationScores::CrossValidationScores(), EvaluateSurrogate::EvaluateSurrogate(), and InverseMapping::initialSetup().

89 {
90  std::vector<T *> models;
92  .query()
93  .condition<AttribName>(name)
94  .condition<AttribSystem>("SurrogateModel")
95  .queryInto(models);
96  if (models.empty())
97  mooseError("Unable to find a SurrogateModel object of type " + std::string(typeid(T).name()) +
98  " with the name '" + name + "'");
99  return *(models[0]);
100 }
void mooseError(Args &&... args)
FEProblemBase & _smi_feproblem
Reference to FEProblemBase instance.
TheWarehouse & theWarehouse() const
const std::string name
Definition: Setup.h:20
Query query()

◆ getSurrogateTrainer() [1/2]

template<typename T >
T & SurrogateModelInterface::getSurrogateTrainer ( const std::string &  name) const
inherited

Definition at line 104 of file SurrogateModelInterface.h.

105 {
106  return getSurrogateTrainerByName<T>(_smi_params.get<UserObjectName>(name));
107 }
const InputParameters & _smi_params
Parameters of the object with this interface.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
const std::string name
Definition: Setup.h:20

◆ getSurrogateTrainer() [2/2]

template<>
SurrogateTrainerBase& SurrogateModelInterface::getSurrogateTrainer ( const std::string &  name) const
inherited

Definition at line 60 of file SurrogateModelInterface.C.

61 {
62  return getSurrogateTrainerByName<SurrogateTrainerBase>(_smi_params.get<UserObjectName>(name));
63 }
const InputParameters & _smi_params
Parameters of the object with this interface.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
const std::string name
Definition: Setup.h:20

◆ getSurrogateTrainerByName() [1/2]

template<>
SurrogateTrainerBase& SurrogateModelInterface::getSurrogateTrainerByName ( const UserObjectName &  name) const
inherited

Definition at line 53 of file SurrogateModelInterface.C.

54 {
56 }
T & getUserObject(const std::string &name, unsigned int tid=0) const
FEProblemBase & _smi_feproblem
Reference to FEProblemBase instance.
const std::string name
Definition: Setup.h:20
This is the base trainer class whose main functionality is the API for declaring model data...

◆ getSurrogateTrainerByName() [2/2]

template<typename T >
T & SurrogateModelInterface::getSurrogateTrainerByName ( const UserObjectName &  name) const
inherited

Definition at line 111 of file SurrogateModelInterface.h.

Referenced by SurrogateTrainerOutput::output().

112 {
113  SurrogateTrainerBase * base_ptr =
115  T * obj_ptr = dynamic_cast<T *>(base_ptr);
116  if (!obj_ptr)
117  mooseError("Failed to find a SurrogateTrainer object of type " + std::string(typeid(T).name()) +
118  " with the name '",
119  name,
120  "' for the desired type.");
121  return *obj_ptr;
122 }
T & getUserObject(const std::string &name, unsigned int tid=0) const
void mooseError(Args &&... args)
FEProblemBase & _smi_feproblem
Reference to FEProblemBase instance.
const std::string name
Definition: Setup.h:20
const THREAD_ID _smi_tid
Thread ID.
This is the base trainer class whose main functionality is the API for declaring model data...

◆ hasModelData()

bool RestartableModelInterface::hasModelData ( ) const
inherited

Check if we need to load model data (if the filename parameter is used)

Definition at line 39 of file RestartableModelInterface.C.

40 {
41  return _model_object.isParamValid("filename");
42 }
bool isParamValid(const std::string &name) const
const MooseObject & _model_object
Reference to the MooseObject that uses this interface.

◆ modelMetaDataName()

const std::string& RestartableModelInterface::modelMetaDataName ( ) const
inlineinherited

Accessor for the name of the model meta data.

Definition at line 47 of file RestartableModelInterface.h.

Referenced by SurrogateTrainerOutput::output(), and MappingOutput::output().

47 { return _model_meta_data_name; }
const std::string _model_meta_data_name
The model meta data name.

◆ validParams()

InputParameters LibtorchANNSurrogate::validParams ( )
static

Definition at line 17 of file LibtorchANNSurrogate.C.

18 {
20  params.addClassDescription("Surrogate that evaluates a feedforward artificial neural net. ");
21  return params;
22 }
static InputParameters validParams()
void addClassDescription(const std::string &doc_string)

Member Data Documentation

◆ _input_standardizer

const StochasticTools::Standardizer& LibtorchANNSurrogate::_input_standardizer
protected

Standardizer for use with input (x)

Definition at line 33 of file LibtorchANNSurrogate.h.

Referenced by evaluate().

◆ _nn

const std::shared_ptr<Moose::LibtorchArtificialNeuralNet>& LibtorchANNSurrogate::_nn
protected

Pointer to the neural net object (initialized as null)

Definition at line 30 of file LibtorchANNSurrogate.h.

Referenced by evaluate().

◆ _output_standardizer

const StochasticTools::Standardizer& LibtorchANNSurrogate::_output_standardizer
protected

Standardizer for use with output response (y)

Definition at line 36 of file LibtorchANNSurrogate.h.

Referenced by evaluate().


The documentation for this class was generated from the following files: