10#ifdef MOOSE_LIBTORCH_ENABLED
26 "num_batches", 1,
"1<=num_batches",
"Number of batches.");
28 "num_epochs", 1,
"0<num_epochs",
"Number of training epochs.");
33 "The relative loss where we stop the training of the neural net.");
34 params.
addParam<std::vector<unsigned int>>(
35 "num_neurons_per_layer", std::vector<unsigned int>(),
"Number of neurons per layer.");
36 params.
addParam<std::vector<std::string>>(
37 "activation_function",
38 std::vector<std::string>({
"relu"}),
39 "The type of activation functions to use. It is either one value "
40 "or one value per hidden layer.");
42 "nn_filename",
"net.pt",
"Filename used to output the neural net parameters.");
43 params.
addParam<
bool>(
"read_from_file",
45 "Switch to allow reading old trained neural nets for further training.");
46 params.
addParam<Real>(
"learning_rate", 0.001,
"Learning rate (relaxation).");
50 "0<=print_epoch_loss",
51 "Epoch training loss printing. 0 - no printing, 1 - every epoch, 10 - every 10th epoch.");
53 "seed", 11,
"Random number generator seed for stochastic optimizers.");
55 "max_processes", 1,
"The maximum number of parallel processes that the trainer will use.");
58 "standardize_input",
true,
"Standardize (center and scale) training inputs (x values)");
60 "standardize_output",
true,
"Standardize (center and scale) training outputs (y values)");
68 _predictor_row(getPredictorData()),
69 _num_neurons_per_layer(declareModelData<
std::vector<unsigned
int>>(
70 "num_neurons_per_layer", getParam<
std::vector<unsigned
int>>(
"num_neurons_per_layer"))),
71 _activation_function(declareModelData<
std::vector<
std::string>>(
72 "activation_function", getParam<
std::vector<
std::string>>(
"activation_function"))),
73 _nn_filename(getParam<
std::string>(
"nn_filename")),
74 _read_from_file(getParam<bool>(
"read_from_file")),
75 _nn(declareModelData<
std::shared_ptr<
Moose::LibtorchArtificialNeuralNet>>(
"nn")),
76 _standardize_input(getParam<bool>(
"standardize_input")),
77 _standardize_output(getParam<bool>(
"standardize_output")),
78 _input_standardizer(declareModelData<
StochasticTools::Standardizer>(
"input_standardizer")),
79 _output_standardizer(declareModelData<
StochasticTools::Standardizer>(
"output_standardizer"))
83 torch::manual_seed(getParam<unsigned int>(
"seed"));
122 unsigned int num_inputs =
_n_dims;
125 _nn = std::make_shared<Moose::LibtorchArtificialNeuralNet>(
132 _console <<
"Loaded requested .pt file." << std::endl;
134 catch (
const c10::Error & e)
136 mooseError(
"The requested pytorch file could not be loaded.\n", e.msg());
141 auto options = torch::TensorOptions().dtype(at::kDouble);
142 torch::Tensor data_tensor =
143 torch::from_blob(
_flattened_data.data(), {num_samples, num_inputs}, options).to(at::kDouble);
144 torch::Tensor response_tensor =
150 auto data_std_mean = torch::std_mean(data_tensor, 0);
151 auto & data_std = std::get<0>(data_std_mean);
152 auto & data_mean = std::get<1>(data_std_mean);
154 data_tensor = (data_tensor - data_mean) / data_std;
156 std::vector<Real> converted_data_mean;
158 std::vector<Real> converted_data_std;
167 auto response_std_mean = torch::std_mean(response_tensor, 0);
168 auto & response_std = std::get<0>(response_std_mean);
169 auto & response_mean = std::get<1>(response_std_mean);
171 response_tensor = (response_tensor - response_mean) / response_std;
173 std::vector<Real> converted_response_mean;
175 std::vector<Real> converted_response_std;
registerMooseObject("StochasticToolsApp", LibtorchANNTrainer)
void ErrorVector unsigned int
const ConsoleStream _console
Trainer responsible of fitting a neural network on predefined data.
std::vector< std::string > & _activation_function
Activation functions for each hidden layer.
StochasticTools::Standardizer & _output_standardizer
Standardizer for use with output response (y)
virtual void postTrain() override
Contains processes which are executed after the training loop.
std::vector< Real > _flattened_data
The gathered data in a flattened form to be able to convert easily to torch::Tensor.
std::vector< unsigned int > & _num_neurons_per_layer
Number of neurons within the hidden layers (the length of this vector should be the same as _num_hidd...
StochasticTools::Standardizer & _input_standardizer
Standardizer for use with input (x)
std::vector< Real > _flattened_response
The gathered response in a flattened form to be able to convert easily to torch::Tensor.
const std::string _nn_filename
Name of the pytorch output file.
const std::vector< Real > & _predictor_row
Data from the current predictor row.
LibtorchANNTrainer(const InputParameters ¶meters)
Construct using input parameters.
const bool _standardize_output
If the training output should be standardized (scaled and shifted)
virtual void preTrain() override
Contains processes which are executed before the training loop.
static InputParameters validParams()
virtual void train() override
Contains processes which are executed for every sample in the training loop.
Moose::LibtorchTrainingOptions _optim_options
The struct which contains the information for the training of the neural net.
std::shared_ptr< Moose::LibtorchArtificialNeuralNet > & _nn
Pointer to the neural net object (initialized as null)
const bool _read_from_file
Switch indicating if an already existing neural net should be read from a file or not.
const bool _standardize_input
If the training output should be standardized (scaled and shifted)
void mooseError(Args &&... args) const
virtual void train(LibtorchDataset &dataset, const LibtorchTrainingOptions &options)
This is the main trainer base class.
unsigned int getLocalSampleSize() const
const Real * _rval
Response value.
static InputParameters validParams()
unsigned int _n_dims
Dimension of predictor data - either _sampler.getNumberOfCols() or _pvals.size() + _pcols....
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
const Parallel::Communicator & _communicator
const Parallel::Communicator & comm() const
void tensorToVector(torch::Tensor &tensor, std::vector< DataType > &vector)
unsigned int parallel_processes
unsigned int print_epoch_loss