https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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 MOOSE_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{
25public:
27
30
32 virtual void preTrain() override;
33
35 virtual void train() override;
36
38 virtual void postTrain() override;
39
40private:
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
74
77
80
83};
84
85#endif
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.
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)
const InputParameters & parameters() const
Class for standardizing data (centering and scaling)
This is the main trainer base class.