n2p2 - A neural network potential package
NeuralNetwork.h
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef NEURALNETWORK_H
18#define NEURALNETWORK_H
19
20#include <fstream> // std::ofstream
21#include <string> // std::string
22#include <vector> // std::vector
23
24namespace nnp
25{
26
29{
30public:
33 {
56 };
57
60 {
113 };
114
125 int numLayers,
126 int const* const& numNeuronsPerLayer,
127 ActivationFunction const* const& activationFunctionsPerLayer);
129 // Prevent copying.
130 //NeuralNetwork(const NeuralNetwork&) = delete;
131 //NeuralNetwork(NeuralNetwork&&) = delete;
141 int getNumNeurons() const;
146 int getNumConnections() const;
149 int getNumWeights() const;
152 int getNumBiases() const;
182 void setConnections(double const* const& connections);
189 void getConnections(double* connections) const;
198 unsigned int seed);
206 ModificationScheme modificationScheme);
216 ModificationScheme modificationScheme,
217 double parameter1,
218 double parameter2);
223 void setInput(double const* const& input) const;
229 void setInput(std::size_t const index,
230 double const value) const;
235 void getOutput(double* output) const;
242 void propagate();
255 void calculateDEdG(double* dEdG) const;
269 void calculateDEdc(double* dEdc) const;
308 void calculateDFdc(double* dFdc,
309 double const* const& dGdxyz) const;
314 void writeConnections(std::ofstream& file) const;
339 void getNeuronStatistics(long* count,
340 double* min,
341 double* max,
342 double* sum,
343 double* sum2) const;
349 //void writeStatus(int, int);
350 long getMemoryUsage();
353 std::vector<std::string> info() const;
354
355private:
357 typedef struct
358 {
360 long count;
362 double x;
364 double value;
367 double dfdx;
370 double d2fdx2;
373 double bias;
377 double dxdG;
379 double min;
381 double max;
383 double sum;
385 double sum2;
390 double* weights;
391 } Neuron;
392
394 typedef struct
395 {
404 } Layer;
405
430
442 void calculateDEdb(double* dEdb) const;
452 void calculateDxdG(int index) const;
468 void calculateD2EdGdc(int index,
469 double const* const& dEdb,
470 double* d2EdGdc) const;
482 void allocateLayer(Layer& layer,
483 int numNeuronsPrevLayer,
484 int numNeurons,
485 ActivationFunction activationFunction);
493 void propagateLayer(Layer& layer, Layer& layerPrev);
494};
495
503
504}
505
506#endif
This class implements a feed-forward neural network.
Definition: NeuralNetwork.h:29
ActivationFunction
List of available activation function types.
Definition: NeuralNetwork.h:33
@ AF_UNSET
Unset activation function.
Definition: NeuralNetwork.h:35
@ AF_RELU
(NOT recommended for HDNNPs!)
Definition: NeuralNetwork.h:45
int getNumConnections() const
Return total number of connections.
Layer * inputLayer
Pointer to input layer.
void setInput(double const *const &input) const
Set neural network input layer node values.
int getNumNeurons() const
Return total number of neurons.
int * biasOnlyOffset
Offset adress of biases per layer in bias only array.
void modifyConnections(ModificationScheme modificationScheme)
Change connections according to a given modification scheme.
int numLayers
Total number of layers (includes input and output layers).
void calculateDEdb(double *dEdb) const
Calculate derivative of output neuron with respect to biases.
int getNumWeights() const
Return number of weights.
void setConnections(double const *const &connections)
Set neural network weights and biases.
void setInput(std::size_t const index, double const value) const
Set neural network input layer node values.
void writeConnections(std::ofstream &file) const
Write connections to file.
bool normalizeNeurons
If neurons are normalized.
void calculateDFdc(double *dFdc, double const *const &dGdxyz) const
Calculate "second" derivative of output with respect to connections.
void propagateLayer(Layer &layer, Layer &layerPrev)
Propagate information from one layer to the next.
void allocateLayer(Layer &layer, int numNeuronsPrevLayer, int numNeurons, ActivationFunction activationFunction)
Allocate a single layer.
int * weightOffset
Offset adress of weights per layer in combined weights+bias array.
void initializeConnectionsRandomUniform(unsigned int seed)
Initialize connections with random numbers.
int * biasOffset
Offset adress of biases per layer in combined weights+bias array.
void calculateD2EdGdc(int index, double const *const &dEdb, double *d2EdGdc) const
Calculate second derivative of output neuron with respect to input neuron and connections.
void getNeuronStatistics(long *count, double *min, double *max, double *sum, double *sum2) const
Return gathered neuron statistics.
int numBiases
Number of NN biases only.
void resetNeuronStatistics()
Reset neuron statistics.
ModificationScheme
List of available connection modification schemes.
Definition: NeuralNetwork.h:60
@ MS_ZEROOUTPUTWEIGHTS
Set all weights connecting to the output layer to zero.
Definition: NeuralNetwork.h:64
@ MS_ZEROBIAS
Set all bias values to zero.
Definition: NeuralNetwork.h:62
@ MS_PRECONDITIONOUTPUT
Apply preconditioning to output layer connections.
@ MS_FANIN
Normalize weights via number of neuron inputs (fan-in).
Definition: NeuralNetwork.h:74
@ MS_GLOROTBENGIO
Normalize connections according to Glorot and Bengio.
Definition: NeuralNetwork.h:90
@ MS_NGUYENWIDROW
Initialize connections according to Nguyen-Widrow scheme.
void getConnections(double *connections) const
Get neural network weights and biases.
int numConnections
Number of NN connections (weights + biases).
void propagate()
Propagate input information through all layers.
void calculateDEdc(double *dEdc) const
Calculate derivative of output neuron with respect to connections.
void setNormalizeNeurons(bool normalizeNeurons)
Turn on/off neuron normalization.
int getNumBiases() const
Return number of biases.
void calculateDEdG(double *dEdG) const
Calculate derivative of output neuron with respect to input neurons.
void calculateDxdG(int index) const
Calculate derivative of neuron values before activation function with respect to input neuron.
Layer * layers
Neural network layers.
int numWeights
Number of NN weights only.
void getOutput(double *output) const
Get neural network output layer node values.
Layer * outputLayer
Pointer to output layer.
NeuralNetwork(int numLayers, int const *const &numNeuronsPerLayer, ActivationFunction const *const &activationFunctionsPerLayer)
Neural network class constructor.
int numHiddenLayers
Number of hidden layers.
std::vector< std::string > info() const
Print neural network architecture.
Definition: Atom.h:29
NeuralNetwork::ActivationFunction activationFromString(std::string c)
Convert string to activation function.
One neural network layer.
int numNeurons
Number of neurons in this layer .
Neuron * neurons
Array of neurons in this layer.
ActivationFunction activationFunction
Common activation function for all neurons in this layer.
int numNeuronsPrevLayer
Number of neurons in previous layer .
double * weights
NN weights assigned to neuron.
double bias
Bias value assigned to this neuron (if this is neuron this bias value is ).
double max
Maximum neuron value over data set (neuron statistics).
double sum
Sum of neuron values over data set (neuron statistics).
double dxdG
Derivative of neuron value before application of activation function with respect to input layer neur...
double value
Neuron value.
long count
How often the value of this neuron has been evaluated.
double d2fdx2
Second derivative of activation function with respect to its argument .
double dfdx
Derivative of activation function with respect to its argument .
double min
Minimum neuron value over data set (neuron statistics).
double x
Neuron value before application of activation function.
double sum2
Sum of squared neuron values over data set (neuron statistics).