10 #ifdef MOOSE_LIBTORCH_ENABLED 14 #include <torch/torch.h> 15 #include <torch/script.h> 20 #include "nlohmann/json.h" 37 const unsigned int num_inputs,
38 const unsigned int num_outputs,
39 const std::vector<unsigned int> & num_neurons_per_layer,
40 const std::vector<std::string> & activation_function = {
"relu"},
41 const torch::DeviceType device_type = torch::kCPU,
42 const torch::ScalarType scalar_type = torch::kDouble);
56 virtual void addLayer(
const std::string & layer_name,
57 const std::unordered_map<std::string, unsigned int> & parameters);
64 virtual torch::Tensor
forward(
const torch::Tensor & x)
override;
86 void store(nlohmann::json & json)
const;
114 void dataStore<Moose::LibtorchArtificialNeuralNet>(
115 std::ostream & stream,
116 std::shared_ptr<Moose::LibtorchArtificialNeuralNet> & nn,
120 void dataLoad<Moose::LibtorchArtificialNeuralNet>(
121 std::istream & stream,
122 std::shared_ptr<Moose::LibtorchArtificialNeuralNet> & nn,
129 void dataStore<Moose::LibtorchArtificialNeuralNet const>(
133 void dataLoad<Moose::LibtorchArtificialNeuralNet const>(
MultiMooseEnum _activation_function
Activation functions (either one for all hidden layers or one for every layer separately) ...
unsigned int numInputs() const
Return the number of neurons on the input layer.
const torch::DeviceType _device_type
The device type used for this neural network.
unsigned int numHiddenLayers() const
Return the number of hidden layers.
const std::vector< unsigned int > _num_neurons_per_layer
Hidden layer architecture.
torch::DeviceType deviceType() const
Return the device which is used by this neural network.
std::vector< torch::nn::Linear > _weights
Submodules that hold linear operations and the corresponding weights and biases (y = W * x + b) ...
void constructNeuralNetwork()
Construct the neural network.
const std::string _name
Name of the neural network.
const MultiMooseEnum & activationFunctions() const
Return the multi enum containing the activation functions.
virtual void addLayer(const std::string &layer_name, const std::unordered_map< std::string, unsigned int > ¶meters)
Add layers to the neural network.
torch::ScalarType dataType() const
Return the data type which is used by this neural network.
This base class is meant to gather the functions and members common in every neural network based on ...
const std::string & name() const
Return the name of the neural network.
void store(nlohmann::json &json) const
Store the network architecture in a json file (for debugging, visualization)
void to_json(nlohmann::json &json, const Moose::LibtorchArtificialNeuralNet *const &network)
unsigned int numOutputs() const
Return the number of neurons on the output layer.
const unsigned int _num_outputs
Number of neurons on the output layer.
const torch::ScalarType _data_type
The data type used in this neural network.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...
const std::vector< unsigned int > & numNeuronsPerLayer() const
Return the hidden layer architecture.
LibtorchArtificialNeuralNet(const std::string name, const unsigned int num_inputs, const unsigned int num_outputs, const std::vector< unsigned int > &num_neurons_per_layer, const std::vector< std::string > &activation_function={"relu"}, const torch::DeviceType device_type=torch::kCPU, const torch::ScalarType scalar_type=torch::kDouble)
Construct using input parameters.
const unsigned int _num_inputs
virtual torch::Tensor forward(const torch::Tensor &x) override
Overriding the forward substitution function for the neural network, unfortunately this cannot be con...