Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvar_neuralnetworkcompute.h
1// -*- c++ -*-
2
3// This file is part of the Collective Variables module (Colvars).
4// The original version of Colvars and its updates are located at:
5// https://github.com/Colvars/colvars
6// Please update all Colvars source files before making any changes.
7// If you wish to distribute your changes, please submit them to the
8// Colvars repository at GitHub.
9
10#ifndef NEURALNETWORKCOMPUTE_H
11#define NEURALNETWORKCOMPUTE_H
12
13#include <vector>
14#include <functional>
15#include <string>
16#include <cmath>
17#include <memory>
18#include <map>
19
20#ifdef LEPTON
21#include "Lepton.h"
22#endif
23
24class colvarmodule; // Forward declaration
25
26namespace neuralnetworkCV {
28extern std::map<std::string, std::pair<std::function<double(double)>, std::function<double(double)>>> activation_function_map;
29
30#ifdef LEPTON
31// allow to define a custom activation function
32class customActivationFunction {
33public:
35 customActivationFunction();
37 customActivationFunction(const std::string& expression_string, colvarmodule *cvmodule);
39 customActivationFunction(const customActivationFunction& source);
41 customActivationFunction& operator=(const customActivationFunction& source);
43 void setExpression(const std::string& expression_string);
45 std::string getExpression() const;
47 double evaluate(double x) const;
49 double derivative(double x) const;
50private:
51 std::string expression;
52 std::unique_ptr<Lepton::CompiledExpression> value_evaluator;
53 std::unique_ptr<Lepton::CompiledExpression> gradient_evaluator;
54 double* input_reference;
55 double* derivative_reference;
56 colvarmodule *cvmodule;
57};
58#endif
59
61private:
62 size_t m_input_size;
63 size_t m_output_size;
64 std::function<double(double)> m_activation_function;
65 std::function<double(double)> m_activation_function_derivative;
66 colvarmodule *cvmodule;
67#ifdef LEPTON
68 bool m_use_custom_activation;
69 customActivationFunction m_custom_activation_function;
70#else
71 static const bool m_use_custom_activation = false;
72#endif
74 std::vector<std::vector<double>> m_weights;
76 std::vector<double> m_biases;
77public:
85 denseLayer(const std::string& weights_file,
86 const std::string& biases_file,
87 const std::function<double(double)>& f,
88 const std::function<double(double)>& df,
89 colvarmodule *cvmodule);
90#ifdef LEPTON
95 denseLayer(const std::string& weights_file,
96 const std::string& biases_file,
97 const std::string& custom_activation_expression,
98 colvarmodule *cvmodule);
99#endif
101 void readFromFile(const std::string& weights_file, const std::string& biases_file);
103 void setActivationFunction(const std::function<double(double)>& f, const std::function<double(double)>& df);
105 void compute(const std::vector<double>& input, std::vector<double>& output) const;
107 double computeGradientElement(const std::vector<double>& input, const size_t i, const size_t j) const;
109 void computeGradient(const std::vector<double>& input, std::vector<std::vector<double>>& output_grad) const;
111 size_t getInputSize() const {
112 return m_input_size;
113 }
115 size_t getOutputSize() const {
116 return m_output_size;
117 }
119 double getWeight(size_t i, size_t j) const {
120 return m_weights[i][j];
121 }
122 double getBias(size_t i) const {
123 return m_biases[i];
124 }
125 ~denseLayer() {}
126};
127
129private:
130 std::vector<denseLayer> m_dense_layers;
131 std::vector<double> m_input;
133 std::vector<std::vector<double>> m_layers_output;
134 std::vector<std::vector<std::vector<double>>> m_grads_tmp;
135 std::vector<std::vector<double>> m_chained_grad;
136private:
138 static std::vector<std::vector<double>> multiply_matrix(const std::vector<std::vector<double>>& A, const std::vector<std::vector<double>>& B);
139public:
140 neuralNetworkCompute(): m_dense_layers(0), m_layers_output(0) {}
141 neuralNetworkCompute(const std::vector<denseLayer>& dense_layers);
142 bool addDenseLayer(const denseLayer& layer);
143 // for faster computation
144 const std::vector<double>& input() const {return m_input;}
145 std::vector<double>& input() {return m_input;}
147 void compute();
148 double getOutput(const size_t i) const {return m_layers_output.back()[i];}
149 double getGradient(const size_t i, const size_t j) const {return m_chained_grad[i][j];}
151 const denseLayer& getLayer(const size_t i) const {return m_dense_layers[i];}
153 size_t getNumberOfLayers() const {return m_dense_layers.size();}
154};
155
156}
157#endif
Collective variables module (main class)
Definition: colvarmodule.h:72
Definition: colvar_neuralnetworkcompute.h:60
double getWeight(size_t i, size_t j) const
getter for weights and biases
Definition: colvar_neuralnetworkcompute.h:119
void readFromFile(const std::string &weights_file, const std::string &biases_file)
read data from file
Definition: colvar_neuralnetworkcompute.cpp:146
void computeGradient(const std::vector< double > &input, std::vector< std::vector< double > > &output_grad) const
output[i][j] is the gradient of i-th output wrt j-th input
Definition: colvar_neuralnetworkcompute.cpp:240
double computeGradientElement(const std::vector< double > &input, const size_t i, const size_t j) const
compute the gradient of i-th output wrt j-th input
Definition: colvar_neuralnetworkcompute.cpp:221
denseLayer()
empty constructor
Definition: colvar_neuralnetworkcompute.h:79
size_t getOutputSize() const
get the output size
Definition: colvar_neuralnetworkcompute.h:115
void compute(const std::vector< double > &input, std::vector< double > &output) const
compute the value of this layer
Definition: colvar_neuralnetworkcompute.cpp:202
std::vector< double > m_biases
bias of each node
Definition: colvar_neuralnetworkcompute.h:76
size_t getInputSize() const
get the input size
Definition: colvar_neuralnetworkcompute.h:111
void setActivationFunction(const std::function< double(double)> &f, const std::function< double(double)> &df)
setup activation function
Definition: colvar_neuralnetworkcompute.cpp:197
std::vector< std::vector< double > > m_weights
weights[i][j] is the weight of the i-th output and the j-th input
Definition: colvar_neuralnetworkcompute.h:74
Definition: colvar_neuralnetworkcompute.h:128
std::vector< std::vector< double > > m_layers_output
temporary output for each layer, useful to speedup the gradients' calculation
Definition: colvar_neuralnetworkcompute.h:133
static std::vector< std::vector< double > > multiply_matrix(const std::vector< std::vector< double > > &A, const std::vector< std::vector< double > > &B)
helper function: multiply two matrix constructed from 2D vector
Definition: colvar_neuralnetworkcompute.cpp:277
size_t getNumberOfLayers() const
get the number of layers
Definition: colvar_neuralnetworkcompute.h:153
void compute()
compute the values and the gradients of all output nodes
Definition: colvar_neuralnetworkcompute.cpp:298
const denseLayer & getLayer(const size_t i) const
get a specified layer
Definition: colvar_neuralnetworkcompute.h:151