https://mooseframework.inl.gov
LibtorchANNTrainer.h
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 
10 #ifdef LIBTORCH_ENABLED
11 
12 #pragma once
13 
14 #include <torch/torch.h>
17 #include "libmesh/utility.h"
18 #include "SurrogateTrainer.h"
19 #include "Standardizer.h"
20 #include "LibtorchUtils.h"
21 
24 {
25 public:
27 
30 
32  virtual void preTrain() override;
33 
35  virtual void train() override;
36 
38  virtual void postTrain() override;
39 
40 private:
42  const std::vector<Real> & _predictor_row;
43 
45  std::vector<Real> _flattened_data;
46 
48  std::vector<Real> _flattened_response;
49 
52  std::vector<unsigned int> & _num_neurons_per_layer;
53 
55  std::vector<std::string> & _activation_function;
56 
59  const std::string _nn_filename;
60 
64  const bool _read_from_file;
65 
68 
70  std::shared_ptr<Moose::LibtorchArtificialNeuralNet> & _nn;
71 
73  const bool _standardize_input;
74 
76  const bool _standardize_output;
77 
80 
83 };
84 
85 #endif
LibtorchANNTrainer(const InputParameters &parameters)
Construct using input parameters.
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...
virtual void train() override
Contains processes which are executed for every sample in the training loop.
const bool _standardize_output
If the training output should be standardized (scaled and shifted)
std::vector< Real > _flattened_response
The gathered response in a flattened form to be able to convert easily to torch::Tensor.
virtual void preTrain() override
Contains processes which are executed before the training loop.
const std::vector< Real > & _predictor_row
Data from the current predictor row.
const bool _standardize_input
If the training output should be standardized (scaled and shifted)
Trainer responsible of fitting a neural network on predefined data.
const std::string _nn_filename
Name of the pytorch output file.
Moose::LibtorchTrainingOptions _optim_options
The struct which contains the information for the training of the neural net.
const bool _read_from_file
Switch indicating if an already existing neural net should be read from a file or not...
This is the main trainer base class.
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.
Class for standardizing data (centering and scaling)
Definition: Standardizer.h:21
const InputParameters & parameters() const
std::shared_ptr< Moose::LibtorchArtificialNeuralNet > & _nn
Pointer to the neural net object (initialized as null)
StochasticTools::Standardizer & _input_standardizer
Standardizer for use with input (x)
std::vector< Real > _flattened_data
The gathered data in a flattened form to be able to convert easily to torch::Tensor.
static InputParameters validParams()