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 {
54 };
55
58 {
111 };
112
123 int numLayers,
124 int const* const& numNeuronsPerLayer,
125 ActivationFunction const* const& activationFunctionsPerLayer);
127 // Prevent copying.
128 //NeuralNetwork(const NeuralNetwork&) = delete;
129 //NeuralNetwork(NeuralNetwork&&) = delete;
139 int getNumNeurons() const;
144 int getNumConnections() const;
147 int getNumWeights() const;
150 int getNumBiases() const;
180 void setConnections(double const* const& connections);
187 void getConnections(double* connections) const;
196 unsigned int seed);
204 ModificationScheme modificationScheme);
214 ModificationScheme modificationScheme,
215 double parameter1,
216 double parameter2);
221 void setInput(double const* const& input) const;
227 void setInput(std::size_t const index,
228 double const value) const;
233 void getOutput(double* output) const;
240 void propagate();
253 void calculateDEdG(double* dEdG) const;
267 void calculateDEdc(double* dEdc) const;
305 void calculateDFdc(double* dFdc,
306 double const* const& dGdxyz) const;
311 void writeConnections(std::ofstream& file) const;
336 void getNeuronStatistics(long* count,
337 double* min,
338 double* max,
339 double* sum,
340 double* sum2) const;
346 //void writeStatus(int, int);
347 long getMemoryUsage();
350 std::vector<std::string> info() const;
351
352private:
354 typedef struct
355 {
357 long count;
359 double x;
361 double value;
364 double dfdx;
367 double d2fdx2;
370 double bias;
374 double dxdG;
376 double min;
378 double max;
380 double sum;
382 double sum2;
387 double* weights;
388 } Neuron;
389
391 typedef struct
392 {
401 } Layer;
402
427
439 void calculateDEdb(double* dEdb) const;
449 void calculateDxdG(int index) const;
465 void calculateD2EdGdc(int index,
466 double const* const& dEdb,
467 double* d2EdGdc) const;
479 void allocateLayer(Layer& layer,
480 int numNeuronsPrevLayer,
481 int numNeurons,
482 ActivationFunction activationFunction);
490 void propagateLayer(Layer& layer, Layer& layerPrev);
491};
492
493}
494
495#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_RELU
(NOT recommended for HDNNPs!)
Definition: NeuralNetwork.h:43
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:58
@ MS_ZEROOUTPUTWEIGHTS
Set all weights connecting to the output layer to zero.
Definition: NeuralNetwork.h:62
@ MS_ZEROBIAS
Set all bias values to zero.
Definition: NeuralNetwork.h:60
@ MS_PRECONDITIONOUTPUT
Apply preconditioning to output layer connections.
@ MS_FANIN
Normalize weights via number of neuron inputs (fan-in).
Definition: NeuralNetwork.h:72
@ MS_GLOROTBENGIO
Normalize connections according to Glorot and Bengio.
Definition: NeuralNetwork.h:88
@ 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:28
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).