10#ifndef NEURALNETWORKCOMPUTE_H
11#define NEURALNETWORKCOMPUTE_H
26namespace neuralnetworkCV {
28extern std::map<std::string, std::pair<std::function<double(
double)>, std::function<double(
double)>>> activation_function_map;
32class customActivationFunction {
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;
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;
64 std::function<double(
double)> m_activation_function;
65 std::function<double(
double)> m_activation_function_derivative;
68 bool m_use_custom_activation;
69 customActivationFunction m_custom_activation_function;
71 static const bool m_use_custom_activation =
false;
86 const std::string& biases_file,
87 const std::function<
double(
double)>& f,
88 const std::function<
double(
double)>& df,
96 const std::string& biases_file,
97 const std::string& custom_activation_expression,
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;
109 void computeGradient(
const std::vector<double>& input, std::vector<std::vector<double>>& output_grad)
const;
116 return m_output_size;
122 double getBias(
size_t i)
const {
130 std::vector<denseLayer> m_dense_layers;
131 std::vector<double> m_input;
134 std::vector<std::vector<std::vector<double>>> m_grads_tmp;
135 std::vector<std::vector<double>> m_chained_grad;
138 static std::vector<std::vector<double>>
multiply_matrix(
const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& B);
144 const std::vector<double>& input()
const {
return m_input;}
145 std::vector<double>& input() {
return m_input;}
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];}
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