Simple feed-forward neural net with optional affine input and output scaling. More...
#include <LibtorchArtificialNeuralNet.h>
Public Member Functions | |
| 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, const bool build_on_construct=true, const std::vector< Real > &input_shift_factors={}, const std::vector< Real > &input_scaling_factors={}, const std::vector< Real > &output_scaling_factors={}) | |
| Build a plain feed-forward neural network. | |
| LibtorchArtificialNeuralNet (const Moose::LibtorchArtificialNeuralNet &nn, const bool build_on_construct=true) | |
| Copy-construct a feed-forward neural network. | |
| virtual void | addLayer (const std::string &layer_name, const std::unordered_map< std::string, unsigned int > ¶meters) |
| Add one linear layer to the network. | |
| virtual torch::Tensor | forward (const torch::Tensor &x) override |
| Run a forward pass through the network. | |
| const std::string & | name () const |
| Return the name of the neural network. | |
| unsigned int | numInputs () const |
| Return the number of neurons on the input layer. | |
| unsigned int | numOutputs () const |
| Return the number of neurons on the output layer. | |
| unsigned int | numHiddenLayers () const |
| Return the number of hidden layers. | |
| const std::vector< unsigned int > & | numNeuronsPerLayer () const |
| Return the hidden layer architecture. | |
| const MultiMooseEnum & | activationFunctions () const |
| Return the multi enum containing the activation functions. | |
| torch::DeviceType | deviceType () const |
| Return the device used by this neural network. | |
| torch::ScalarType | dataType () const |
| Return the data type used by this neural network. | |
| const std::vector< Real > & | inputShiftFactors () const |
| Return the affine input shift factors used before evaluation. | |
| const std::vector< Real > & | inputScalingFactors () const |
| Return the affine input scaling factors used before evaluation. | |
| const std::vector< Real > & | outputScalingFactors () const |
| Return the output scaling factors applied after evaluation. | |
| virtual void | constructNeuralNetwork () |
| Construct the neural network. | |
| void | synchronizeAffineFactorsFromBuffers () |
| Update cached affine metadata vectors from the registered libtorch buffers. | |
| Real | determineGain (const std::string &activation) |
| Map an activation name to the orthogonal-initialization gain we want to use. | |
| virtual void | initializeNeuralNetwork (c10::optional< at::Generator > generator=c10::nullopt) |
| Initialize the trainable weights and biases. | |
| void | store (nlohmann::json &json) const |
| Store the network architecture in a JSON object for debugging or visualization. | |
Protected Member Functions | |
| void | initializeAffineBuffers () |
| Initialize the registered affine metadata buffers used by serialization. | |
| virtual torch::Tensor | preprocessInput (const torch::Tensor &x) const |
| Apply affine preprocessing to the raw input tensor. | |
| virtual torch::Tensor | scaleOutput (const torch::Tensor &y) const |
| Apply the configured output scaling to a network output tensor. | |
Static Protected Member Functions | |
| static std::vector< Real > | setAffineFactors (const std::vector< Real > &factors, unsigned int expected_size, Real default_value, const std::string &factor_name) |
| Set affine metadata by either accepting the user values or filling defaults. | |
Protected Attributes | |
| const std::string | _name |
| Name of the neural network. | |
| std::vector< torch::nn::Linear > | _weights |
| Submodules that hold linear operations and the corresponding weights and biases (y = W * x + b) | |
| const unsigned int | _num_inputs |
| Number of neurons on the input layer. | |
| const unsigned int | _num_outputs |
| Number of neurons on the output layer. | |
| const std::vector< unsigned int > | _num_neurons_per_layer |
| Hidden layer architecture. | |
| MultiMooseEnum | _activation_function |
| Activation functions (either one for all hidden layers or one for every layer separately) | |
| const torch::DeviceType | _device_type |
| The device type used for this neural network. | |
| const torch::ScalarType | _data_type |
| The data type used in this neural network. | |
| std::vector< Real > | _input_shift_factors |
| Affine preprocessing applied to the flattened input. | |
| std::vector< Real > | _input_scaling_factors |
| Multiplicative affine preprocessing applied after shifting the input. | |
| std::vector< Real > | _output_scaling_factors |
| Multiplicative scaling applied after the network output is formed. | |
| torch::Tensor | _input_shift_tensor |
| Registered libtorch buffer holding the affine input shifts. | |
| torch::Tensor | _input_scale_tensor |
| Registered libtorch buffer holding the affine input scaling factors. | |
| torch::Tensor | _output_scale_tensor |
| Registered libtorch buffer holding the output scaling factors. | |
Simple feed-forward neural net with optional affine input and output scaling.
Definition at line 29 of file LibtorchArtificialNeuralNet.h.
| Moose::LibtorchArtificialNeuralNet::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, |
||
| const bool | build_on_construct = true, |
||
| const std::vector< Real > & | input_shift_factors = {}, |
||
| const std::vector< Real > & | input_scaling_factors = {}, |
||
| const std::vector< Real > & | output_scaling_factors = {} |
||
| ) |
Build a plain feed-forward neural network.
| name | Name of the neural network module. |
| num_inputs | Number of input neurons or parameters. |
| num_outputs | Number of output neurons. |
| num_neurons_per_layer | Hidden-layer widths. |
| activation_function | Hidden-layer activation names. |
| device_type | Torch device used by the module. |
| scalar_type | Torch scalar type used by the module. |
| build_on_construct | Whether to build the torch modules right away. |
| input_shift_factors | Optional affine input shifts. |
| input_scaling_factors | Optional affine input scales. |
| output_scaling_factors | Optional output scaling factors. |
Definition at line 198 of file LibtorchArtificialNeuralNet.C.
| Moose::LibtorchArtificialNeuralNet::LibtorchArtificialNeuralNet | ( | const Moose::LibtorchArtificialNeuralNet & | nn, |
| const bool | build_on_construct = true |
||
| ) |
Copy-construct a feed-forward neural network.
| nn | Neural network to copy. |
| build_on_construct | Whether to rebuild the module structure during the copy. |
Definition at line 237 of file LibtorchArtificialNeuralNet.C.
|
inline |
Return the multi enum containing the activation functions.
Definition at line 103 of file LibtorchArtificialNeuralNet.h.
|
virtual |
Add one linear layer to the network.
| layer_name | Name of the layer to add. |
| parameters | Small parameter map that describes the layer shape. |
Definition at line 412 of file LibtorchArtificialNeuralNet.C.
Referenced by constructNeuralNetwork().
|
virtual |
Construct the neural network.
Definition at line 346 of file LibtorchArtificialNeuralNet.C.
Referenced by LibtorchArtificialNeuralNet(), and LibtorchArtificialNeuralNet().
|
inline |
Return the data type used by this neural network.
Definition at line 111 of file LibtorchArtificialNeuralNet.h.
| Real Moose::LibtorchArtificialNeuralNet::determineGain | ( | const std::string & | activation | ) |
Map an activation name to the orthogonal-initialization gain we want to use.
| activation | Activation name to look up. |
Definition at line 271 of file LibtorchArtificialNeuralNet.C.
Referenced by initializeNeuralNetwork().
|
inline |
Return the device used by this neural network.
Definition at line 107 of file LibtorchArtificialNeuralNet.h.
|
overridevirtual |
Run a forward pass through the network.
| x | Input tensor for the evaluation. |
Implements Moose::LibtorchNeuralNetBase.
Definition at line 386 of file LibtorchArtificialNeuralNet.C.
Referenced by forward().
|
protected |
Initialize the registered affine metadata buffers used by serialization.
Definition at line 313 of file LibtorchArtificialNeuralNet.C.
Referenced by LibtorchArtificialNeuralNet(), and LibtorchArtificialNeuralNet().
|
virtual |
Initialize the trainable weights and biases.
| generator | Optional torch random-number generator used for reproducible initialization. |
Definition at line 282 of file LibtorchArtificialNeuralNet.C.
|
inline |
Return the affine input scaling factors used before evaluation.
Definition at line 119 of file LibtorchArtificialNeuralNet.h.
|
inline |
Return the affine input shift factors used before evaluation.
Definition at line 115 of file LibtorchArtificialNeuralNet.h.
|
inline |
|
inline |
Return the number of hidden layers.
Definition at line 95 of file LibtorchArtificialNeuralNet.h.
Referenced by constructNeuralNetwork(), and initializeNeuralNetwork().
|
inline |
Return the number of neurons on the input layer.
Definition at line 87 of file LibtorchArtificialNeuralNet.h.
|
inline |
|
inline |
Return the number of neurons on the output layer.
Definition at line 91 of file LibtorchArtificialNeuralNet.h.
|
inline |
Return the output scaling factors applied after evaluation.
Definition at line 123 of file LibtorchArtificialNeuralNet.h.
|
protectedvirtual |
Apply affine preprocessing to the raw input tensor.
| x | Raw input tensor. |
Definition at line 368 of file LibtorchArtificialNeuralNet.C.
Referenced by forward().
|
protectedvirtual |
Apply the configured output scaling to a network output tensor.
| y | Raw network output tensor. |
Definition at line 380 of file LibtorchArtificialNeuralNet.C.
Referenced by forward().
|
staticprotected |
Set affine metadata by either accepting the user values or filling defaults.
| factors | User-provided affine factors. |
| expected_size | Expected number of entries. |
| default_value | Default value used when the vector is empty. |
| factor_name | Name used in error messages. |
Definition at line 298 of file LibtorchArtificialNeuralNet.C.
| void Moose::LibtorchArtificialNeuralNet::store | ( | nlohmann::json & | json | ) | const |
Store the network architecture in a JSON object for debugging or visualization.
| json | JSON object that receives the network architecture. |
Definition at line 432 of file LibtorchArtificialNeuralNet.C.
Referenced by Moose::to_json().
| void Moose::LibtorchArtificialNeuralNet::synchronizeAffineFactorsFromBuffers | ( | ) |
Update cached affine metadata vectors from the registered libtorch buffers.
Definition at line 332 of file LibtorchArtificialNeuralNet.C.
|
protected |
Activation functions (either one for all hidden layers or one for every layer separately)
Definition at line 195 of file LibtorchArtificialNeuralNet.h.
Referenced by activationFunctions(), forward(), initializeNeuralNetwork(), and LibtorchArtificialNeuralNet().
|
protected |
The data type used in this neural network.
Definition at line 199 of file LibtorchArtificialNeuralNet.h.
Referenced by constructNeuralNetwork(), dataType(), initializeAffineBuffers(), and preprocessInput().
|
protected |
The device type used for this neural network.
Definition at line 197 of file LibtorchArtificialNeuralNet.h.
Referenced by constructNeuralNetwork(), deviceType(), initializeAffineBuffers(), and preprocessInput().
|
protected |
Registered libtorch buffer holding the affine input scaling factors.
Definition at line 209 of file LibtorchArtificialNeuralNet.h.
Referenced by initializeAffineBuffers(), preprocessInput(), and synchronizeAffineFactorsFromBuffers().
|
protected |
Multiplicative affine preprocessing applied after shifting the input.
Definition at line 203 of file LibtorchArtificialNeuralNet.h.
Referenced by initializeAffineBuffers(), inputScalingFactors(), store(), and synchronizeAffineFactorsFromBuffers().
|
protected |
Affine preprocessing applied to the flattened input.
Definition at line 201 of file LibtorchArtificialNeuralNet.h.
Referenced by initializeAffineBuffers(), inputShiftFactors(), store(), and synchronizeAffineFactorsFromBuffers().
|
protected |
Registered libtorch buffer holding the affine input shifts.
Definition at line 207 of file LibtorchArtificialNeuralNet.h.
Referenced by initializeAffineBuffers(), preprocessInput(), and synchronizeAffineFactorsFromBuffers().
|
protected |
Name of the neural network.
Definition at line 183 of file LibtorchArtificialNeuralNet.h.
Referenced by name().
|
protected |
Number of neurons on the input layer.
Definition at line 188 of file LibtorchArtificialNeuralNet.h.
Referenced by constructNeuralNetwork(), and numInputs().
|
protected |
Hidden layer architecture.
Definition at line 192 of file LibtorchArtificialNeuralNet.h.
Referenced by constructNeuralNetwork(), LibtorchArtificialNeuralNet(), numHiddenLayers(), and numNeuronsPerLayer().
|
protected |
Number of neurons on the output layer.
Definition at line 190 of file LibtorchArtificialNeuralNet.h.
Referenced by constructNeuralNetwork(), and numOutputs().
|
protected |
Registered libtorch buffer holding the output scaling factors.
Definition at line 211 of file LibtorchArtificialNeuralNet.h.
Referenced by initializeAffineBuffers(), scaleOutput(), and synchronizeAffineFactorsFromBuffers().
|
protected |
Multiplicative scaling applied after the network output is formed.
Definition at line 205 of file LibtorchArtificialNeuralNet.h.
Referenced by initializeAffineBuffers(), outputScalingFactors(), store(), and synchronizeAffineFactorsFromBuffers().
|
protected |
Submodules that hold linear operations and the corresponding weights and biases (y = W * x + b)
Definition at line 186 of file LibtorchArtificialNeuralNet.h.
Referenced by addLayer(), constructNeuralNetwork(), forward(), and initializeNeuralNetwork().